#!/bin/sh
#
#  ======== prep-hdd ========
#
#  This shell script performs initial partitioning, formating, and populating
#  of the hard disk drive (HDD) for the DaVinci EVM.
#
#  Usage: prep-hdd [URL prefix or path of install files]
#

#
#  URL prefix is where the files needed for the install are located
#  (ftp or http). If there is not a http:// or ftp:// prefix, then the
#  prefix is assumed to be a directory path and a local cp will be used.
#
if test $# != 1
then
    echo "Usage: $0 <URL or path to install files>"
    echo "Example: $0 ftp://128.247.27.36/sanb-davinci/1_00_00_25/restore"
    echo "Example: $0 /opt/newstuff"
    exit 2
fi
    
urlprefix=$1
if test  $urlprefix = "." 
then
   urlprefix=`pwd`
fi


# device name for the HDD
disk=/dev/hda

# temporary mount point for the restore HDD partition
restore=/mnt/restore

# number of cylinders on the HDD
cylinders=$(fdisk -l /dev/hda | grep heads, | cut -d " " -f 5)

# number of cylinders for primary partition (arbitrarily set at 90%)
let pri=($cylinders*90)/100
echo "Using $pri cylinders for primary partition"

#
#  This function copies depending on the presence of a URL prefix on the
#  source file. If a URL prefix is found (http:// or ftp://), then wget is
#  used to copy the file. If not, cp is used to do a filesystem copy.
#
function copy ()
{
    f=${1#*://}

    if test $f = $1
    then
        cp $1 .
    else
        wget $1
    fi
}

#
#  Make sure user really wants to do this ...
#
echo -n "Are you sure you want to partition and format $disk? (yes/NO): "
read answer

if test "$answer" != "yes"
then
    echo "$0: exiting because answer was not yes"
    exit 2
fi

#
#  Make sure disks are not mounted
#  (umount on MVL 4.0 does not work with devices, so need to find mount point)
#
for i in 1 2
do 
    dir=$(mount | grep ${disk}$i | cut -d " " -f 3)
    if test "$dir" != ""
    then
        umount $dir > /dev/null 2>&1
    fi
done    

#
#  Partition the disk using fdisk. The following script deletes any
#  existing partitions and then makes 2 partitions of size $pri and
#  ($cylinders - $pri) (ie, the remaining amount) cylinders respectively.
#
echo "Partitioning $disk ... "
fdisk $disk <<EOF
d
1
d
2
d
3
d
4
n
p
1
1
$pri
n
p
2


w
EOF

# Delay due to an issue where a mkfs righ after fdisk
# on occasion fails
sleep 6;

#
#  Format the new restore (second) partition using ext3 format. The
#  main (primary) partition will be done as part of installing the SW below.
#
echo "Formating ${disk}2 ... "
if ! mkfs.ext3 ${disk}2
then
    echo "$0: failed to format ${disk}2"
    exit 2
fi    

#
#  Make a mount point and then mount the new partition
#
echo "Mounting the new partition ... "
rm -f $restore && mkdir $restore
mount -t ext3 ${disk}2 $restore

#
#  Copy the target filesystem and overlay tar-files into the restore area
#
cd $restore
echo "Copying tar.gz files ... "
for i in data.tar.gz mvltarget5_0_0801921_update.tar.gz overlay.tar.gz restore-hdd
do
    echo $i
    copy $urlprefix/$i
    chmod -w $i
done

chmod +x restore-hdd

#
#  Run the restore script to setup the primary area
#
echo "Un-tar'ing target filesystem, modules, and demos ... "
./restore-hdd y
