#!/bin/sh

VERSION=0.9.2

DEFPREFIX=/usr/local
PREFIX=
echo -n "Prefix [$DEFPREFIX]: "
read PREFIX
if [ -z "$PREFIX" ]; then
	PREFIX=${DEFPREFIX}
fi

mkdir -p $PREFIX/man/man1
mkdir -p $PREFIX/bin
mkdir -p $PREFIX/lib/shirc
mkdir -p $PREFIX/etc

echo -n "Installing $PREFIX/man/man1/shirc.1 ..."
rm -f ${PREFIX}/man/man1/shirc.1 
cat man/shirc.1 | sed "s#@prefix@#${PREFIX}#g;s#@version@#${VERSION}#g" > ${PREFIX}/man/man1/shirc.1 && \
	chmod 444 $PREFIX/man/man1/shirc.1 
if [ $? = 0 ]; then
	echo " done"
else
	echo " failed"
	exit 1
fi

echo -n "Installing $PREFIX/man/man1/shirc-link.1 ..."
rm -f ${PREFIX}/man/man1/shirc-link.1
cat man/shirc-link.1 | sed "s#@prefix@#${PREFIX}#g;s#@version@#${VERSION}#g" > ${PREFIX}/man/man1/shirc-link.1 && \
	chmod 444 $PREFIX/man/man1/shirc-link.1 
if [ $? = 0 ]; then
	echo " done"
else
	echo " failed"
	exit 1
fi


echo -n "Installing $PREFIX/bin/shirc ..."
cat shirc |
	sed "s#@prefix@#${PREFIX}#g;s#@version@#${VERSION}#g" > \
	$PREFIX/bin/shirc && \
	chmod 755 $PREFIX/bin/shirc
if [ $? = 0 ]; then
	echo " done"
else
	echo " failed"
	exit 1
fi

echo -n "Installing $PREFIX/bin/shirc-link ..."
cp shirc-link $PREFIX/bin && chmod 755 $PREFIX/bin/shirc-link
cat shirc-link |
	sed "s#@prefix@#${PREFIX}#g;s#@version@#${VERSION}#g" > \
	$PREFIX/bin/shirc-link && \
	chmod 755 $PREFIX/bin/shirc-link
if [ $? = 0 ]; then
	echo " done"
else
	echo " failed"
	exit 1
fi


echo -n "Installing modules in $PREFIX/lib/shirc ..."
cp `find lib -type f -maxdepth 1` $PREFIX/lib/shirc
if [ $? = 0 ]; then
	echo " done"
else
	echo " failed"
	exit 1
fi

echo -n "Installing modules in $PREFIX/lib/shirc ..."
cp `find lib -type f -maxdepth 1` $PREFIX/lib/shirc
if [ $? = 0 ]; then
	echo " done"
else
	echo " failed"
	exit 1
fi

echo -n "Creating etc/shirc.conf ..."
sed "s#@prefix@#${PREFIX}#g;s#@version@#${VERSION}#g" etc/shirc.conf.in > \
	etc/shirc.conf
if [ $? = 0 ]; then
	echo " done"
else
	echo " failed"
	exit 1
fi

echo "-------------------------------------------------------------------------------"
echo "Installation finished."
echo "If this is your first installation, consider installing"
echo "etc/shirc.conf and etc/shirc.motd to ${PREFIX}/etc"
echo "-------------------------------------------------------------------------------"
