#!/bin/bash
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin/X11:/usr/games:/usr/local/bin:/root/bin

TITLE="Euro3G - Aggiornamenti"

cd /opt/euro-beta
unset LD_LIBRARY_PATH
unset LD_PRELOAD

if `uname -a | grep -qi ubuntu`
then
  PKG_TYPE=".deb"
else
  PKG_TYPE=".rpm"
fi

MSG_NEWLINE="\n"
GUI_MODE=0
if [ -n "$DISPLAY" ]
then
  GUI_MODE=1
fi 
 
if echo "$*" | grep -q "\-display"
then
  GUI_MODE=1
#  export DISPLAY=:0
fi  

source /opt/euro-beta/set3g_env
#export DISPLAY=:0

if [ ${GUI_MODE} -eq 1 ]
then
  if [ -x ./e3g_upgrade ] 
  then
    ./e3g_upgrade
    exit
  fi
  source /opt/euro-beta/set3g_env
  if ! which sysquery >/dev/null 2>&1 ;
  then
    PATH=$PATH:/opt/euro-beta
  fi
  MSG_NEWLINE="<br>"
fi

ERR_MSG="Verifica degli aggiornamenti in corso.${MSG_NEWLINE}Attendere ......" 
if [ ${GUI_MODE} -eq 1 ]
then
  sysmsg "$TITLE" "<b><pre>${ERR_MSG}</pre></b>" & 
else
  echo -e ${ERR_MSG}
fi


SITE_READY=0
if wget -t1 -T3 -q -O /tmp/e3g_update_list.html http://intranet.spintec.com/download/euro3g/euro3g_updates.php 2>/dev/null 
then
  SITE_READY=1
fi

if [ ${SITE_READY} -ne 1 ]
then
  ERR_MSG="Impossibile verificare aggiornamenti.${MSG_NEWLINE}Collegamento a internet non disponibile"
  if [ ${GUI_MODE} -eq 1 ]
  then
    killall sysmsg
    sysmsg "$TITLE" "<b><pre>${ERR_MSG}</pre></b>" & 
  else
    echo -e ${ERR_MSG}
  fi
  exit 2
fi      

CUR_SEC=`date '+%s'`
MNT_ISO_DATE=`./systools/get_eurodb_config_val MaintenanceExpireDate`
#MNT_ISO_DATE="2017-02-01"
MNT_DATE="${MNT_ISO_DATE:8:2}/${MNT_ISO_DATE:5:2}/${MNT_ISO_DATE:0:4}"
MNT_SEC=`date -d$MNT_ISO_DATE +%s`
DIF_DAYS=$(( ($MNT_SEC-$CUR_SEC)/86400 ))
#CUR_VER=`./systools/get_eurodb_config_val Euro3GLastUpdate | cut -d' ' -f3 | cut -d'<' -f1`
CUR_VER=`cat /opt/euro-beta/last_update.info | cut -d' ' -f3 | cut -d'<' -f1`
#CUR_VER="15.12-00"

for UPDATE in `w3m -dump http://intranet.spintec.com/download/euro3g/euro3g_updates.php | grep 'File:' | grep -i euro | grep $PKG_TYPE | awk '{ print $2"*"$5}'`
do
  PKG_LINK=`echo $UPDATE | cut -d'*' -f1`
  PKG_DATE=`echo $UPDATE | cut -d'*' -f2`
  PKG_ISO_DATE="${PKG_DATE:6:4}-${PKG_DATE:3:2}-${PKG_DATE:0:2}"
  PKG_SEC=`date -d$PKG_ISO_DATE +%s`
  DIF_DAYS=$(( ($MNT_SEC-$PKG_SEC)/86400 ))
#  echo "$PKG_LINK - $PGK_DATE - $DIF_DAYS"
  LAST_LINK=$PKG_LINK
  LAST_VER=`echo ${PKG_LINK:0:${#PKG_LINK}-9} | sed 's/euro3g-bp/Euro3GUpdate/' | cut -d'-' -f2-`
  LAST_VER_DATE=$PKG_DATE
  if [ $DIF_DAYS -gt 0 ]
  then
    INST_LINK=$LAST_LINK
    INST_VER=$LAST_VER 
    INST_VER_DATE=$PKG_DATE
  fi
done 
#echo "Versione installata:   $CUR_VER"
#echo "Scadenza manutenzione: $MNT_DATE" 
#echo "Ultima versione:       $LAST_VER del $LAST_VER_DATE"
#echo "Versione installabile: $INST_VER del $INST_VER_DATE"
SUMMARY="Versione installata:   ${CUR_VER}${MSG_NEWLINE}Scadenza manutenzione: ${MNT_DATE}${MSG_NEWLINE}Ultima versione:       ${LAST_VER} del ${LAST_VER_DATE}${MSG_NEWLINE}Versione installabile: ${INST_VER} del ${INST_VER_DATE}${MSG_NEWLINE}"


DOWNLOAD=0 
UPGRADE=0

rm -f /tmp/e3g_last_update_link > /dev/null 2>&1
if [ "$INST_VER" ">" "$CUR_VER" -o "$INST_VER" "=" "$CUR_VER" ]
then
  echo $INST_LINK > /tmp/e3g_last_update_link
fi
if [ ${GUI_MODE} -eq 1 ]
then 
  killall sysmsg
  if [ -f /tmp/e3g_last_update_link ]
  then 
#    sysquery "$TITLE" "<b><pre>${SUMMARY}${MSG_NEWLINE}${MSG_NEWLINE}Volete scaricare aggioramento $INST_VER su PenDrive USB ?</pre></b>" "Si, procedere" "No, non fare niente"
    sysquery "$TITLE" "<b><pre>${SUMMARY}${MSG_NEWLINE}${MSG_NEWLINE}Volete installare aggioramento $INST_VER ?</pre></b>" "Si, procedere" "No, non fare niente"
    if [ $? == 0 ]
    then
      DOWNLOAD=1
    fi 
  else
    sysmsg "$TITLE" "<b><pre>${SUMMARY}</pre></b>" 
  fi
else
  echo -e $SUMMARY
  if [ -f /tmp/e3g_last_update_link ]
  then
    echo -n "Procedere con aggiornamento ? [S/N] "
    read ANSW
    if [ ${ANSW} == "Y" -o ${ANSW} == "y" -o  ${ANSW} == "S" -o  ${ANSW} == "s" ]
    then
      DOWNLOAD=1
      UPGRADE=1
    fi
  fi
fi
if [ $DOWNLOAD -eq 1 ]
then
#  if [ ${GUI_MODE} -eq 1 ]
#  then 
#    if `uname -a | grep -qiv ubuntu`
#    then
#      mkdir -p /mnt/backup >/dev/null 2>&1
#      mount /dev/backup /mnt/backup
#      USB_MOUNTPOINT="/mnt/backup"
#    else
#      USB_MOUNTPOINT=`readlink /mnt/backup`
#      mount ${USB_MOUNTPOINT} -o async,remount
#    fi
#    if mount | grep -q ${USB_MOUNTPOINT} 
#    then 
#      echo "Pendrive mounted on /mnt/backup"
#    else  
#      sysmsg "$TITLE" "<b><pre>Impossibile salvare aggiornamento su PenDrive USB</pre></b>"
#      exit 1      
#    fi
#  fi

  PKG_NAME=`cat /tmp/e3g_last_update_link`
  PKG_LINK=`cat /tmp/e3g_update_list.html | tr ' ' '\n' | grep $PKG_NAME | grep href | cut -d"'" -f2`
  echo "Will download $PKG_LINK"
  rm -f /tmp/$PKG_NAME >/dev/null 2>&1

  if [ ${GUI_MODE} -eq 1 ]
  then
    if `uname -a | grep -qi ubuntu`
    then
      echo "#!/bin/bash" > /tmp/e3g_download_update
      echo "stdbuf -i0 -o0 -e0 curl --progress-bar -o /tmp/$PKG_NAME $PKG_LINK 2>&1 | stdbuf -i0 -o0 -e0 tr '\r' '\n'" >> /tmp/e3g_download_update
      chmod +x /tmp/e3g_download_update
      ./show_process_output -T "Scaricamento del aggiornamento $INST_VER" /tmp/e3g_download_update
    else
      xterm -T "Sacricamento del aggiornamento $INST_VER" -geometry 80x2 -e curl --progress-bar -o /tmp/$PKG_NAME $PKG_LINK
    fi
  else  
    wget -O /tmp/$PKG_NAME $PKG_LINK
  fi     
  if [ $UPGRADE -eq 1 ]
  then 
    if `uname -a | grep -qi ubuntu`
    then
      gdebi -n /tmp/$PKG_NAME
      echo `dpkg-query -W -f='${Version};${Package};${Maintainer}\n' | grep 'info@spintec.com' | grep 'euro3g' | cut -d';' -f1 | tr -d '\n'` > /opt/euro-beta/last_update.info
      ./systools/upgrade_db eurotest
    else
      rpm -ihv --nodeps --force /tmp/$PKG_NAME
      echo `rpm -q -g Application/Euro3G --last | grep Euro3GUpdate | head -1 | cut -d'-' -f 2-` > /opt/euro-beta/last_update.info
      systools/patch_menu_lst -a
    fi
    LAST_UPDATE=`cat /opt/euro-beta/last_update.info`
    systools/set_eurodb_config_val "Euro3GLastUpdate" string "$LAST_UPDATE"
    if [ ${GUI_MODE} -eq 1 ]
    then
      ./e3g_restart
    else
      if [ -f /opt/euro-beta/full_restart ]
      then
        rm /opt/euro-beta/full_restart
        echo -e "\n\n\n****  Full restart required ****\n\n\n"
      else
        if [ -f /opt/euro-beta/no_restart ]
        then
          rm /opt/euro-beta/no_restart
        else
          echo -e "\n\n\n****  Quick restart required ****\n\n\n"
        fi
      fi
      rm -f /opt/euro-beta/no_restart >/dev/null 2>&1
    fi
  else
    if [ ${GUI_MODE} -eq 1 ]
    then
      if `uname -a | grep -qi ubuntu`
      then
        rm /mnt/backup
        mkdir /mnt/backup
        cp /tmp/$PKG_NAME /mnt/backup  >/dev/null 2>&1
      fi 
      ./aggiornamento eurodb $PKG_NAME
      if `uname -a | grep -qi ubuntu`
      then
        rm /mnt/backup/$PKG_NAME
        rmdir /mnt/backup
      fi
#      killall sysmsg
#      sysmsg "$TITLE" "<b><pre>Copia del aggiornamento ${INST_VER} su PenDrive USB.${MSG_NEWLINE}Attendere ......</pre></b>" & 
#      cp -f /tmp/$PKG_NAME /mnt/backup  >/dev/null 2>&1
#      sync
#      if `uname -a | grep -qiv ubuntu`
#      then
#        umount /mnt/backup
#      else
#        mount ${USB_MOUNTPOINT} -o sync,remount
#      fi
#      killall sysmsg
#      sysmsg "$TITLE" "<b><pre>Aggiornamento $INST_VER salvato su PenDrive USB</pre></b>"
    fi
  fi
fi 

