#!/bin/bash
#
# $Id: install.sh 2183 2009-04-30 15:52:54Z bruno $
#

if [ ! -f "mindi" ] ; then
    echo "Please 'cd' to the directory you have just untarred." >> /dev/stderr
    exit 1
fi

if [ "_$PREFIX" != "_" ]; then
	local=${HEAD}$PREFIX
	sublocal=$PREFIX
	if [ "_$CONFDIR" != "_" ]; then
		conf=${HEAD}$CONFDIR/mindi
		subconf=$CONFDIR/mindi
	else
		echo "CONFDIR should be defined if PREFIX is defined"
		exit -1
	fi
else
	local=/usr/local
	sublocal=$local
	if [ -f /usr/sbin/mindi ]; then
		echo "WARNING: /usr/sbin/mindi exists. You should probably remove the mindi package !"
	fi
	conf=$local/etc/mindi
	subconf=$conf
	echo $PATH | grep $local/sbin > /dev/null || echo "Warning - your PATH environmental variable is BROKEN. Please add $local/sbin to your PATH."
fi

if uname -a | grep Knoppix > /dev/null || [ -e "/ramdisk/usr" ] ; then
    local=/ramdisk/usr
	sublocal=$local
	conf=/ramdisk/etc
	subconf=$conf
    export PATH=/ramdisk/usr/sbin:/ramdisk/usr/bin:/$PATH
fi

MINDIVER=PBVER
MINDIREV=PBREV
ARCH=`/bin/uname -m`
echo "mindi ${MINDIVER}-r${MINDIREV} will be installed under $local"

if [ _"$CACHEDIR" = _"" ]; then
	CACHEDIR=$local/var/cache/mindi
else
	CACHEDIR=${HEAD}$CACHEDIR
fi
if [ _"$MANDIR" = _"" ]; then
	MANDIR=$local/share/man/man8
else
	MANDIR=${HEAD}$MANDIR/man8
fi
if [ _"$DOCDIR" = _"" ]; then
	DOCDIR=$local/share/doc/mindi-$MINDIVER
else
	DOCDIR=${HEAD}$DOCDIR/mindi-$MINDIVER
fi
if [ _"$LIBDIR" = _"" ]; then
	echo $ARCH | grep -E '^i[0-9]86$' &> /dev/null && ARCH=i386 && locallib=$local/lib
	echo $ARCH | grep -E '^x86_64$' &> /dev/null && locallib=$local/lib64
	echo $ARCH | grep -E '^ia64$' &> /dev/null && locallib=$local/lib
	sublocallib="$locallib/mindi"
else
	locallib=${HEAD}$LIBDIR
	sublocallib="$LIBDIR/mindi"
fi

echo "Creating target directories ..."
install -m 755 -d $conf $locallib/mindi $MANDIR $local/sbin $CACHEDIR

echo "Copying files ..."
cp -af rootfs $locallib/mindi
chmod 755 $locallib/mindi/rootfs/sbin/*
install -m 755 analyze-my-lvm $locallib/mindi
install -m 644 msg-txt dev.tgz $locallib/mindi
install -m 644 deplist.txt udev.files proliant.files $conf
install -m 755 mindi-bkphw > $local/sbin
install -m 755 mindi-get-perl-modules $local/sbin
install -m 755 parted2fdisk.pl $local/sbin

# Substitute variables for mindi
sed -e "s~^MINDI_PREFIX=XXX~MINDI_PREFIX=$sublocal~" -e "s~^MINDI_CONF=YYY~MINDI_CONF=$subconf~" -e "s~^MINDI_LIB=LLL~MINDI_LIB=$sublocallib~" mindi > $local/sbin/mindi
chmod 755 $local/sbin/mindi $local/sbin/mindi-bkphw

install -m 644 mindi.8 $MANDIR
#install -m 644 ChangeLog COPYING README README.busybox README.ia64 README.pxe TODO INSTALL svn.log $DOCDIR

if [ "_$PREFIX" = "_" ] && [ ! -f $locallib/mindi/rootfs/bin/busybox ]; then
		echo "WARNING: no busybox found, mindi will not work on this arch ($ARCH)"
fi

# Managing parted2fdisk
if [ "$ARCH" = "ia64" ] ; then
	(cd $local/sbin && ln -sf parted2fdisk.pl parted2fdisk)
	install -s -m 755 $local/sbin/parted2fdisk.pl $locallib/mindi/rootfs/sbin/parted2fdisk
else
	# FHS requires fdisk under /sbin
	(cd $local/sbin && ln -sf /sbin/fdisk parted2fdisk)
	echo "Symlinking fdisk to parted2fdisk"
	( cd $locallib/mindi/rootfs/sbin && ln -sf fdisk parted2fdisk)
fi

if [ "$PKGBUILDMINDI" != "true" ]; then
	chown -R root:root $locallib/mindi $conf # $DOCDIR
	chown root:root $local/sbin/mindi $MANDIR/mindi.8 $locallib/mindi/analyze-my-lvm $local/sbin/parted2fdisk.pl 
	if [ "$ARCH" = "ia64" ] ; then
		chown root:root $local/sbin/parted2fdisk
	fi
fi

# Generate the list of perl modules needed at restore time for mindi
cat > $conf/perl-scripts << EOF
$local/sbin/mindi-get-perl-modules
$local/sbin/mindi-bkphw
$local/sbin/parted2fdisk.pl
EOF

exit 0
