#!/bin/bash

#  install.sh	install webconv scripts (convert old web pages to new Green Bank style)

#  paulruffle@paulruffle.com		last revised 15 Apr 2008

convpath="$HOME/webconv"
binpath="$HOME/bin"

echo ""

if [ $# = 0 ] ; then
    echo "Error: you have not specified a target operating system!"
    echo ""
    echo "Usage: ./install osx"
    echo "Usage: ./install linux"
    echo ""
    exit
fi

if [ "$1" != "osx" ] && [ "$1" != "linux" ] ; then
    echo "Error: invalid target operating system!"
    echo ""
    echo "Usage: ./install osx"
    echo "Usage: ./install linux"
    echo ""
    exit
elif [ "$1" = "osx" ] ; then
    opsys="osx"
    opnam="Mac OS X"
elif [ "$1" = "linux" ] ; then
    opsys="linux"
    opnam="Linux"
fi

echo "Installing WebConv for $opnam, hit <RET> to continue ..."
read ans

if test -d $convpath ; then
    echo "WebConv directory $convpath already exists"
else
    echo "Creating WebConv directory $convpath"
    mkdir $convpath
fi

if test -d $binpath ; then
    echo "Home bin directory $binpath already exists"
else
    echo "Creating home bin directory $binpath"
    mkdir $binpath
fi

echo "Copying resource files to $convpath"
cp conv2gbweb_*.sh $convpath
cp *.sed  $convpath
cp *.html $convpath
cp README $convpath

echo "Linking resource files to $binpath"
ln -s -f $convpath/conv2gbweb_$opsys.sh $binpath/web2gbweb
ln -s -f $convpath/conv2gbweb_$opsys.sh $binpath/html2gbweb
ln -s -f $convpath/conv2gbweb_$opsys.sh $binpath/twiki2gbweb
ln -s -f $convpath/conv2gbweb_$opsys.sh $binpath/gbwiki2gbweb

homebin=`echo $PATH | grep $binpath`

if [ "$homebin" = "" ] ; then
    echo ""
    echo "You need to have $binpath in your PATH to use WebConv"
    echo "Would you like me to modify your .bashrc to add $binpath to your PATH? (y/n)"
    read ans
    if [ "$ans" = "y" ] ; then
        echo "Adding $binpath to your PATH by modifying .bashrc"

        echo "" >> $HOME/.bashrc
        echo "# $binpath added to PATH for WebConv on `date`" >> $HOME/.bashrc
        echo "export PATH=$PATH:$binpath" >> $HOME/.bashrc
        echo "" >> $HOME/.bashrc

        echo "Please start a new shell for $binpath to be in your PATH"
    else
        echo "To use WebConv please add $binpath to your PATH manually"
    fi
fi

echo ""
echo "WebConv successfully installed"
echo ""

# end of script

