#!/bin/bash

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

PARAMS=( "$@" )
UPDATE_SERVER="update.2x.mikbill.pro"
TESTING=0
FORCE=0

while [[ $# -gt 0 ]]; do
  key="$1"

  case $key in
    -t|--testing)
      # shellcheck disable=SC2034
      TESTING=1
      UPDATE_SERVER="current.2x.mikbill.pro"
	  shift
      ;;
    -f|--force)
      FORCE=1
	  shift
      ;;
    *)
      # unknown option
	  shift
      ;;
  esac
done

LICENSE_DIR="/var/mikbill/license"
if [ ! -d "${LICENSE_DIR}" ]; then
  mkdir -p "${LICENSE_DIR}"
fi

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

FILE_NAME="mbcabapi"
MODULE="cabinetapi"
BILL_HOME="/var/mikbill"
WORK_DIR="/var/www/cabinetapi"
UPD_DIR="${BILL_HOME}/updates/${MODULE}"
BKP_DIR="${BILL_HOME}/www_backups"
TMP_DIR="/tmp/mikbill"

UPDATER_REVISION=5
UPDATER_NAME="cabinetapi_update.sh"

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

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

if [ ! -d "${LOG_DIR}" ]; then
  mkdir -p "${LOG_DIR}"
  upd_log "create log dir ${LOG_DIR}"
fi

if [ ! -f "${LOG_DIR}/general.log" ]; then
  touch ${LOG_DIR}/general.log
  chmod 755 ${LOG_DIR}/general.log
fi

if [ ! -f "${LOG_DIR}/debug.log" ]; then
  touch ${LOG_DIR}/debug.log
  chmod 755 ${LOG_DIR}/debug.log
fi

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

if [ ! -d "${BKP_DIR}" ]; then
  upd_log "create backup dir ${BKP_DIR}"
  mkdir -p "${BKP_DIR}"
fi

if [ ! -d "${UPD_DIR}" ]; then
  upd_log "create update dir ${UPD_DIR}"
  mkdir -p "${UPD_DIR}"
fi

if [ ! -d "${WORK_DIR}" ]; then
  upd_log "create work dir ${WORK_DIR}"
  mkdir -p "${WORK_DIR}"
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}" "${PARAMS[@]}"
    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.live" ]; then
  echo 0 > "${FILE_NAME}.current.checksum.live"
fi

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

if [ $FORCE -eq 1 ]; then
	upd_log "force mode"
	
	if [ $TESTING -eq 0 ]; then
	  echo 0 > "${FILE_NAME}.current.checksum.live"
	else 
	  echo 0 > "${FILE_NAME}.current.checksum.test"
	fi
fi

if [ $TESTING -eq 0 ]; then
  MD5_CURRENT=$(cat "${FILE_NAME}.current.checksum.live")
else
  MD5_CURRENT=$(cat "${FILE_NAME}.current.checksum.test")
fi

MD5_DOWNLOADED=$(cat "${FILE_NAME}.downloaded.checksum")
if [ "${MD5_CURRENT}" != "${MD5_DOWNLOADED}" ] || [ "$FORCE" -eq 1 ]; 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.live"
  else
    echo "${MD5_DOWNLOADED}" > "${UPD_DIR}/${FILE_NAME}.current.checksum.test"
  fi

  mv "${FILE_NAME}.tar.gz" "${WORK_DIR}/"
  cd "${WORK_DIR}" || exit

  if [ -d "${MODULE}" ]; then
    upd_log "backup current version to ${BKP_DIR}/${FILE_NAME}_${VERSION}.${DATENAME}.tar.gz"
    tar -zcf "${BKP_DIR}/${FILE_NAME}_${VERSION}.${DATENAME}.tar.gz" "${MODULE}/"
  fi

  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