#!/bin/sh
#
#  ======== restore-hdd ========
#
#  Shell script to install or restore the primary partition of the HDD on a
#  DaVinci DVEVM.
#

#
#  ======== progress ========
#
#  Simple progress indicator function to give feedback on how much has been
#  completed.
#
#  Usage: progress num-lines-expected
#
progress ()
{
    let inc=$1/20
    let cnt=0
    let percent=5

    while read line
    do
        let cnt=cnt+1
        if test $cnt -eq $inc
        then
            echo -n -e "${percent}% \r"
            if test $percent -lt 100
            then
                let percent=percent+5
            fi
            let cnt=0
        fi
    done
}

disk=/dev/hda1
primary=/mnt/primary

# Add an option from prep-hdd not to ask
if test "$1" != "y"
then
  # make sure user really wants to do this
  echo -n "This will destroy all data on $disk - are you sure? (yes/NO) "
  read answer

  if test "$answer" != "yes"
  then
    echo "The answer must be "yes" to perform the restore operations"
    exit 1
  fi
fi
# check to make sure all the needed files are present
test -f arago-demo-image-dm6467t-evm.tar.gz && test -f overlay_dm6467.tar.gz && test -f data_dm6467.tar.gz
if test $? -ne 0
then
    echo "One or more of the necessary files are missing - cannot restore"
    exit 2
fi

# save where we started from
old=$PWD

# make sure the disk is not mounted or we cannot format it
dir=$(mount | grep ${disk} | cut -d " " -f 3)
if test "$dir" != ""
then
    umount $dir > /dev/null 2>&1
fi

echo "Formatting $disk ... "
if ! mkfs.ext3 $disk
then
    echo "$0: could not format $disk"
    exit 2
fi    


echo "Mounting newly formated partition ... "
mkdir $primary > /dev/null 2>&1
mount -t ext3 $disk $primary > /dev/null

echo "Creating target root filesystem (be patient) ... "
cd $primary
tar xzvf $old/arago-demo-image-dm6467t-evm.tar.gz | progress 63000

#
#  Install the demo applications and associated data files. Since the
#  demos can be invoked from the web server, they need to be suid so they
#  can open the A/V devices.
#
echo "Installing DVEVM demos ... "

cd  $primary
tar zxf $old/overlay_dm6467.tar.gz

mkdir -p $primary/opt/dvsdk
cd  $primary/opt/dvsdk
tar zxf $old/data_dm6467.tar.gz

chown -R root $primary/opt/dvsdk
cd  $primary/opt/dvsdk/dm64*
chmod u+s interface decode encode encodedecode

#
#  Setup for the auto-start of the demo interface and web server at
#  system boot.
#

mv $primary/opt/dvsdk/dm64*/web/thttpd $primary/etc/init.d/thttpd
cp -r $primary/opt/dvsdk/dm64*/web/* $primary/srv/www/
chmod 644 $primary/srv/www/*.*
chmod a+x $primary/srv/www/cgi-bin/*
chmod a+x $primary/opt/dvsdk/dm64*/dvevmdemo
mv $primary/opt/dvsdk/dm64*/dvevmdemo $primary/etc/init.d
cd $primary/etc/rc5.d
ln -s ../init.d/dvevmdemo S99dvevmdemo

#
#  Install the kernel modules for things like USB mass storage.
#
if [ -f $old/modules.tar.gz ];then
  echo "Installing modules ... "
  cd $primary
  tar xzf $old/modules.tar.gz
  chown -R root /lib
  #depmod -a -r -b $primary
  depmod -a -r -b $primary $(ls $primary/lib/modules)
else
  echo "Warning $old/modules.tar.gz was not found (depending on LSP, this may be ok)"
fi



echo ""

echo "Restore complete"


