#!/bin/sh
# Installation script. Don't call this directly, but via "make install"!
#
# $Id: install.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
#
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: install resource stuff
echo "installing madwifi-ifupdown..."

if [ ! -d $DESTDIR/$LIBDIR ]; then
	echo "  MKDIR   $DESTDIR/$LIBDIR"
	mkdir -p "$DESTDIR/$LIBDIR"
fi

for f in lib/*; do
	if [ -f $f ]; then
		file=${f##*/}
		echo "  INSTALL lib/$file"
		cp $f $DESTDIR/$LIBDIR
	fi
done


# step 2: install hook scripts
for d in if-*.d; do
	dir=${d##*/}

	if [ ! -d $DESTDIR/$IFUPDOWNDIR/$dir ]; then
		echo "  MKDIR   $DESTDIR/$IFUPDOWNDIR/$dir"
		mkdir -p $DESTDIR/$IFUPDOWNDIR/$dir
	fi

	for f in $d/*; do
		if [ -f $f ]; then
			file=${f##*/}
			target=$DESTDIR/$IFUPDOWNDIR/$dir/$file

			echo "  INSTALL $dir/$file..."

			$SED -e "s#@LIBDIR@#$LIBDIR#g" $f > $target
			chmod u=rwx,g=rx,o=r $target
		fi
	done
done

echo "done."
