#!/bin/sh


OWNDIR=`pwd`
if [ $? -ne 0 ]; then
  OWNDIR=/tmp
fi  

#busybox Path
bb_path="/bin/" 
#Link Path
sl_path="/bin/"
if [ "${bb_path}" = "${sl_path}" ];then
	cd "${bb_path}"
	ln -s busybox grep > /dev/null 2>&1
	ln -s busybox sed > /dev/null 2>&1
else
	ln -s "${bb_path}busybox" "${sl_path}grep" > /dev/null 2>&1 
	ln -s "${bb_path}busybox" "${sl_path}sed" > /dev/null 2>&1
fi

cd $OWNDIR


TMP="/tmp/patch.help"
RCS="/etc/init.d/rcS"

if [ `grep "AAF rcS Patch3 ------ be" $RCS | wc -l` -gt 0 ]; then
  grep -n "AAF rcS Patch3 ------ be" $RCS > $TMP
  PATCHLINE1=`cut -d: -f1 $TMP`
  HEADLAST=`expr $PATCHLINE1 - 1`
  grep -n "AAF rcS Patch3 ------- end" $RCS > $TMP
  PATCHLINE2=`cut -d: -f1 $TMP`
  LENGTH=`cat $RCS | wc -l`
  TAILBEGIN=`expr $LENGTH "-" $PATCHLINE2`
   #echo $PATCHLINE $LENGTH $REMAIN
   head -n $HEADLAST $RCS > $TMP
   tail -n $TAILBEGIN $RCS >> $TMP
   cp $TMP $RCS
fi

# update1 ----------------
if [ `grep "AAF rcS update1" $RCS | wc -l` -eq 0 ]; then
  echo "upating rcS with update1"
  grep -n "/etc/init.d/vsftpd start" $RCS > $TMP
  PATCHLINE=`cut -d: -f1 $TMP`
  LENGTH=`cat $RCS | wc -l`
  REMAIN=`expr $LENGTH "-" $PATCHLINE`
  head -n $PATCHLINE $RCS > $TMP
  echo "# ------------------ AAF rcS update1 ------- begin ---------" >> $TMP
  echo "   /bin/amount1 10 &" >> $TMP
  echo "# ------------------ AAF rcS update1 ------- end -----------" >> $TMP
  tail -n $REMAIN $RCS >> $TMP
  cp $TMP $RCS
else
 echo "update1 is already installed"   
fi 

cat $RCS | sed s/" mount -t ext3 \/dev\/sda1 \/hdd"/"#AAF5-mount -t ext3 \/dev\/sda1 \/hdd"/g > $TMP
cat $TMP > $RCS

echo "install update2"
cp ./update2.tar.gz /update2.tar.gz
cd /
tar -xzf update2.tar.gz
rm /update2.tar.gz
sync