#!/bin/sh

MayIDoThis() {
    local description question command res
    description=$1
    question=$2
    command=$3
    default=$4
    res=0

    ask-me-a-question "$question" yn $default
    if [ "$?" -eq "1" ] ; then
        LogIt "$description" 1
        $command
        res=$?
    else
        LogIt "Skipping it." 1
    fi
    if [ "$res" -ne "0" ] ; then
        LogIt "Warning - error returned by '$command'" 1
    fi
    return $res
}


# ------------------------------ main -----------------------------------

LogIt "normal-mode --- starting"


ask-me-a-question "Do you want to edit the mountlist (y/N) ?" "yn" "n"
keepgoing=$?
while [ "$keepgoing" -ne "2" ] ; do
    echo "Do it :)" > /tmp/CALL-HACK-ME
    which pico &> /dev/null
    if [ "$?" -eq "0" ] ; then
        pico /tmp/mountlist.txt
    else
        vi /tmp/mountlist.txt
    fi
    ask-me-a-question "Do you want to edit it again (y/N) ?" "yn" "n"
    keepgoing=$?
done

MayIDoThis "Mondo-restore is now prepping the hard disks." \
"May I format and partition the disks (y/N) ?" prep-me n
if [ "$?" -ne "0" ] ; then
    echo "Mondo-restore is aborting."
    exit 1
fi

MayIDoThis "Mondo-restore is now mounting the partitions." \
"May I mount the partitions (Y/n) ?" mount-me y
if [ "$?" -ne "0" ] ; then
    echo "Mondo-restore is aborting."
    exit 2
fi

if [ -e "/mnt/cdrom/archives/NO-TARBALLS-HERE" ] ; then
    LogIt "No tarballs on CD, so I shan't try to restore data." 1
else
    MayIDoThis "Mondo-restore is now restoring all the data." \
    "May I restore all the data (Y/n) ?" restore-me y
fi

if [ -e "/tmp/CALL-HACK-ME" ] ; then
    LogIt "/tmp/CALL-HACK-ME found (presumably put there by normal-mode)"
    MayIDoThis "Mondo-restore is now amending fstab and lilo.conf" \
    "May I amend fstab and lilo.conf (Y/n) ?" stablilo-me y
fi

mkdir -p -m 1777 /mnt/RESTORING/tmp

MayIDoThis "Mondo-restore is now unmounting the partitions." \
"May I unmount the partitions (Y/n) ?" unmount-me y

label-partitions-as-necessary /tmp/mountlist.txt < /tmp/fstab

LogIt "Syncing." 2
sync
sync
sync
#LogIt "If you want to do clever stuff, re-insert the Mondo CD." 1
#eject /dev/cdrom
exit 0
