#!/bin/sh
# Installation script. Don't call this directly, but via "make install"!
#
# $Id: uninstall.sh 34 2006-09-28 11:32:37Z kelmo $
#
# Copyright (c) 2006 Michael Renzmann <mrenzmann@otaku42.de>
#
# For further information see:
# http://projects.otaku42.de/wiki/madwifi-ifupdown
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# This is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# the madwifi-ifupdown package; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

# returns 0 if the two given files seem to be similar, 1 otherwise
function files_appear_similar {
	[ -n "$1" ] || return 1
	[ -n "$2" ] || return 1
	
	[ -f "$1" ] || return 1
	[ -f "$2" ] || return 1
	
	if [ -n "$(which md5sum)" ]; then
		# compare by md5 checksums
		cs1=$(md5sum $1 | cut -d" " -f1)
		cs2=$(md5sum $2 | cut -d" " -f1)
		[ "$cs1" = "$cs2" ] || return 1
	else
		# compare by file sizes (unreliable!)
		if [ -n "$(which stat)" ]; then
			fs1=$(stat -c%s $1)
			fs2=$(stat -c%s $2)
		else
			fs1=$(ls -l $1 | cut -d" " -f6)
			fs2=$(ls -l $2 | cut -d" " -f6)
		fi
		[ "$fs1" -eq "$fs2" ] || return 1
	fi
	
	return 0
}

if [ -z "$1" ] || [ -z "$2" ]; then
	echo "Run \"make install\" from the top-level source directory."
fi

IFUPDOWNDIR=${1%/}
LIBDIR=${2%/}
DESTDIR=${3%/}

SED=/bin/sed
if [ ! -x $SED ]; then
	echo "$SED: no such file, or file not executable"
	exit 1
fi

if [ "$(id -u)" -ne "0" ]; then
	echo "ERROR: you need to be root to do this."
	exit 1
fi


# step 1: try to make sure that the local installation has been made from this
# madwifi-ifupdown version
if [ ! -d "$DESTDIR/$IFUPDOWNDIR" ] || [ ! -d "$DESTDIR/$LIBDIR" ]; then
	echo "ERROR: Local installation seems incomplete. Refusing to uninstall."
	exit 1
fi

match=1

for d in if-*.d; do
	dir=${d##*/}
	for f in $d/*; do
		[ -f $f ] || continue
		
		file=${f##*/}

		g=/tmp/madwifi-ifupdown-uninstall.tmp
		$SED -e "s#@LIBDIR@#$LIBDIR#g" $f > $g
		
		if ! files_appear_similar $g $DESTDIR/$IFUPDOWNDIR/$dir/$file; then
			match=0
		fi
		
		rm -f $g
	done
done

for f in lib/*; do
	[ -f $f ] || continue
	
	file=${f##*/}
	if ! files_appear_similar $f $DESTDIR/$LIBDIR/$file; then
		match=0
	fi
done


if [ "$match" -ne "1" ]; then
	echo "ERROR: Local installation seemingly does not match this version of"
	echo "       madwifi-ifupdown. Refusing to uninstall."
	exit 1
fi


# step 2: remove all files that we installed before
echo "uninstalling madwifi-ifupdown..."
for d in if-*.d; do
	dir=${d##*/}
	for f in $d/*; do
		[ -f $f ] || continue
		file=${f##*/}
		target="$DESTDIR/$IFUPDOWNDIR/$dir/$file"
		
		echo "  RM $target"
		rm $target
	done
done

# make sure we don't try to delete /
if [ -n "$DESTDIR/$LIBDIR" ] && [ "$DESTDIR/$LIBDIR" != "/" ]; then
	echo "  RM $DESTDIR/$LIBDIR"
	rm -r $DESTDIR/$LIBDIR
else
	echo "ERROR: refusing to delete '$DESTDIR/$LIBDIR'!"
	exit 1
fi

echo "done."
