#!/bin/bash

UPDATE_SERVER="update.2x.mikbill.pro"
if [[ -n $1 ]]; then
  TESTING=1
  case "$1" in
    "-t") UPDATE_SERVER="current.2x.mikbill.pro" ;;
    "--testing") UPDATE_SERVER="current.2x.mikbill.pro" ;;
    *) exit 0 ;;
  esac
else
  TESTING=0
fi

LICENSE_DIR="/var/bill/license"
URL="http://free.${UPDATE_SERVER}"
if [ "$(ls -A "${LICENSE_DIR}")" ]; then
  URL="http://pay.${UPDATE_SERVER}"
fi

FILE_NAME="mbdbupdate"
MODULE="mbdbupdate"
BILL_HOME="/var/bill"
VERSION="production"
WORK_DIR="${BILL_HOME}/${MODULE}/${VERSION}"
UPD_DIR="${BILL_HOME}/${MODULE}/${VERSION}"
TMP_DIR="/tmp/bill/install"

UPDATER_REVISION=2
UPDATER_NAME="mbdbupdate_update.sh"

DATENAME=$(date +%F_%H-%M)
INFO="[${DATENAME} ${VERSION}]"
LOG_DIR="${BILL_HOME}/logs/${MODULE}"
FILE_LOG="${LOG_DIR}/update.log"

function upd_log() {
  echo "${INFO} $1" >>"${FILE_LOG}"
}

if [ ! -d "${TMP_DIR}" ]; then
  upd_log "create tmp ${TMP_DIR}"
  mkdir "${TMP_DIR}"
fi

if [ ! -d "${WORK_DIR}" ]; then
  upd_log "${WORK_DIR} dir not found"
  exit 0
fi

if [ $TESTING -eq 1 ]; then
    upd_log "use testing update server"
fi

cd "${UPD_DIR}" || exit
upd_log "check version UPDATER SOFT"
SITE_REVISION=$(wget -qO- "${URL}/${UPDATER_NAME}.revision")
if [ -n "${SITE_REVISION}" ]; then
  if [ "${SITE_REVISION}" != "${UPDATER_REVISION}" ]; then
    upd_log "found new version for UPDATER SOFT"
    wget -q -O "${UPD_DIR}/${UPDATER_NAME}.new" "${URL}/${UPDATER_NAME}"
    if [ $? -ne 0 ]; then
      upd_log "download FAILED! please check DNS and connection to ${URL}/${UPDATER_NAME}"
      if [ -f "${UPD_DIR}/${UPDATER_NAME}.new" ]; then
        rm -f "${UPD_DIR}/${UPDATER_NAME}.new"
      fi
      exit 0
    fi

    upd_log "run NEW VERSION UPDATER SOFT"
    if [ -f "${UPD_DIR}/${UPDATER_NAME}" ]; then
      rm -f "${UPD_DIR}/${UPDATER_NAME}"
    fi

    mv "${UPD_DIR}/${UPDATER_NAME}.new" "${UPD_DIR}/${UPDATER_NAME}"
    chmod +x "${UPD_DIR}/${UPDATER_NAME}"
    bash "${UPD_DIR}/${UPDATER_NAME}"
    exit 0
  else
    upd_log "installed latest version UPDATER SOFT"
  fi
fi

wget -O "${FILE_NAME}.downloaded.checksum" -q "${URL}/${FILE_NAME}.checksum"
if [ $? -ne 0 ]; then
  upd_log "download FAILED! please check DNS and connection to ${URL}/${FILE_NAME}.checksum"
  if [ -f "${FILE_NAME}.downloaded.checksum" ]; then
    rm -f "${FILE_NAME}.downloaded.checksum"
  fi
  exit 0
fi

if [ ! -f "${FILE_NAME}.current.checksum" ]; then
  echo 0 >"${FILE_NAME}.current.checksum"
fi

MD5_CURRENT=$(cat "${FILE_NAME}.current.checksum")
MD5_DOWNLOADED=$(cat "${FILE_NAME}.downloaded.checksum")
if [ "${MD5_CURRENT}" != "${MD5_DOWNLOADED}" ]; then
  upd_log "download ${FILE_NAME} update..."
  cd "${TMP_DIR}" || exit

  wget -O "${FILE_NAME}.tar.gz" -q "${URL}/${FILE_NAME}.tar.gz"
  if [ $? -ne 0 ]; then
    upd_log "download FAILED! please check DNS and connection to ${URL}/${FILE_NAME}"
    if [ -f "${FILE_NAME}.tar.gz" ]; then
      rm -f "${FILE_NAME}.tar.gz"
    fi
    exit 0
  fi

  if [ $TESTING -eq 0 ]; then
    echo "${MD5_DOWNLOADED}" > "${UPD_DIR}/${FILE_NAME}.current.checksum"
  fi

  mv "${FILE_NAME}.tar.gz" "${WORK_DIR}/"
  cd "${WORK_DIR}" || exit
  if [ -f "${FILE_NAME}.tar.gz" ]; then
    upd_log "install new ${FILE_NAME} update"
    tar -zxf "${FILE_NAME}.tar.gz"
    rm -f "${FILE_NAME}.tar.gz"
  fi
else
  upd_log "installed latest ${FILE_NAME}"
fi

exit 0
