#!/bin/sh
# @vasm : vbackupsys
# @level: user
# @description: backupsys configurator
# 
# (c) Eko M. Budi, 2003
# (c) VLocity Linux, 2003
#
# Released under GNU GPL

vdir=$(dirname $0)

. $vdir/vasm-functions
check_root

## List of system files
sysfiles()
{
cat<<EOF
/etc/HOSTNAME:on:TCP/IP name of the computer
/etc/rc.d/rc.inet1:on:TCP/IP settings
/etc/rc.d/rc.inet:off:TCP/IP settings
/etc/rc.d/rc.route:on:TCP/IP Gateway and routing
/etc/resolv.conf:on:TCP/IP Domain Name server
/etc/hosts:on:TCP/IP static name mappings
/etc/modules.conf:on:Hardware modules
/etc/sysconfig/vlocity:on:Hardware information
/etc/sysconfig/kudzu:on:Hardware information
/etc/X11/Xorg.conf:on:X-Window settings
/etc/rc.d/rc.keymap:on:Keymap settings
/etc/hardwareclock:off:Hardware clock setting 
/etc/localtime:off:Time Zone setting
/etc/passwd:off:Users account
/etc/shadow:off:User password
/etc/group:off:Users group
/etc/vlocity-version:off:VLocity Linux version
/etc/sudoers:off:SUDO configuration
/etc/rc.d/rc.paranoid:off:Firewall script
/etc/rc.d/rc.firewall:off:Firewall script
/etc/rc.d/rc.local:off:Custom initialization
/etc/nsswitch.conf:off:Domain name switching policy
/etc/dnsmasq.conf:off:DNS-Masq configuration 
/etc/hosts.allow:off:List of allowed hosts
/etc/hosts.deny:off:List of banned hosts
/etc/hosts.equiv:off:List of equivalent hosts
/etc/securetty:off:Allowed TTYs for root
/etc/inetd.conf:off:Inet daemon configuration
/etc/ftpusers:off:List of FTP users
/etc/proftpd.conf:off:ProFTP daemon configuration
/etc/samba/smb.conf:off:SAMBA configuration
/etc/backup2l.conf:off:Backup configuration
/etc/backupsys.conf:off:System backup configuration
/etc/X11/XF86Config:on:X-Window settings
/etc/X11/XF86Config-4:on:X-Window settings
/etc/X11/xorg.conf:on:X-Window settings
/etc/XwmMenu.ini:off:XwmMenu settings
/etc/lilo.conf:off:Linux loader configuration
/etc/fstab:off:Mounted file system table
/etc/raidtab:off:RAID table
/etc/asound.state:on:ALSA settings
/dev/cdrom:on:CDROM symlink
/dev/mouse:on:Mouse symlink
/dev/modem:on:Modem symlink
EOF
}

set_cron()
{
    if cat /etc/backupsys.conf | \
       sed "s/^CRON_ACTIVATED=.*/CRON_ACTIVATED=$1/" > \
      /etc/backupsys.conf.new ; then
       mv -f /etc/backupsys.conf.new /etc/backupsys.conf
    fi
}

#########################################################################
menu_cron()
{

TITLE="BACKUPSYS CRON"
TEXT="\n
Backupsys can run daily with the help of cron daemon.\n
So, say ACTIVATE here, then turn on cron daemon using
vsrvmenu."
DIMENSION="12 58 2"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"ACTIVATE"  "let cron daemon runs backupsys each day" \
"DEACTIVATE" "stop automatic backupsys" \
2> $freply || return $?

   case `cat $freply` in
      ACTIVATE)
        set_cron 1
        ;;
      DEACTIVATE)
        set_cron 0
        ;;
    esac
}

#########################################################################
menu_backup()
{
TITLE="BACKUPSYS DO IT!"
TEXT="\n
So ... how do you want me to do it ?"
DIMENSION="10 46 2"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"UPDATE" "backup new files only" \
"BACKUP" "backup without comparing dates" \
2> $freply || return $?

case `cat $freply` in
      BACKUP)
	backupsys > $fmenu
        ;;
      UPDATE)
	backupsys --update > $fmenu
        ;;
  esac
  $DCMD --backtitle "$BACKTITLE" --title "BACKUPSYS LOG" \
        --textbox $fmenu 20 74 2>/dev/null
}

#########################################################################
build_vol_menu()
{
    . /etc/backupsys.conf
    if [ "$BACKUP_DEV" ]; then
	if ! mount | grep -qe "^$BACKUP_DEV "; then
	    mount $BACKUP_DEV $BACKUP_MNT -t $BACKUP_FS >/dev/null 2>&1
	    if [ $? = 0 ]; then
		msgbox "Cannot mount $BACKUP_DEV" "ERROR"
		return 1
	    fi
	fi
    fi
    if [ ! -d $BACKUP_MNT ]; then
	msgbox "Base directory $BACKUP_MNT is not exist" "ERROR"
	return 1
    fi
    BACKUP_BASE=$BACKUP_MNT/$BACKUP_DIR
    if ! ls $BACKUP_BASE/*.tar.bz2 &> /dev/null; then
	msgbox "No backup volume yet" "ERROR"
	return 1
    fi
    for BACKUP_FILE in $BACKUP_BASE/*.tar.bz2; do
	BDATE=`stat $BACKUP_FILE | grep -e '^Change:' | 
	       cut -f2- -d : | cut -f1 -d.`
	BACKUP_FILE=`basename $BACKUP_FILE`
	echo "'$BACKUP_FILE' 'Date =$BDATE' \\" >> $fmenu 
    done
}

menu_restore_vol()
{
TITLE="BACKUPSYS RESTORE"
TEXT="\n
This menu will restore one or more files from a backup volume.
The following backup volume(s) are available.\n
Which one do you want to use?"
DIMENSION="20 68 10"

echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
echo '--menu "$TEXT" $DIMENSION \' >> $fmenu 
build_vol_menu || return 0
echo '2> $freply' >> $fmenu

. $fmenu || return $?

BACKUP_FILE=`cat $freply`
}


check_file()
{
    STATUS="off"
    DESC=`basename $1`
    SLINE=`sysfiles | grep $DESC`
    if [ $? = 0 ]; then
	DESC=`echo $SLINE | cut -f3 -d:`
	STATUS=`echo $SLINE | cut -f2 -d:`
    fi
    if [ -z "$RESTORE_INSTALL" ]; then
	if echo $NEWER_FILES | grep -q `basename $1`; then
	    STATUS="on"
	else
	    STATUS="off"
	fi
    fi
}

build_file_menu()
{
    ## add newer files
    NEWER_FILES=`backupsys --diff -f $BACKUP_BASE/$BACKUP_FILE | cut -f1 -d:`
    backupsys --list -f $BACKUP_BASE/$BACKUP_FILE | while read LINE; do
	if [ ! -d /$LINE ]; then
	    check_file $LINE
	    echo "'$LINE' '$DESC' '$STATUS' \\" >> $fmenu
	fi
    done
}

menu_restore_files()
{
TITLE="BACKUPSYS RESTORE"
TEXT="\n
Volume $BACKUP_FILE contains the following files.\n
This operation could be fatal, if the restored files are wrong.\n
So, please choose which files to be restored wisely:"
DIMENSION="20 72 10"

echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu 
build_file_menu || return 0
echo '2> $freply' >> $fmenu

. $fmenu || return $?

FILELIST=`cat $freply`

backupsys --restore -f $BACKUP_BASE/$BACKUP_FILE $FILELIST &> $fmenu

$DCMD --backtitle "$BACKTITLE" --title "BACKUPSYS LOG" \
        --textbox $fmenu 20 74 2>/dev/null
}


#########################################################################
menu_main()
{

while [ 0 ]; do
TITLE="BACKUPSYS MENU"
TEXT="\n
Backupsys allows you to keep several copies of VLocity Linux
important system files. You can restore it later at anytime,
or when you have to reinstall it (hopefully not)."
DIMENSION="14 64 4"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"CRON" "activate or deactivate regular backup" \
"BACKUP" "do manual backup" \
"RESTORE" "do manual restore" \
"DONE" "enough for this time, thanks" \
2> $freply || return $?

   case `cat $freply` in
      CRON)
        menu_cron
        ;;
      BACKUP)
        menu_backup
        ;;
      RESTORE)
	wizard menu_restore_vol menu_restore_files
        ;;
      *)
        return 0
	;;
    esac
  done
}

####################################################################################
# MAIN PROGRAM
case $1 in
    --install)
	RESTORE_INSTALL=yes
	wizard menu_restore_vol menu_restore_files
	;;
    --restore|-r)
	wizard menu_restore_vol menu_restore_files
	;;
    --backup|-b)
	menu_backup
	;;
    --cron)
	menu_cron
	;;
    *)
	menu_main
	;;
esac
clean_exit $?

