#!/bin/bash
# simple filelocking and /tmp race avoid
# keep it all in users home dir.

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH

    



if [ ! -d $HOME/tmp ]; then
 mkdir $HOME/tmp
fi
touch $HOME/tmp/`whoami`
export ANSFILE=$HOME/tmp/`whoami`
export READFILE=$HOME/tmp/.about.$$
export BACKTITLE="Euro 3G"
while true ; do
cd /opt/euro-beta/config_tools

#allow using explora gold's  quasi-keybord
#loadkeys gold_univ

# This is the main dialog menu
dialog  --nocancel --begin 3 4 --backtitle "$BACKTITLE" \
        --title "Procedure per l'assistenza tecnica" \
        --menu "\nScegliere la procedura da eseguire:" 21 72 14 \
"SpinKey" "Aggiornamento della licenza d'uso (SpinKey)" \
"E3GOpt"  "Modalita' di avvio del Euro3G" \
"Fine" "Riavvio del sistema" \
"ClrAcct" "Cancella conti aperti" \
"ClrStat" "Cancela TUTTE le statistiche + TUTTI conti" \
"ClrPLU"  "Cancella magazzino articoli" \
"ClrPrn"  "Cancella le stampe sospese" \
"ClrInv"  "Cancella fatture archiviate" \
"Backup"  "Salvataggio del DB del sistema" \
"Restore" "Ripristino del DB del sistema" \
"HwConf"  "Setup HW preconfigurato" \
"ConnVPN" "Connesione tramite VPN" 2>$ANSFILE
# now the routines
ANS=`cat $ANSFILE`
if [ "$ANS" = "Redetct" ]; then
  dialog --backtitle "$BACKTITLE" --title "Richiesta di conferma" \
         --yesno "Verra' forzata la autoconfigurazione del hardware.\nIl sistema viene riavviato.\n\nSiete sicuri di voler procedere ?" 10 70
  if [ $? -eq 0 ]
  then
     echo Redetect >/opt/euro-beta/hw_config
     reboot
  fi
fi
if [ "$ANS" = "Default" ]; then
 ./preconf_config
fi
if [ "$ANS" = "PersCnf" ]; then
 ./custom_config 
fi
if [ "$ANS" = "LogFile" ]; then
 ./check_logfile 
fi

if [ "$ANS" == "ClrAcct" ]; then
  CURDIR=`pwd`;
  cd /opt/euro-beta
  ./systools/clear_accounts eurotest
  cd $CURDIR 
fi

if [ "$ANS" == "ClrStat" ]; then
  CURDIR=`pwd`;
  cd /opt/euro-beta
  ./systools/clear_statistics eurotest
  cd $CURDIR 
fi

if [ "$ANS" == "ClrPLU" ]; then
  CURDIR=`pwd`;
  cd /opt/euro-beta
  ./systools/clear_store eurotest
  cd $CURDIR 
fi

if [ "$ANS" == "ClrPrn" ]; then
  CURDIR=`pwd`;
  cd /opt/euro-beta
  ./systools/clear_print eurotest
  cd $CURDIR 
fi

if [ "$ANS" == "ClrInv" ]; then
  CURDIR=`pwd`;
  cd /opt/euro-beta
  ./systools/clear_invoices eurotest
  cd $CURDIR 
fi

if [ "$ANS" == "Auto3G" ]; then
  mkdir -p /mnt/target_hd > /dev/null 2>&1
  mount /dev/hda1 /mnt/target_hd -t vfat -n > /dev/null 2>&1
  if [ -e /mnt/target_hd/rootfs.tgz ] ; then
    dialog --backtitle "$BACKTITLE" --title "Richiesta di conferma" --yesno "Questa procedura permettera di reinstallare il sistema dalla partizione predisposta dal produttore.\n\n\n      ! ! !  A T T E N Z I O N E  ! ! ! \n\n\nTutti i dati della installazione attuale andranno persi.\n\nUtilizzare solo per generare una nuova installazione con tutte le impostazioni del produttore.\n\n Siete sicuri di voler procedere ?" 18 64
    if [ $? -eq 0 ] ; then
      grub --batch < autoinst_grub_batch > /dev/null 2>&1
      dialog --backtitle "$BACKTITLE" --title "Autoinstallazione dal HD" --msgbox "Il disco e' stato configurato per l'autoinstallazione. Riavviare il sistema per avviare la procedura." 0 0
    fi
  else
    dialog --backtitle "$BACKTITLE" --title "Autoinstallazione dal HD" --msgbox "Il disco non e' predisposto per questo tipo di installazione" 0 0        
  fi    	
  umount /dev/hda1 -n > /dev/null 2>&1
fi

if [ "$ANS" == "Upgrade" ]; then
  DONE=0
  while [ $DONE -eq 0 ]
  do
#    mount /dev/backup /mnt/usb_hd >/dev/null 2>&1
    RPMFILE=`dialog --stdout --fselect "/mnt/backup/" 14 75` 
    if [ $? == 0 ] ; then
      DONE=1
      VALIDFILE=`echo -n $RPMFILE | grep "^/mnt/.*$" | grep -v "\./"`
      if [ "$RPMFILE" == "$VALIDFILE" ] ; then
        dialog --backtitle "$BACKTITLE" --title "Richiesta di conferma" --yesno "Installare il file $RPMFILE?" 8 64
        if [ $? -eq 0 ] ; then
    
          dialog --backtitle "$BACKTITLE" --title "Installazione aggiornamenti" --msgbox "La installazione degli aggiornamenti proseguira' in modalita' 'silenziosa'. Al termine della installazione viene terminata la sessione di lavoro e sara' necessario effettuare un nuovo login per continuare" 24 64
          /bin/rpm -i --nodeps --force $RPMFILE > /dev/null 2>&1
	  RESULT=$?
          umount /mnt/usb_hd 2>/dev/null
	  if [ $RESULT -eq 0 ] ; then exit 0; fi
          dialog --backtitle "$BACKTITLE" --title "Installazione aggiornamenti" --msgbox "ERRORE durante la installazione" 8 64
	else
	  DONE=0  
	fi
      fi
      umount /mnt/usb_hd 2>/dev/null
    fi    	
  done    
fi
if [ "$ANS" == "Backup" ]; then
#  DBFILE=`dialog --stdout --fselect "/mnt/" 14 50` 
#  mount /dev/backup /mnt/usb_hd >/dev/null 2>&1
#  mount | grep -q /mnt/usb_hd
#  if [ $? == 0 ] ; then
  if [ -e /mnt/backup ] && [ -h /mnt/backup ] ; then
#    VALIDFILE=`echo -n $DBFILE | grep "^/mnt/.*$" | grep -v "\./"`
    DBPATH=/mnt/backup
#    if [ "$DBFILE" == "$VALIDFILE" ] ; then
        CURDIR=`pwd`;
	cd /opt/euro-beta
	./systools/backup_db eurotest
	/bin/cp eurotest.sql.gz $DBPATH
	/bin/cp eurotest.sql.gz $DBPATH/`systools/get_system_name`.sql.gz
	cd $CURDIR 
#	umount /mnt/usb_hd
        dialog --backtitle "$BACKTITLE" --title "Backup" --msgbox "Salvataggio terminato con sucesso" 8 64
#    fi
  else
        dialog --backtitle "$BACKTITLE" --title "Backup" --msgbox "ERRORE - pen drive non trovato" 8 64

  fi    	
fi
if [ "$ANS" == "Restore" ]; then
  DONE=0
  while [ $DONE -eq 0 ] 
  do
    DONE=1
#    mount /dev/backup /mnt/usb_hd >/dev/null 2>&1
    DBFILE=`dialog --stdout --fselect "/mnt/backup/" 14 75` 
    if [ $? == 0 ] ; then
      VALIDFILE=`echo -n $DBFILE | grep "^/mnt/.*$" | grep -v "\./"`
      if [ -f $DBFILE -a "$DBFILE" == "$VALIDFILE" ] ; then
        CURDIR=`pwd`;
	cd /opt/euro-beta
	/bin/cp $DBFILE eurotest.sql.gz
	./systools/restore_db eurotest
	cd $CURDIR 
        dialog --backtitle "$BACKTITLE" --title "Restore" --msgbox "Ripristino terminato con successo" 8 64
      else
        dialog --backtitle "$BACKTITLE" --title "Restore" --msgbox "ERRORE - file non valido" 8 64
	DONE=0
      fi
    fi    	
#    umount /mnt/usb_hd >/dev/null 2>&1
  done    
fi
if [ "$ANS" = "Fine" ]; then
  /sbin/reboot
# poweroff
  exit 0
fi
if [ "$ANS" = "RevNuke" ]; then
 ./prep_spare_part
fi
if [ "$ANS" = "Nuke" ]; then
 ./nuke_euro3g_usb
fi
if [ "$ANS" = "PenForm" ]; then
 ./format_usb_storage
fi
if [ "$ANS" = "E3GOpt" ]; then
 ./e3g_startup_options
fi
if [ "$ANS" = "SpinKey" ]; then
 ./upgrade_spinkey
fi
if [ "$ANS" = "HwConf" ]; then
  CURDIR=`pwd`;
  cd /opt/euro-beta
  ./config_tools/e3g_hwconfig
  cd $CURDIR 
fi
if [ "$ANS" == "ConnVPN" ]; then
  CURDIR=`pwd`;
  cd /opt/euro-beta
  ./config_tools/vpn_connect
#  ./systools/remote_server_connect
#  read -p "Premere tasto ENTER per continuare"
  cd $CURDIR 
fi

done


