#! /bin/sh
#
# Copyright (C) 2000 by USC/ISI
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that the above copyright notice and this paragraph are
# duplicated in all such forms and that any documentation, advertising
# materials, and other materials related to such distribution and use
# acknowledge that the software was developed by the University of
# Southern California, Information Sciences Institute.  The name of the
# University may not be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# $Header: /nfs/jade/vint/CVSROOT/ns-2/allinone/install,v 1.22 2002/04/22 17:49:34 buchheim Exp $

die() {
	echo "$@"  1>&2
	test ! -z "$blame" && echo "$blame" 1>&2
	exit 1
}

warn() {
	echo "$@"
}

# Package VERSIONs. Change these when releasing new packages
TCLVER=8.3.2
TKVER=8.3.2
OTCLVER=1.0a8
TCLCLVER=1.0b12
NSVER=2.1b9
NAMVER=1.0a11
XGRAPHVER=12.1

# Get current path
CUR_PATH=`pwd`

# Compile and install xgraph

echo "============================================================"
echo "* Build XGraph-$XGRAPHVER"
echo "============================================================"

cd ./xgraph-$XGRAPHVER
./configure --prefix=../
if make
then
	echo "xgraph has been installed successfully. "
else 
	echo "Can not create xgraph; But xgraph is an optional package, continuing..."
fi

cd ../

# Compile and install cweb and sgblib

echo "============================================================"
echo "* Build CWeb"
echo "============================================================"

cd ./cweb

if [ ! -f ./Makefile ]
then
	echo "ns-allinone unable to install cweb for you. Please install it manually. cweb is used by sgb to create sgblibrary needed by scenario-generator. But this will not affect the use of ns as such, so continue.."
else
	echo "Making cweb"
	touch *.c
	make all || warn "cweb failed to make, but it's optional"
	# xxx: other stuff will fail...
	chmod 755 cweave
	chmod 755 ctangle
	cd ..
	#echo "cd .."
	if [ ! -d bin ]
	then
		mkdir bin
	fi
	cd bin
	ln -s $CUR_PATH/cweb/cweave cweave
	ln -s $CUR_PATH/cweb/ctangle ctangle
fi

cd ..
PATH=$CUR_PATH/bin:$PATH
export PATH

echo "============================================================"
echo "* Build Stanford GraphBase"
echo "============================================================"

cd ./sgb
if [ ! -f ./Makefile ]
	then
	echo "Unable to create sgb library. This library is used by gt-itm and so for scenario generators. If you already have sgblib (possible if you are on solaris,sunos or freebsd platforms) you may still be able to run gt-itm. so continuing.."
else
	echo "Making sgb"
	if make tests
	then
                if [ -f libgb.a ] ; then
                        rm -f ../gt-itm/lib/libgb.a
                        cp libgb.a ../gt-itm/lib/libgb.a
                else 
                        echo "* Wierd: sgb said it has been built but we can't find libgb.a!
"
                        exit -1
                fi
	else
		echo "Unable to create sgb library, but it's optional, so continuing..."
	fi
fi

cd ..

# Compile and install gt-itm & sgb2ns

echo "============================================================"
echo "* Build GT-ITM"
echo "============================================================"

if [ -f ./gt-itm/lib/libgb.a ]
then
 if [ ! -f ./gt-itm/src/Makefile ] 
    then
    echo "ns-alline is unable to install gt-itm sgb2ns for you, please install"
    echo "them manually. You can't run scenario generator without gt-itm"
    echo "and sgb2ns. But it will not affect you use ns, so continue ..."
 else
    cd ./gt-itm/src
    if make
    then
      echo "gt-itm has been installed successfully."
    fi
    
    cd ../sgb2ns
    if make
    then
      echo "sgb2ns has been installed successfully."
    fi
   cd ../../
 fi
else
    echo "sgb lib not found. gt-itm & sgb2ns could not be installed. Continuing.."
fi

# Build zlib

echo "============================================================"
echo "* Build zlib"
echo "============================================================"

cd ./zlib-1.1.3

if ./configure --exec-prefix=../ --prefix=../
then
	if make
	then
		echo "Zlib has been installed successfully."
	else
		warn "Zlib make failed, but it's optional Continue ..."
	fi
else
	warn "Zlib-1.1.3 configuration failed, but it's optional, so continuing ..."
fi

cd ../

# Build Tcl8.3.2

echo "============================================================"
echo "* Build tcl$TCLVER"
echo "============================================================"

cd ./tcl$TCLVER/unix
if [ -f Makefile ] ; then 
	make distclean
fi

blame='Tcl is not part of the ns project.  Please see www.Scriptics.com
to see if they have a fix for your platform.'
./configure --enable-gcc --disable-shared --prefix=$CUR_PATH || die "tcl8.3.2 configuration failed! Exiting ..."
if make 
then 
	echo "tcl$TCLVER make succeeded."
	make install || die "tcl$TCLVER installation failed."
	echo "tcl$TCLVER installation succeeded."
	cp ../generic/*.h ../../include
else
	echo "tcl$TCLVER make failed! Exiting ..."
	echo "For problems with Tcl/Tk see http://www.scriptics.com"
	exit
fi

cd ../../

# compile and install tk

echo "============================================================"
echo "* Build Tk$TKVER"
echo "============================================================"

cd ./tk$TKVER/unix
if [ -f Makefile ] ; then
	make distclean
fi

blame='Tk is not part of the ns project.  Please see www.Scriptics.com
to see if they have a fix for your platform.'
./configure --enable-gcc --disable-shared --prefix=$CUR_PATH || die "tk8.3.2 configuration failed! Exiting ..."
if make 
then
	echo "tk$TKVER build succeeded."
	make install || die "tk$TKVER installation failed."
	echo "tk$TKVER installation succeeded."
else
	echo "tk$TKVER make failed! Exiting ..."
	echo "For problems with Tcl/Tk see http://www.scriptics.com"
	exit
fi

cd ../../

#
# Since our configures search for tclsh in $PATH, the following 
# is needed. This is necessary for otcl/tclcl/ns/nam
#
PATH=$CUR_PATH/tcl$TCLVER/unix:$CUR_PATH/tk$TKVER/unix:$PATH
export PATH
LD_LIBRARY_PATH=$CUR_PATH/tcl$TCLVER/unix:$CUR_PATH/tk$TKVER/unix:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

# Build otcl

echo "============================================================"
echo "* Build OTcl-$OTCLVER"
echo "============================================================"

cd ./otcl-$OTCLVER

blame='Please check http://www.isi.edu/nsnam/ns/ns-problems.html
for common problems and bug fixes.'
./configure || die "otcl-$OTCLVER configuration failed! Exiting ..."

if make 
then
	echo "otcl-$OTCLVER has been installed successfully."
else
	echo "otcl-$OTCLVER make failed! Exiting ..."
	echo "See http://www.isi.edu/nsnam/ns/ns-problems.html for problems"
	exit
fi

cd ..

# Build tclcl

echo "============================================================"
echo "* Build Tclcl-$TCLCLVER"
echo "============================================================"

cd ./tclcl-$TCLCLVER

./configure || die "tclcl-$TCLCLVER configuration failed! Exiting ..."

if make
then
	echo "tclcl-$TCLCLVER has been installed successfully."
else
	echo "tclcl-$TCLCLVER make failed! Exiting ..."
	echo "See http://www.isi.edu/nsnam/ns/ns-problems.html for problems"
	exit
fi	

cd ../

# John's hack
test -f ./otcl-$OTCLVER/libotcl.a && rm ./otcl-$OTCLVER/libotcl.so

echo "============================================================"
echo "* Build ns-$NSVER"
echo "============================================================"

cd ./ns-$NSVER
./configure || die "Ns configuration failed! Exiting ..."

if make
then
	echo " Ns has been installed successfully." 
else
	echo "Ns make failed!"
	echo "See http://www.isi.edu/nsnam/ns/ns-problems.html for problems"
	exit
fi

cd ../

# Build nam

echo "============================================================"
echo "* Build nam-$NAMVER"
echo "============================================================"

cd ./nam-$NAMVER

./configure || die "Nam configuration failed! Exiting ..."

if make
then 
    echo "Nam has been installed successfully."
else
    echo "Nam make failed! Continue ..."
    echo "See http://www.isi.edu/nsnam/ns-problems.html for problems"
fi

cd ../

# Install nam, ns, xgraph into bin

if [ ! -d bin ] ; then
    mkdir bin
fi

cd bin

ln -s $CUR_PATH/ns-$NSVER/ns ns

if test -x $CUR_PATH/nam-$NAMVER/nam
then
    ln -s $CUR_PATH/nam-$NAMVER/nam nam
else
    echo "Please compile your nam separately."
fi

if test -x $CUR_PATH/xgraph-$XGRAPHVER/xgraph
then
    ln -s $CUR_PATH/xgraph-$XGRAPHVER/xgraph xgraph
else
    echo "Please compile your xgraph separately."
fi

if test -x $CUR_PATH/gt-itm/bin/sgb2ns
then 
    ln -s $CUR_PATH/gt-itm/bin/sgb2ns sgb2ns
    ln -s $CUR_PATH/gt-itm/bin/sgb2hierns sgb2hierns
    ln -s $CUR_PATH/gt-itm/bin/sgb2comns sgb2comns
    ln -s $CUR_PATH/gt-itm/bin/itm itm
    ln -s $CUR_PATH/gt-itm/bin/sgb2alt sgb2alt
    ln -s $CUR_PATH/gt-itm/bin/edriver edriver
else
    echo "Please compile your gt-itm & sgb2ns separately."
fi

echo "Ns-allinone package has been installed successfully."
echo "Here are the installation places:"
echo "tcl$TCLVER:	$CUR_PATH/{bin,include,lib}"
echo "tk$TKVER:		$CUR_PATH/{bin,include,lib}"
echo "otcl:		$CUR_PATH/otcl-$OTCLVER"
echo "tclcl:		$CUR_PATH/tclcl-$TCLCLVER"
echo "ns:		$CUR_PATH/ns-$NSVER/ns"

if [ -x $CUR_PATH/nam-$NAMVER/nam ]
then
echo "nam:	$CUR_PATH/nam-$NAMVER/nam"
fi

if [ -x $CUR_PATH/xgraph-$XGRAPHVER/xgraph ]
then
echo "xgraph:	$CUR_PATH/xgraph-$XGRAPHVER"
fi
if [ -x $CUR_PATH/gt-itm/bin/sgb2ns ] 
then
echo "gt-itm:   $CUR_PATH/itm, edriver, sgb2alt, sgb2ns, sgb2comns, sgb2hierns"
fi

echo ""
echo "----------------------------------------------------------------------------------"
echo ""
echo "Please put $CUR_PATH/bin:$CUR_PATH/tcl$TCLVER/unix:$CUR_PATH/tk$TKVER/unix" 
echo "into your PATH environment; so that you'll be able to run itm/tclsh/wish/xgraph."
echo ""
echo "IMPORTANT NOTICES:"
echo ""
echo "(1) You MUST put $CUR_PATH/otcl-$OTCLVER, $CUR_PATH/lib, "
echo "    into your LD_LIBRARY_PATH environment variable."
echo "    If it complains about X libraries, add path to your X libraries "
echo "    into LD_LIBRARY_PATH."
echo "    If you are using csh, you can set it like:"
echo "		setenv LD_LIBRARY_PATH <paths>"
echo "    If you are using sh, you can set it like:"
echo "		export LD_LIBRARY_PATH=<paths>"
echo ""
echo "(2) You MUST put $CUR_PATH/tcl$TCLVER/library into your TCL_LIBRARY environmental"
echo "    variable. Otherwise ns/nam will complain during startup."
echo ""
echo "(3) [OPTIONAL] To save disk space, you can now delete directories tcl$TCLVER "
echo "    and tk$TKVER. They are now installed under $CUR_PATH/{bin,include,lib}"
echo ""
echo "After these steps, you can now run the ns validation suite with"
echo "cd ns-$NSVER; ./validate"
echo ""
echo "For trouble shooting, please first read ns problems page "
echo "http://www.isi.edu/nsnam/ns/ns-problems.html. Also search the ns mailing list archive"
echo "for related posts." 
echo ""

exit 0

