#!/bin/sh

. /etc/color
#set -x

DES="/"
TMP=/tmp/mup$$
mkdir $TMP
ARC=$TMP/arc.tar

# LIBRARY

abort()
{
echo -e "$BRIGHT$RED Abort: $@ $NORMAL"
exit 1
}

msg()
{
echo -e "$BRIGHT $@ $NORMAL"
}

check_tar()
{
version=`tar --version 2>&1`
case $version in
*GNU*)	return 0;;
*)	return 1;;
esac
}

ask_upgrade()
{
cat <<END

Any files in following dialog differs in SIZE and/or MODIFICATION 
time from those currently installed in your system. You can control
interactively upgrading process for each, answering 'y' or 'n'.

Basically you have to answer "n" only to secondary files you 
customized and/or changed by hand.  

END

read -p "Do you wish interactive control (yes/no)? [yes]" a
a=${a:-yes}

case $a in
y|yes) W="w";;
n|no)  W="";;
*)
        echo "Unknown option"
	exit 1 ;;
esac

}

ask_add()
{
cat <<END

Any files in following dialog DO NOT APPEAR currently installed  
in your system (they are NEW files). You can control interactively
the extraction process for any, answering 'y' or 'n' to each query.

END

read -p "Do you wish interactive control (yes/no)? [yes]" a
a=${a:-yes}

case $a in
y|yes) W="w";;
n|no)  W="v";;
*)
        echo "Unknown option"
	exit 1
        ;;
esac

}


filter()
{
sed "s/:.*$//g"
}

add_files()
{
cd $DES
tar -df $ARC  | rgrep "Does not exist" | filter >  $TMP/new 
if [ -s $TMP/new ] ; then
	n=`wc -l < $TMP/new`
	msg "Found "$n" new file ..."
	ask_add
	tar -${W}xf $ARC -T $TMP/new  -C $DES 2>/dev/null 
else
	msg "No news files."
fi
}

upgrade_files()
{
cd $DES
tar  -df $ARC  | rgrep "Size differs" | filter >  $TMP/upgrade 
n=`wc -l < $TMP/upgrade`
if [ "$n" -eq 0 ] ; then
	msg "No files to upgrade."
	return 0
fi
msg "Found "$n" files which differs from yours  ..."

ask_upgrade
tar -${W}xf $ARC -T $TMP/upgrade  -C $DES 2>/dev/null || abort untarring 
msg "Upgrading finished."
}

scan_dir()
{
SOURCE=$1
cd $SOURCE || abort "entering in $SOURCE"


addons="`/bin/ls -1q *.tgz| rgrep -v mulinux`"

for f in ROOT.gz USR.bz2 ${addons} 
do
	[ -f $f ] && echo $f 
done

}


uncompress_image()
{
a=$1

echo -n "Uncompressing archive $a ...."
case $a in
*ROOT.gz)
	gzip -dc $a 2>/dev/null > $TMP/img 
	mkdir $TMP/mnt
	mnt=$TMP/mnt
	mount -o loop $TMP/img $mnt
	cd $mnt
	tar -pscf $ARC *  
	cd /
	umount $mnt
	rm $TMP/img
	;;
*USR.bz2)
	bzip2 -ds < $a > $TMP/img
        mnt=$TMP/mnt/usr
	mkdir -p $mnt
        mount -o loop $TMP/img $mnt
        cd $TMP/mnt
	tar -pcf $ARC *
        cd /
	umount $mnt
        rm $TMP/img
        ;;
*.tgz)	# addons
	type=`file $a`
	case $type in
	*gzip*)
		gzip -dc $a > $ARC
		;;
	*bzip*)
		bzip2 -ds < $a > $ARC 2>/dev/null 
		;;
	*)
		echo "unknown compressor.";;
	esac
	;;
esac	
echo "done."	
}

#
# MAIN
#
clear
msg "MUP: Mulinux upgrading utility, Rev 0.2"
msg "Fractured Software Foundation (C) M. Andreoli 2000"
echo

check_tar 
if [ $? -ne 0 ] ; then
	msg "$0 requires GNU tar" ; exit 1
fi

echo -e "$GREEN"
cat <<END
Because this script will try to add/upgrade in your system some files
or binaries currently under use, it is suggested to keep the machine 
very quiet, shutting down all services ( setup -a shutdown stop).

Upgrade command know about this kind of archive: ROOT.gz, USR.bz2,
the various addons (tar+bzip2) and also sparse tgz patches 
(tar+gzip) 

How to use the Upgrade Utility:

	1. put your new archives in a mountable directory. 
	  For example: if you are in UMSDOS, you might use 
	  /DOS/mulinux as SOURCE directory.
	2. start this command, and supply your SOURCE directory 

If you are a little confused, stop now this script with CONTROL-C, 
and mount the right disk.
END
echo -e "$NORMAL"

while [ 1 ] ; do
default=${SOURCE:-"/mnt/mulinux"}
read -p "Enter the SOURCE directory name [$default] " SOURCE
SOURCE=${SOURCE:-$default}
[ -d $SOURCE ] && break
echo "Do not exists!"
done


echo
msg "Setup is inspecting your computer :-))"
echo
sleep 2

list="`scan_dir $SOURCE`"
msg "Found these archive:"
msg $list
echo
for a in $list
do
	read -p "Process Archive $a (y/n) [y]? " p
	p=${p:-y}
	[ "$p" = 'n' ] && continue

	uncompress_image $SOURCE/$a

	cd $DES
	tar  -df $ARC > $TMP/diff 
	add_files
	upgrade_files
done

msg "Removing tmp area ..."
rm -r $TMP

msg "DONE!"
msg
