#! /bin/sh

die() {
      echo "$@" 
      exit 1
}
# Get current path
CUR_PATH=`pwd`

# create tcl8.0 and tk8.0 installation directory

if [ ! -d tclbox ]
then
mkdir tclbox
fi

if [ ! -d tkbox ]
then
mkdir tkbox
fi

# install and install Tcl8.0

cd ./tcl8.0/unix

./configure --prefix="$CUR_PATH/tclbox" || die "tcl8.0 configuration failed! Exiting ..."
make || die "tcl8.0 make failed! Exiting ..."
make install || die "tcl8.0 installation failed! Exiting ..."

cd ../../

# compile and install tk

cd ./tk8.0/unix

./configure --prefix="$CUR_PATH/tkbox" || die "tk8.0 configuration failed! Exiting ..."
make || die "tk8.0 make failed! Exiting ..."
make install || die "tk8.0 installation failed! Exiting ..."

cd ../../

# do some copy for otcl

cp ./tcl8.0/generic/*.h ./tclbox/include/
cp ./tk8.0/generic/*.h ./tkbox/include/

# compile and install otcl

cd ./otcl 

./configure --with-tcl=../tclbox --with-tk=../tkbox || die "otcl configuration failed! Exiting ..."

make || die "otcl make failed! Exiting ..."

echo "otcl has been installed successfully !"

cd ../

# compile and install TclCL

cd ./TclCL

./configure --with-otcl=../otcl --with-tcl=../tclbox --with-tk=../tkbox  --with-tcl-ver=8.0 --with-tk-ver=8.0 || die "TclCL configuration failed! Exiting ..."

make || die "TclCL make failed! Exiting ..."

echo "TclCL has been installed successfully!"

cd ../

#compile and install xgraph

cd ./xgraph
if xmkmf
then
    if make
    then
	echo "xgraph has been installed successfully! "
    else 
	echo "Can not create xgraph! But xgraph is an optional package, continue..."
    fi
else
    echo " Can not create Makefile for xgraph !, but xgraph is an optional package, continue ..."
fi

cd ../

#compile and install ns

cd ./ns-2
./configure --with-otcl=../otcl --with-tclcl=../TclCL --with-tcl=../tclbox --with-tk=../tkbox --disable-static || die "Ns configuration failed! Exiting ..."

make || "Ns make failed! Exiting ..."

echo " Ns has been installed successfully!" 

cd ../

#compile and install nam

cd ./nam-1

./configure --with-otcl=../otcl --with-tclcl=../TclCL --with-tcl=../tclbox --with-tk=../tkbox --disable-static || die "Nam configuration failed! Exiting ..."

make || die "Nam make failed! Exiting ..."

echo "Nam has been installed successfully!"

cd ../

# install nam, ns, xgraph into bin

if [ ! -d bin ]
then
mkdir bin
fi

cd bin

ln -s $CUR_PATH/ns-2/ns ns
ln -s $CUR_PATH/nam-1/nam nam

if test -x $CUR_PATH/xgraph/xgraph
then
    ln -s $CUR_PATH/xgraph/xgraph xgraph
else
    echo "Please compiling your xgraph separately!"
fi

echo "Ns-allinone package has been installed successfully !"
echo "Here are the installation places:"
echo "ns:	$CUR_PATH/ns-2/ns"
echo "nam:	$CUR_PATH/nam-1/nam"
echo "otcl:	$CUR_PATH/otcl"
echo "TclCL:	$CUR_PATH/TclCL"
echo "tcl8.0:	$CUR_PATH/tclbox"
echo "tk8.0:	$CUR_PATH/tkbox"
if [ -x $CUR_PATH/xgraph/xgraph ]
then
echo "xgraph:	$CUR_PATH/xgraph"
fi
echo ""
echo "You can delete $CUR_PATH/tcl8.0 and $CUR_PATH/tk8.0 if "
echo "you want save your disk space"

echo "-----------------------------------------------------"
echo "Please put $CUR_PATH/bin into your PATH environment!"
echo "Please put $CUR_PATH/otcl into your LD_LIBRARY_PATH environment!"
echo ""
echo "You can run the ns validation suite with"
echo "cd ns-2; ./validate"

exit 0

