#!/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
}

LOCAL_SETUP() {
    dialog --menu "FreeSBIE Local Backup" 13 42 3 \
        "*" "$DEV_STR" \
        Q "Quit" \
        2> /tmp/me
        exit
}

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() {
    DA_DEV=`mount | grep "usb." | cut -d\  -f 1 | cut -d\/ -f 3""`
    DEV_STR=`camcontrol devlist | grep da1 | cut -d\> -f 1 | cut -d\< -f 2`
    dialog --menu "FreeSBIE liveCD - Choose your site" 13 42 3 \
        A "Remote Site" \
        L "$DEV_STR" \
        Q "Quit" \
    2> $SITE_TMP
    case `cat $SITE_TMP` in
        A)
            MEDIA_TYPE="REMOTE"
            REMOTE_SETUP
            ;;
        L)
            MEDIA_TYPE="LOCAL"
            MOUNTPOINT="$DA_DEV"
            ;;
        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 "Restore /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

        case $MEDIA_TYPE in
            REMOTE)
            case $MEDIA_PROTO in
                FTP)
                PWD=`pwd`
                cd /tmp
		[ -e root.tar.bz2 ] && rm root.tar.bz2
                ftp -A ftp://$FTP_USER:$FTP_PASS@$FTP_HOST/freesbie/root.tar.bz2 > /dev/null 2>&1
                if [ "$?" != "0" ]; then
                    dialog --msgbox "Error while transferring data." 9 50 
                else
                    dialog --msgbox "Data transferred successfull." 9 50 
                fi
                tar jxvf root.tar.bz2 -C / > /dev/null 2>&1
                rm root.tar.bz2
                cd $PWD
                exit
                ;;
            esac
            ;;
            LOCAL)
            # USB Devices
            # Mounted with /sbin/umass_mount.sh
            #Retriving device name (e.g. da0)
            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}
