#!/bin/sh

# invers_off failed on AIX
#--------------------------
#invers_on="[7m"
#invers_off="[27m"

#bold_on="[1m"
#bold_off="[22m"

# from GNU configure:
#--------------------------
if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
  # Stardent Vistra SVR4 grep lacks -e, says ghazi@caip.rutgers.edu.
  if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
    ac_n= ac_c='
' ac_t='	'
  else
    ac_n=-n ac_c= ac_t=
  fi
else
  ac_n= ac_c='\c' ac_t=
fi

tn="$invers_on                           TOAD INSTALL TOOL v1.6                               $invers_off"

# move to TOADs top level directory
old_pwd=`pwd`
build_prog=$0
build_dir=`echo $0 | sed "s%/build$%%"`

if test -d $build_dir; then
  cd $build_dir
  build_dir=`pwd`
fi

while true ; do
	clear
cat << EOF
$tn

$bold_on        TOAD - the simple and powerful C++ GUI toolkit for X Windows

                  Copyright (C) 1996-99 by Mark-Andr Hopf$bold_off

  [1] Compile TOAD
  [2] Select directories and install TOAD (experimental)
  [3] Create a nasty HTML reference for TOAD
  [4] Read the reference (experimental)

  [5] Remove all generated files

  [6] About
  [7] License
  [0] Quit

EOF
  echo $ac_n "$bold_on  Enter your choice:$bold_off " $ac_c
	read inp
	echo
	case $inp in
		1) clear
		   echo "$tn"
		   echo " "
		   echo "Building TOAD library..."
		   echo
		   if [ ! -r conf/MakefileLib ]; then
		   	 cd conf
		   	 ./configure
		   	 cd ..
		   fi
		   if [ ! -r src/Makefile ]; then
			echo "Can't create Makefile. Sorry..."
		   else
        if test ! -L include/toad ; then
          cd include
          ln -sf ../src toad
					if test $? -ne 0 ; then
            echo " "
            echo "Couldn't create link include/toad -> ../src. Aborted."
            echo "Maybe you should check the ownership and the file permissions."
            echo " "
						exit
					fi
          cd ..
        fi
	   		cd src
	   		if test -x /usr/X11R6/bin/makedepend; then
	   			echo "Updating dependencies with X11R6 makedepend..."
 	   	 		make -f Makefile depend
 	   	 	fi
		     	make -f Makefile
		     	cd ..
		   fi;;
    2) ls lib/libtoad.so.*.* &> /dev/null
       if test $? -ne 0 ; then
         echo "  You have to compile the library before installation."
       else
         while true ; do
		       clear
		       echo "$tn"
		       echo ""
		         cat << EOF
  Where would you like to install the library ?

  [1] "/usr/local/include/" and "/usr/local/lib/"
  [2] "/opt/include/" and "/opt/lib/"
  [3] "/usr/include/" and "/usr/lib/"
  [4] "/usr/X11/include/" and "/usr/X11/lib/"
  
  [5] "$HOME/include/" and "$HOME/lib/"
  
  [6] Somewhere else...

  [0] Abort
  
EOF
         echo $ac_n "$bold_on  Please enter a number:$bold_off " $ac_c
				 read inp
				 hdir=""
				 ldir=""
				 case $inp in
				 	0) break;;
				 	1)	hdir="/usr/local/include"
				 			ldir="/usr/local/lib";;
				 	2)  hdir="/opt/include"
				 			ldir="/opt/lib";;
				 	3)  hdir="/usr/include"
							ldir="/usr/lib";;
					4)	hdir="/usr/X11/include"
							ldir="/usr/X11/lib";;
					5)  hdir="$HOME/include"
							ldir="$HOME/lib";;
					t)  ldir="/tmp/toad/lib"
					    hdir="/tmp/toad/include";;
					6)	echo ""
							echo "  Please enter the path names for the TOAD installation:"
							echo ""
							echo $ac_n "$bold_on  header files (eg. /tmp/include) :$bold_off " $ac_c
							read hdir
							if test ! -z $hdir ; then
								echo $ac_n "$bold_on  library files (eg. /tmp/lib) :$bold_off " $ac_c
								read ldir
							fi;;
				 esac
				 if test ! -z $ldir -a ! -z $hdir ; then
				   clear
				   echo "$tn"
				   echo ""
				   echo "  Are you sure to install TOAD in the following locations?"
				   echo ""
				   echo "  header files  : $hdir"
				   echo "  library files : $ldir"
				   echo ""
				   echo $ac_n "$bold_on  Please enter y or n:$bold_off " $ac_c
				   read inp
				   if test $inp = "y" -o $inp = "Y" ; then
				      echo ""
				      echo "  Installing TOAD..."
				      install -d lib/ $ldir
				      # install -m 0755 lib/* $ldir
              cp -af lib/* $ldir
              chmod 0755 $ldir/*
				      install -d $hdir/toad
				      install -d $hdir/toad/X11
				      install -d $hdir/toad/dialogeditor
				      install -d $hdir/toad/dnd
				      install -d $hdir/toad/gadget
				      install -d $hdir/toad/gadgeteditor
				      install -d $hdir/toad/io
				      install -d $hdir/toad/lba
							install -d $hdir/toad/pen
							install -d $hdir/toad/util
				      cd src
				      hdr_files=`find . -name "*.hh"`
				      cd ..
				      for file in $hdr_files ; do
				        echo "installing $file"
				      	install -m 0644 "src/$file" "$hdir/toad/$file"
				      done
				      echo ""
				      echo "Done"
				      echo ""
				   fi
				   break;
				 fi
				 done
			 fi
			 ;;
		3) clear
       echo "$tn"
       echo ""
       echo "Creating HTML reference..."
       echo ""
       cd doc/reference/
       make;;
    4) clear
       echo "$tn"
       echo ""
       cd construction/html/
       make
       cd ../..
       construction/html/html doc/reference/index.html &> /dev/null &;;
    5) clear
       echo "$tn"
       echo " "
       echo "Removing all generated files..."
       echo

			 # use the Makefile to remove old files
		   current=`pwd`
		   for x in `find . -name Makefile` 
		   do
		     cd `echo $x|sed 's%/[^/][^/]*$%%'`
	       make -s clean 2> /dev/null
         cd $current
       done

       # other stuff
		   rm `find . -name '*~' -print` 2> /dev/null
		   rm `find . -name '*.bak' -print` 2> /dev/null
		   rm `find . -name '*.o' -print` 2> /dev/null
		   rm `find . -name '*.so' -print` 2> /dev/null
		   rm `find . -name 'a.out' -print` 2> /dev/null
		   rm `find . -name 'core' -print` 2> /dev/null
		   rm `find . -name '*.log' -print` 2> /dev/null
		   rm `find . -name 'DEADJOE' -print` 2> /dev/null

			 # clear configuration directory
		   rm conf/config.cache 2> /dev/null
		   rm conf/config.log 2> /dev/null
		   rm conf/config.status 2> /dev/null
		   rm conf/confdefs.h 2> /dev/null
		   rm -r conf/conftestdir 2> /dev/null
		   rm conf/MakefilePrg 2> /dev/null
		   rm conf/MakefileLib 2> /dev/null
			 rm -f conf/xtralibs.out
		   rm src/config.h 2> /dev/null

		   # remove library
		   rm -f lib/*.a 2> /dev/null
       rm -f lib/*.so 2> /dev/null
			 rm -f lib/*.so.* 2> /dev/null

			 # remove links for `diff'
			 rm -f include/toad 2> /dev/null

			 # clear `doc' directory
			 rm -f doc/*.aux doc/*.log doc/*.dvi 2> /dev/null;;

		6) clear; more README;;
		7) clear; more COPYING.LIB;;
		0) break;;
	esac
	echo
	echo $ac_n "$bold_on  Please hit enter to continue... $bold_off" $ac_c
	read inp
done
clear
echo "Leaving TOAD SETUP TOOL. Bye..."
echo
