#!/bin/sh
#MOUNTPOINT="/mnt/floppy"
MD5_DIR=/usr/local/
#MD5_ETC=etc.md5
MD5_ROOT=root.md5

#echo "Trying to mount /dev/fd0 on ${MOUNTPOINT}..."
#mount -t msdos /dev/fd0 ${MOUNTPOINT}
#if [ $? = 0 ]; then
#    echo "Ok."
#else
#    echo "failed. Exiting..."
#    exit 1
#fi;

MEDIA_SET="NO"

NFS_HOST_TMP=`/usr/bin/mktemp -t nfs_host`
NFS_MOUNTPOINT_TMP=`/usr/bin/mktemp -t nfs_mountpoint`
FTP_SETUP() {
    dialog --title "FreeSBIE liveCD - Choose your NFS server" \
    --inputbox "NFS Hostname or IP Address" 9 50 \
    "" 2> $NFS_HOST_TMP
    NFS_HOST=`cat $NFS_HOST_TMP`
    rm $NFS_HOST_TMP
    dialog --title "FreeSBIE liveCD - Choose your NFS mountpoint" \
    --inputbox "Mountpoint" 9 50 \
    "" 2> $NFS_MOUNTPOINT_TMP
    NFS_MOUNTPOINT_TMP=`cat $NFS_MOUNTPOINT_TMP`
    rm $NFS_MOUNTPOINT_TMP
}
        
FTP_HOST_TMP=`/usr/bin/mktemp -t ftp_host`
FTP_USER_TMP=`/usr/bin/mktemp -t ftp_user`
FTP_PASS_TMP=`/usr/bin/mktemp -t ftp_pass`
FTP_SETUP() {
    dialog --title "FreeSBIE liveCD - Choose your ftp server" \
    --inputbox "FTP Hostname or IP Address" 9 50 \
    "" 2> $FTP_HOST_TMP
    FTP_HOST=`cat $FTP_HOST_TMP`
    rm $FTP_HOST_TMP
    dialog --title "FreeSBIE liveCD - Choose your ftp username" \
    --inputbox "Username" 9 50 \
    "" 2> $FTP_USER_TMP
    FTP_USER=`cat $FTP_USER_TMP`
    rm $FTP_USER_TMP
    # ATTENTION
    # Inputbox is not a Passwordbox (the password entered is 
    # displayed!!!!)
    #
    dialog --title "FreeSBIE liveCD - Choose your ftp password" \
    --inputbox "Password" 9 50 \
    "" 2> $FTP_PASS_TMP
    FTP_PASS=`cat $FTP_PASS_TMP`
    rm $FTP_PASS_TMP
}

SMB_HOST_TMP=`/usr/bin/mktemp -t smb_host`
SMB_USER_TMP=`/usr/bin/mktemp -t smb_user`
SMB_PASS_TMP=`/usr/bin/mktemp -t smb_pass`
SMB_MOUNTPOINT_TMP=`/usr/bin/mktemp -t smb_mountpoint`

SMB_SETUP() {
    dialog --title "FreeSBIE liveCD - Choose your SMB server" \
    --inputbox "SMB Hostname or IP Address" 9 50 \
    "" 2> $SMB_HOST_TMP
    SMB_HOST=`cat $SMB_HOST_TMP`
    rm $SMB_HOST_TMP
    dialog --title "FreeSBIE liveCD - Choose your SMB username" \
    --inputbox "Username" 9 50 \
    "" 2> $SMB_USER_TMP
    SMB_USER=`cat $SMB_USER_TMP`
    rm $SMB_USER_TMP
    dialog --title "FreeSBIE liveCD - Choose your SMB password" \
    --inputbox "Password" 9 50 \
    "" 2> $SMB_PASS_TMP
    SMB_PASS=`cat $SMB_PASS_TMP`
    rm $SMB_PASS_TMP
    dialog --title "FreeSBIE liveCD - Choose your SMB username" \
    --inputbox "Username" 9 50 \
    "" 2> $SMB_USER_TMP
    SMB_MOUNTPOINT=`cat $SMB_MOUNTPOINT_TMP`
    rm $SMB_MOUNTPOINT_TMP
}

REMOTE_TMP=`/usr/bin/mktemp -t remote`
REMOTE_SETUP() {
    dialog --menu "FreeSBIE liveCD - Choose your protocol" 13 42 4 \
        A "FTP Server" \
        N "NFS Host" \
        S "SMB Host" \
        Q "Quit" \
    2> $REMOTE_TMP
    case `cat $REMOTE_TMP` in
        A)
            MEDIA_PROTO="FTP"
            FTP_SETUP
            ;;
        N)
            MEDIA_PROTO="NFS"
            NFS_SETUP
            ;;
        S)
            MEDIA_PROTO="SMB"
            SMB_SETUP
            ;;
        esac
        rm $REMOTE_TMP
}

SITE_TMP=`/usr/bin/mktemp -t site`
CHOOSE_MEDIA() {
    dialog --menu "FreeSBIE liveCD - Choose your site" 13 42 3 \
        A "Remote Site" \
        F "Local Site" \
        Q "Quit" \
    2> $SITE_TMP
    case `cat $SITE_TMP` in
        A)
            MEDIA_TYPE="REMOTE"
            REMOTE_SETUP
            ;;
        F)
            MEDIA_TYPE="LOCAL"
            LOCAL_SETUP
            ;;
        esac
        rm $SITE_TMP
        MEDIA_SET="YES"
}

MENU_TMP=`/usr/bin/mktemp -t menu`
menu() {
    dialog --menu "FreeSBIE Media Backup" 13 42 3 \
        M "Choose media type" \
        R "Backup /root" \
        Q "Quit" \
    2> $MENU_TMP
    case `cat $MENU_TMP` in
        M)
        CHOOSE_MEDIA
        menu
        ;;
        R)
        if [ "$MEDIA_SET" = "NO" ]; then
            dialog --msgbox "First you have to select a media." 9 50 
            menu     
        fi

        cd /
        [ -e tmp/root_files.tmp ] && rm tmp/root_files.tmp
        touch tmp/root_files.tmp
        case $MEDIA_TYPE in
            REMOTE)
            echo -n "Finding data to transfer... "
            for i in `find root -type f`
            do
                j=`md5 $i | awk '{print substr($0,5,length($0))}'`
                RESULT=`grep "${j}" $MD5_DIR/$MD5_ROOT`
                if [ ! "${RESULT}" = "${j}" ]; then
                    echo "${j}" | awk -F '=' '{print substr($1,2,length($1)-3)}'>> /tmp/root_files.tmp
                fi
            done
            echo "[OK]"
            case $MEDIA_PROTO in
                FTP)
                echo -n "Compressing data to transfer... "
                tar jcvpf /tmp/root.tar.bz2 -T /tmp/root_files.tmp > /dev/null 2>&1
                echo "[OK]"
                ftp -A -u ftp://$FTP_USER:$FTP_PASS@$FTP_HOST/freesbie/root.tar.bz2 /tmp/root.tar.bz2 #> /dev/null 2>&1
                if [ "$?" != "255" ]; then
                    dialog --msgbox "Error while transferring data." 9 50 
                else
                    dialog --msgbox "Data transferred successfull." 9 50 
                fi
                exit
                ;;
            esac
            ;;
            LOCAL)
            tar jcvpf ${MOUNTPOINT}/root.tar.bz2 -T /tmp/root_files.tmp
            rm tmp/root_files.tmp
            ;;
        esac
        ;;

        Q)
        umount ${MOUNTPOINT}
        exit 0       
        ;;

        *)
        exit 0
    esac
    #rm $MENU_TMP
}
menu
#umount ${MOUNTPOINT}
