#!/bin/sh




FdiskAndFormatPartition() {
    local incoming last_partno partname partno mountpt mounttype \
partsize destdrive devstub errors sendphrase partcode res ex_part
    incoming=$1
    last_partno=$2
    partname=`echo $incoming | cut -d' ' -f1`
    partno=`echo $partname | tr -s 'abcdeghsvz/' ' ' | cut -d' ' -f2`
    mountpt=`echo $incoming | cut -d' ' -f2`
    mounttype=`echo $incoming | cut -d' ' -f3`
# leave the 'K' on the end of the next line, please :-)
    partsize_int=`echo $incoming | cut -d' ' -f4`
    partsize="$partsize_int"K
    destdrive=`echo $partname | sed s/[0-9]// | sed s/[0-9]//`
    devstub=`basename $destdrive`

    LogIt "Creating $partno..." 2
    FdiskPartition $destdrive $partno $last_partno $partsize $partsize_int $mounttype
    res=$?
    if [ "$res" -ne "0" ] ; then
        LogIt "Failed to create $partno" 3
        errors=$(($errors+1))
    else
        LogIt "$partno created. Formatting..." 2
        FormatPartition $mounttype $destdrive$partno
	res=$?
        if [ "$res" -ne "0" ] ; then
            LogIt "Failed to format $partno" 3
            errors=$(($errors+1))
	else
            LogIt "$partno created and formatted ok        " 2
        fi
    fi
    return $errors
}




FdiskPartition() {
    local incoming last_partno partname partno mountpt mounttype \
partsize partsize_int destdrive devstub errors sendphrase partcode \
res ex_part
    destdrive=$1
    partno=$2
    last_partno=$3
    partsize=$4
    partsize_int=$5
    mounttype=$6
   
    errors=0
    sendphrase=""
    if [ "$partno" -le "4" ] ; then
	if [ "$partno" -eq "$last_partno" ] ; then
	    sendphrase=$sendphrase"n\np\n$partno\n\n\n"
	else
	    sendphrase=$sendphrase"n\np\n$partno\n\n+$partsize\n"
	fi
    else
	if [ "$partno" -eq "5" ] ; then
            ex_part=1
            while [ "$ex_part" -lt "5" ] \
&& [ "`fdisk -l $destdrive | grep $destdrive$ex_part`" != "" ] ; do
               ex_part=$(($ex_part+1))
            done
            if [ "$ex_part" -ge "5" ] ; then
                LogIt "You must leave at least one primary free," 1
                LogIt "or I can't allocate logicals." 1
                exit 1
            fi
	    sendphrase=$sendphrase"n\ne\n$ex_part\n\n\n"
	fi
	if [ "$partno" -eq "$last_partno" ] ; then
	    sendphrase=$sendphrase"n\nl\n\n\n"
 	else
	    sendphrase=$sendphrase"n\nl\n\n+$partsize\n"
	fi
    fi
    if [ "$mounttype" = "swap" ] ; then
	partcode=82
    elif [ "$mounttype" = "vfat" ] ; then
	if [ "$partsize_int" -ge "8192" ] ; then
	    partcode=c
	else
	    partcode=b
	fi
    elif [ "$mounttype" = "ext2" ] || [ "$mounttype" = "reiserfs" ] || [ "$mounttype" = "ext3" ] || [ "$mounttype" = "xfs" ] ; then
	partcode=83
    else
	partcode=83                 ; # default
    fi
    if [ "$partcode" != "" ] ; then
	sendphrase=$sendphrase"t\n$partno\n$partcode\n"
    fi

    sendphrase=$sendphrase"w\n"
    LogIt "I am about to send '$sendphrase' to fdisk $destdrive"
    echo -e "$sendphrase" | fdisk $destdrive &> /tmp/tmp.log
    res=$?
    cat /tmp/tmp.log >> /tmp/mondo-restore.log
    return $res
}


FormatPartition() {
    local mounttype device
    mounttype=$1
    device=$2
    LogIt "Formatting $device as $mounttype"
    if [ "$mounttype" = "swap" ] ; then
         mkswap $destdrive$partno &> /tmp/tmp.log
    elif [ "$mounttype" = "vfat" ] ; then
        format-and-kludge-vfat $destdrive$partno / &> /dev/null
    elif [ "$mounttype" = "reiserfs" ] ; then
        echo "y" | mkreiserfs $destdrive$partno
    elif [ "$mounttype" = "xfs" ] ; then
	mkfs.xfs -f -q $destdrive$partno
    elif [ "$mounttype" = "jfs" ] ; then
	echo "y" | mkfs.jfs $destdrive$partno
    elif [ "$mounttype" = "ext3" ] ; then
	mkfs -t ext2 -F -j -Jsize=10 -q $destdrive$partno
    elif [ "$mounttype" = "ext2" ] ; then
	mkfs -t ext2 -F -q $destdrive$partno
    else
        echo -en "y\ny\ny\n" | mkfs -t $mounttype -q $destdrive$partno
    fi
    res=$?
    LogIt "Result = $res"
    return $res
}




ProcessDrive() {
    local drive mountlist retval res stub
    drive=$1
    mountlist=$2

    retval=0
    partno=0
    partition_no_list=""
    LogIt "Prepping drive $drive" 2
    stub=`basename "$drive"`
    res=`dmesg | grep "$stub: "`
    if [ "$res" = "" ] ; then
        LogIt "You don't appear to have a drive $drive; ignoring it." 2
        return 0
    fi
    WipeDrive $drive
    last_partno=-1
    while [ "$partno" -lt "50" ] ; do
	partno=$(($partno+1))
	partname=$drive$partno
	found=`cat $mountlist | grep "$partname "`
	if [ "$found" != "" ] ; then
	    partition_no_list="$partition_no_list $partno"
	    last_partno=$partno
	fi
    done
    LogIt "Drive $drive will have partitions $partition_no_list" 2
    for partno in $partition_no_list ; do
	partname=$drive$partno
	found=`cat $mountlist | grep "$partname "`
	FdiskAndFormatPartition "$found" 999
	res=$?
	if [ "$res" -ne "0" ] ; then
#	    if [ "$partno" = "$last_partno" ] ; then
		LogIt " Retrying w/vacuum-packed final partition..." 2
		FdiskAndFormatPartition "$found" $last_partno
		res=$?
		if [ "$res" -ne "0" ] ; then
		    LogIt "Vacuum-pack failed." 1
		    LogIt "*sigh* `basename $drive`, I do believe I've failed you. :-/"
		    FAILED_TO_FORMAT="$FAILED_TO_FORMAT $partname"
		else
		    LogIt "Vacuum-pack succeeded. Partition $partno OK." 2
		    LogIt "Kudos to Fran Boon for finding an ancient bug"
		fi
	    fi
	    if [ "$res" -ne "0" ] ; then
		LogIt "Failed to fdisk and/or format $partno" 3
	    fi
#	else
#	    LogIt "OK, failed to fdisk and/or format $partno, despite \
#trying to vacuum-pack" 3
#	fi
# $last_partno instead of 999
        retval=$(($retval+$res))
        [ "$res" -ne "0" ] && LogIt "Failed to do groovy things to $partno"
    done
    return $retval
}



WipeDrive() {
    dd if=/dev/zero of=$1 bs=1024k count=1 &> /dev/null
    if [ "$?" -ne "0" ] ; then
        LogIt "Error occurred while wiping $1" 1
        return 1
    else
        LogIt "$1 wiped ok" 2
        return 0
    fi
}


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


echo "DO NOT USE THIS SCRIPT"
exit 0

LogIt "prep-me --- starting"

fdisk -v
if [ "$?" -ne "0" ] ; then
    LogIt "Fdisk will not run. Danger, Will Robinson!"
    exit 1
fi

if [ "$#" -ne "0" ] && [ "$#" -ne "1" ] ; then
    LogIt "prep-me (<mountlist>)" 1
    exit 1
fi

if [ "$#" -eq "1" ] ; then
    mountlist=$1
else
    mountlist=/tmp/mountlist.txt
fi

if [ ! -f "$mountlist" ] ; then
    LogIt "Mount list should be at $mountlist but it isn't." 1
    exit 1
fi

drivelist=`cat $mountlist | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]// | sort | uniq`
total_errors=0
FAILED_TO_FORMAT=""
for currentdrive in $drivelist ; do
    ProcessDrive $currentdrive $mountlist
    res=$?
    if [ "$res" -ne "0" ] ; then
	LogIt "$res errors occurred while $currentdrive was being prepped." 1
    fi
    total_errors=$(($total_errors+$res))
done
if [ "$FAILED_TO_FORMAT" != "" ] ; then
    LogIt "Failed to prep:-$FAILED_TO_FORMAT" 1
    total_errors=$(($total_errors+1))
fi
if [ "$total_errors" -ne "0" ] ; then
    LogIt "$total_errors occurred while drives were being prepped." 1
    LogIt "Oh dear. See /tmp/mondo-restore.log for details of errors." 1
else
    LogIt "All drives were partitioned and formatted OK." 1
fi

LogIt "prep-me --- leaving"
exit $total_errors
