#!/bin/sh


dir=`dirname $0`
echo DIR is $dir
cd $dir

#find . -type f | grep -v checksum.txt | xargs -d '\n' md5sum | sort > /tmp/.update_checksum
i=0

rm -f /usr/trimui/res/lang/ch*

cnt=`find . -type f | grep -v checksum.txt | wc -l`
cnt=`expr $cnt \* 2`
rm -f /tmp/.update_checksum
percent=0
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in `find . -type f | grep -v checksum.txt | grep -v fix_target_links.sh | sort` ; do
    md5sum "$f" >> /tmp/.update_checksum
	i=`expr $i + 1` 
    percent=`expr $i \* 100 \/ $cnt`
    fn=`basename $f`
    notify $percent verify $fn
done
IFS=$SAVEIFS
diff /tmp/.update_checksum $dir/checksum.txt
if ! [ "$?" == "0" ] ; then
	notify $percent package verification fail
	sleep 1
	notify $percent quit
	exit 1
fi
rm /tmp/.update_checksum


SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in `find . -type f | grep -v checksum.txt | grep -v updater | grep -v fix_target_links.sh` ; do 
    dn=`dirname $f`
	fn=`basename $f`
	mkdir -p /usr/trimui/$dn
	cp $f /usr/trimui/$dn/$fn
	i=`expr $i + 1` 
	percent=`expr $i \* 100 \/ $cnt`
	notify $percent update $fn
done
IFS=$SAVEIFS
if [ -f ./fix_target_links.sh ] ; then
    chmod a+x ./fix_target_links.sh
    ./fix_target_links.sh /usr/trimui
fi
sed -i 's@/bin/mount -t ${fstype} -o utf8 /dev/${MDEV} ${MOUNTPOINT}@/bin/mount -t ${fstype} -o utf8,errors=continue /dev/${MDEV} ${MOUNTPOINT}@g' /usr/bin/hotplug.sh
cd -
notify $percent quit