#!/bin/sh
# ***********************************************************
#              Backupscript nach Methode niovs
#              wird vom Addon-Manager aufgerufen
#
#           kann beliebig für jede Box weitert werden
#
#           angepasst fuer Fortis-Boxen by Bauernbub
# ***********************************************************

boxtyp="`cat /proc/stb/info/model`"

# check for saved settings
if [ ! -e /var/etc/restore.cfg ]; then
  # make a default and say all yes
  settings=n
  binarys=n
  camconfig=n
  plugins=n
  network=n
  samba=n
  bilder=n
  sonstige=n
  addonmanager=y
  backup=y
  cp /usr/lib/enigma2/python/Plugins/Extensions/AddonManager/restore.sh /var/etc/
else
  . /var/etc/restore.cfg   # read config from Addon-Manager
fi

if [ "$boxtyp" == "dm8000" ]
  then
    BOXDIR="backup_vu"
    echo -e "***********************************************************"
    echo -e "**************Sicherung VU Plus oder Dreambox**************"
    echo -e "***********************************************************\n"
  else
    BOXDIR="backup_fortis"
    echo -e "***********************************************************"
    echo -e "**************Sicherung Fortis Box gestartet **************"
    echo -e "***********************************************************\n"
fi

echo -e "\nLege Ordner und die Sicherungsdatei an"

if [ `mount | grep "on /var/swap" | wc -l` -ge 1 ]; then DIR="/var/swap/$BOXDIR"; fi
if [ `mount | grep "on /media/hdd" | wc -l` -eq 1 ]; then DIR="/media/hdd/$BOXDIR"; fi



# make a backup copy
if [ -e "$DIR" ]; then
  echo -e "\nLege eine Sicherungsdatei des alten Backup an"
  oldtime=`date +%d.%m_%H.%M`
  echo "---> $DIR"_"$oldtime"
  mv  "$DIR" "$DIR"_"$oldtime"
fi

mkdir -p $DIR/
rm -f $DIR/backup.log ; touch $DIR/backup.log

if [ "$boxtyp" == "dm8000" ]
 then
 if [ "$settings" == "y" ]
	then
		tar -cvf $DIR/settings.tar /etc/enigma2/* /etc/tuxbox/* >> $DIR/backup.log
		echo -e "******** Settings gesichert ********\n"
	else
	echo "******** Settings NICHT gesichert ********\n"
 fi

 if [ "$camconfig" == "y" ]
  then
   tar -cvf $DIR/camconfig.tar /var/keys/* /usr/scce/* /etc/CC* /usr/scam/* >> $DIR/backup.log
   echo -e "******** Camconfig gesichert ********\n"
  else
   echo "******** Camconfig NICHT gesichert ********\n"
 fi

 if [ "$plugins" == "y" ]
  then
		ipkg list_installed |grep enigma2-plugin > /tmp/ipkg.list
		cut -f 1 -d " " /tmp/ipkg.list > $DIR/ipkg.list
		echo -e "******** Plugins gesichert ********\n"
  else
    echo -e "******** Plugins NICHT gesichert ********\n"
 fi

 if [ "$network" == "y" ]
  then
 		tar -cvf $DIR/network.tar /etc/hostname /etc/resolv.conf /etc/wpa_supplicant.conf /etc/network/interfaces >> $DIR/backup.log
		echo -e "******** Netzwerkkonfiguration gesichert ********\n"
  else
    echo -e "******** Netzwerkkonfiguration NICHT gesichert ********\n"
 fi

 if [ "$samba" == "y" ]
  then
    tar -cvf $DIR/samba.tar /etc/samba/smb.conf >> $DIR/backup.log
		echo -e "******** Sambakonfiguration gesichert ********\n"
  else
    echo -e "******** Sambakonfiguration NICHT gesichert ********\n"
 fi

 if [ "$bilder" == "y" ]
  then
    tar -cvf $DIR/bilder.tar /usr/share/enigma2/*.mvi /usr/share/*.mvi >> $DIR/backup.log
		echo -e "******** Bootbilder und Radiobild gesichert ********\n"
  else
    echo -e "******** Bootbilder und Radiobild NICHT gesichert ********\n"
 fi

 if [ "$sonstige" == "y" ]
  then
		if [ -e /etc/inadyn.conf ]
    	then
       tar -cvf $DIR/sonstige.tar /etc/fstab /etc/localtime /etc/inadyn.conf /etc/videomode /etc/ntpserver /etc/passwd>> $DIR/backup.log
		   echo -e "******** sonstige Konfigurationsdateien gesichert inkl. DynDNS-Config ********\n"
	 	  else
       tar -cvf $DIR/sonstige.tar /etc/fstab /etc/localtime /etc/videomode /etc/ntpserver /etc/passwd>> $DIR/backup.
       echo -e "******** sonstige Konfigurationsdateien gesichert ********\n"
    fi
     else
       echo -e "******** sonstige Konfigurationsdateien NICHT gesichert ********\n"
 fi


else  # Boxtyp is not dm8000 (vubox)
 if [ "$settings" == "y" ]
	then
		tar -cvf $DIR/settings.tar /usr/local/share/enigma2/* /var/etc/enigma2/* /etc/tuxbox/* /var/etc/blueKey >> $DIR/backup.log
		echo -e "******** Settings gesichert ********\n"
	else
	echo "******** Settings NICHT gesichert ********\n"
 fi

 if [ "$camconfig" == "y" ]
  then
   tar -cvf $DIR/camconfig.tar /var/emu/* /var/keys/* /var/etc/*emu /var/etc/active_cam >> $DIR/backup.log
   echo -e "******** Camconfig gesichert ********\n"
  else
   echo "******** Camconfig NICHT gesichert ********\n"
 fi

 if [ "$binarys" == "y" ]
  then
   tar -cvf $DIR/bin.tar /var/bin/* >> $DIR/backup.log
   echo -e "******** Binarys gesichert ********\n"
  else
   echo "******** Binarys NICHT gesichert ********\n"
 fi

 if [ "$plugins" == "y" ]
  then
		ipkg list_installed |grep enigma2-plugin > /tmp/ipkg.list
		cut -f 1 -d " " /tmp/ipkg.list > $DIR/ipkg.list

    # check ob Referenzliste existiert
    if [ ! -e /usr/lib/enigma2/python/Plugins/Extensions/Aafpanel/dirlist ]; then
        echo "Aafpanel
        BoardViewer
        DVDPlayer
        GraphMultiEPG
        IMDb
        MediaCenter
        OSDAdjustment
        PlugLoad
        SimpleBitrate
        SocketMMI
        SplitScreen
        VFD_Brightness
        VirtualZap
        Volume_adjust
        WebInterface" >/usr/lib/enigma2/python/Plugins/Extensions/Aafpanel/dirlist
 		    #wget -O /usr/lib/enigma2/python/Plugins/Extensions/Aafpanel/dirlist http://kati910.aaf-board.info/dbase/E2/online/dirlist
 		    #chmod 755 /usr/lib/enigma2/python/Plugins/Extensions/Aafpanel/dirlist
    fi

    # welche Plugins wurden zusaetzlich installiert?
    cd /usr/lib/enigma2/python/Plugins/Extensions

    # welche sind jetzt installiert?
    ls -A1 >/usr/lib/enigma2/python/Plugins/Extensions/AddonManager/curlist
    cd /usr/lib/enigma2/python/Plugins/Extensions/AddonManager

    # vergleiche Standartplugins mit installierten
    diff dirlist curlist >difflist
    grep "^+[^+_]" difflist | sed ' s/+//g' >difflist2

    list="/usr/lib/enigma2/python/Plugins/Extensions/AddonManager/difflist2"
    dir2="/usr/lib/enigma2/python/Plugins/Extensions/"

    string="$DIR/plugins.tar"
    for i in $(cat $list); do
      string=$string" "$dir2$i
    done
    tar -cvf $string >> $DIR/backup.log
 		tar -cvf $string /var/etc/uninst/*  >> $DIR/backup.log

    rm /usr/lib/enigma2/python/Plugins/Extensions/AddonManager/difflist*
    rm /usr/lib/enigma2/python/Plugins/Extensions/AddonManager/curlist
		echo -e "******** Plugins gesichert ********\n"
  else
    echo -e "******** Plugins NICHT gesichert ********\n"
 fi

 if [ "$network" == "y" ]
  then
 		tar -cvf $DIR/network.tar /var/etc/automount/* /var/etc/hostname /var/etc/hosts /var/etc/resolv.conf /var/etc/default_gw /var/etc/wpa_supplicant.conf /var/etc/network/interfaces >> $DIR/backup.log
		echo -e "******** Netzwerkkonfiguration gesichert ********\n"
  else
    echo -e "******** Netzwerkkonfiguration NICHT gesichert ********\n"
 fi

 if [ "$samba" == "y" ]
  then
    tar -cvf $DIR/samba.tar /var/etc/smb.conf >> $DIR/backup.log
		echo -e "******** Sambakonfiguration gesichert ********\n"
  else
    echo -e "******** Sambakonfiguration NICHT gesichert ********\n"
 fi

if [ "$addonmanager" == "y" ]
	then
     	tar -cvf $DIR/am.tar /usr/lib/enigma2/python/Plugins/Extensions/AddonManager/* >> $DIR/backup.log
			echo -e "******** AddonManager wurde gesichert ********\n"
  else
	  	echo -e "******** AddonManager NICHT vorhanden! ********\n"
fi

 if [ "$sonstige" == "y" ]
  then
		if [ -e /var/etc/inadyn.conf ]
    	then
       tar -cvf $DIR/sonstige.tar /var/etc/fstab /var/etc/localtime /var/etc/inadyn.conf /var/etc/videomode /var/etc/ntpserver /var/etc/passwd>> $DIR/backup.log
		   echo -e "******** sonstige Konfigurationsdateien gesichert inkl. DynDNS-Config ********\n"
	 	  else
       tar -cvf $DIR/sonstige.tar /var/etc/fstab /var/etc/localtime /var/etc/videomode /var/etc/ntpserver /var/etc/passwd>> $DIR/backup.
       echo -e "******** sonstige Konfigurationsdateien gesichert ********\n"
    fi
     else
       echo -e "******** sonstige Konfigurationsdateien NICHT gesichert ********\n"
 fi
fi

# for automatic restore after flashing
cp /usr/lib/enigma2/python/Plugins/Extensions/AddonManager/restore.sh /var/etc/restore.sh
cp /var/etc/restore.cfg $DIR/restore.cfg
cp /usr/lib/enigma2/python/Plugins/Extensions/AddonManager/restore.sh $DIR/restore.sh

exit 0
