#!/bin/sh
#download the latest dCore initrd
#tinycorelinux.net
#Original from jpeters, 
#http://forum.tinycorelinux.net/index.php/topic,3101.msg15964.html#msg15964
#modified for dCore by jls (franclinux@gmail.com)


BOOTDIR="/etc/sysconfig/tcedir//boot"
MIRROR="$(cat /opt/tcemirror)/dCore/x86"
DCORETYPE=$(version|awk -F . '{ print $1 }')
INITRD=$DCORETYPE.gz


### Select Release or Release Candidate 
echo "Enter r or c (release or candidate)" 
read RELEASE

case $RELEASE in 
 "r" ) REMOTEDIR="${MIRROR}/release/$DCORETYPE"  ;;
 "c" ) REMOTEDIR="${MIRROR}/release_candidates/$DCORETYPE" ;;
 * ) echo "invalid option" ; exit 1 ;; 
esac

####### View Directory ######
if [ `which netrik` ]; then 
   netrik  "${REMOTEDIR}"
   echo "continue?  'y' "
   read RESPONSE
   [ ${RESPONSE} == "y" ] || exit 1
fi
 


####### Download and Check Release #########

[ -e /tmp/download ] || mkdir /tmp/download


cd /tmp/download 
wget ${REMOTEDIR}/$INITRD
wget ${REMOTEDIR}/$INITRD.md5.txt


CHECK="$(md5sum  -c  *.md5.txt)"
 if [ `echo $CHECK | awk '{print $2}'` != "OK" ]; then
   echo "md5 doesn't match"
   exit 1 
 fi 

##### Extract #########

#[ -e /tmp/extract ] || sudo mkdir /tmp/extract 

#sudo mount *.iso  /tmp/extract -o loop,ro 


######### Backup present release 

cd ${BOOTDIR}
[ -e $INITRD ] && mv $INITRD  $INITRD.old

########### Copy new release to boot dirctory #######

cp /tmp/download/$INITRD ${BOOTDIR} 

############  Cleanup ##########
#sudo  umount /tmp/extract
#rmdir /tmp/extract
sudo rm -R /tmp/download 


echo "Done!"

exit 0


