#!/bin/sh

[ -z "$ROOT" ] && ROOT=/www
CONFIG=$ROOT/config
SCRIPT=$ROOT/script
TEMPLATE=$ROOT/web/template

if [ -s $CONFIG/ng.conf ] ; then
  source $CONFIG/ng.conf
  source $CONFIG/ap.conf
  source $CONFIG/api.conf

  source $SCRIPT/utils/hotspot.sh
  ssids="`getHotspotInstanceList`"
else
  echo "No ng config found, update all template"
  source $CONFIG/ap.conf
  source $CONFIG/api.conf

  ssids=`seq 1 "$NO_SSID"`
fi

source $SCRIPT/utils/misc.sh
source $SCRIPT/utils/token.sh

failed() {
  [ -z "$signal" ] && return
  local reason=`encodeURIComponent "$1"`
  wget -T 5 -qO /dev/null $(getAuthHeader) "$CLOUD_ADDRESS/failed?type=template&reason=$reason"
}

success() {
  wget -T 5 -qO /dev/null $(getAuthHeader) "$CLOUD_ADDRESS/success?type=template"
}

echo "Updating template..."
###################################
#-- Step 1: Get new template config

configFile=`mktemp`
wget -T 5 -qO "$configFile" $(getAuthHeader) "$CLOUD_ADDRESS/template.conf"
if [ ! -s "$configFile" ] ; then
  echo "  get template config failed"
  rm -rf $configFile

  failed "get config"
  exit
fi

source "$configFile"
mv "$configFile" $CONFIG/template.conf

# reset
tmpDir=`mktemp -d` && cd "$tmpDir"

cleanup() {
  rm -rf "$tmpDir"

  local allUsingTemplate="default"
  for ssid in $ssids ; do
    eval templateId="\$TEMPLATE_SSID${ssid}"
    allUsingTemplate="$templateId $allUsingTemplate"
  done

  local allTemplate=`ls "$TEMPLATE"`
  for template in $allTemplate ; do
    [ "`echo $allUsingTemplate | grep $template`" ] || rm -rf "$TEMPLATE/$template"
  done
}

cleanupTmp() {
  rm -rf "$tmpDir/*"
}

##################################
#-- Step 2: Get new template
for ssid in $ssids ; do
  echo "ssid $ssid"

  eval templateId="\$TEMPLATE_SSID${ssid}"

  [ -z "$templateId" ] && echo "  No template config for ssid $ssid" && continue

  [ -s "$TEMPLATE/$templateId/version" ] && oldChecksum=`cat "$TEMPLATE/$templateId/version"`

  echo -n "  Checking new template..."
  wget -T 5 -qO template.tgz $(getAuthHeader) \
    "$CLOUD_ADDRESS/template/download?templateId=$templateId&checksum=$oldChecksum"

  ls -la
  [ ! -s template.tgz ] && echo "  up-to-date" && cleanupTmp && continue
  echo "  found"

  echo -n "  Extracting..."
  tar -zxvf template.tgz 2>/dev/null
  if [ $? -ne 0 ] ; then
    echo "failed"
    failed "extract"
    cleanupTmp
    continue
  fi
  echo "done"
  rm -rf template.tgz

  if [ ! -d "$templateId" ] ; then
    echo "  Not found folder $templateId"
    cleanupTmp
    continue
  fi

  if [ ! -s "$templateId/index.html" ] ; then
    echo "  Not found file index.html"
    failed "no index"
    cleanupTmp
    continue
  fi

  fsFree=`df -k | grep overlayfs | awk '{print $4}'`;
  templateSize=`du -sk $templateId | awk '{print $1}'`;
  newFsFree=$((fsFree - templateSize))

  if [ $newFsFree -lt 1024 ] ; then
    echo "  Disk is full"
    failed "disk full"
    cleanupTmp
    continue
  fi

  cp -rf "$templateId" "$TEMPLATE/$templateId.new"
  rm -rf "$TEMPLATE/$templateId"
  mv "$TEMPLATE/$templateId.new" "$TEMPLATE/$templateId"
  cleanupTmp

  success
done

cleanup

exit 0
