#!/bin/bash

MOUNT_POINT="/mnt/backup"
VOKNO_NAME="Ripristino di configurazione di sistema"

REST_FILE="/tmp/restoring_sysconfig.tgz"

sysmsg="./sysmsg"

function restore
{
	if [ ! "$1" -o  ! -e "$1" ]
	then
	  echo Errore: archivio non trovato
	  exit 1
	fi

	if ! rm -f $REST_FILE  || ! GPGRES=`gpg --trust-model always --no-default-keyring --keyring .gnupg/euro3g_new_pubring.gpg --secret-keyring .gnupg/euro3g_new_secring.gpg -q -o $REST_FILE -d "$1" 2>&1` || ! echo $GPGRES | egrep -q "gpg: Signature .* using DSA key ID E8D21E79"
	then
	  echo Errore: archivio corrotto
	  exit 2
	fi
	cd /opt/euro-beta
	rm -f config_tools/*.disabled log_enabled
	tar xzf $REST_FILE
}

cd /opt/euro-beta
if [ "$1" = "-i" ]
then
#        mkdir $MOUNT_POINT
#        if ! mount /dev/backup $MOUNT_POINT
        if ! [ -e $MOUNT_POINT ] || ! [ -h $MOUNT_POINT ]
        then
           $sysmsg "$VOKNO_NAME" "Impossibile accedere al dispositivo di salvataggio (pendrive)"
           rmdir $MOUNT_POINT
           exit 1
        fi
        STATION=`./systools/get_eurodb_config_val "ModemSystemName"`
        FILENAME="${STATION}_system_config.e3g"
	if [ ! -e "$MOUNT_POINT/$FILENAME" ]
	then
                $sysmsg  "$VOKNO_NAME" "Errore: archivio di configurazione sistema ($STATION) non trovata"
        elif ! ERROR=`restore $MOUNT_POINT/$FILENAME`
        then
                $sysmsg  "$VOKNO_NAME" "$ERROR"
                RETVAL=2
        else
                $sysmsg  "$VOKNO_NAME" "Configurazione sistema ($STATION) ripristinata."
		./e3g_restart
        fi
#        umount $MOUNT_POINT
#        rmdir $MOUNT_POINT
        exit $RETVAL
else
        restore $*
fi

