#!/bin/sh

# Make-a-wish   (aka  Tcl-my-fancy)
#
# Tom Poindexter    tpoind@advtech.uswest.com

if [ "`echo -n hello`" = "hello" ]
then
  # it's a bsd echo
  EN=-n
  EL=""
else
  # it's a sysv echo
  EN=""
  EL='\c'
fi


# ask what to make: plain or extended, tcl or wish or both

echo
echo "Make-a-wish  (aka Tcl-my-fancy)"
echo
echo "Tom Poindexter"
echo "tpoind@advtech.uswest.com"
echo

EXTCL=""
while [ "$EXTCL" = "" ]
do
  echo
  echo "Do you want to build interpreter(s) with"
  echo $EN "standard Tcl or with Extended Tcl? (s/e) $EL"
  read EXTCL
  case $EXTCL in
    s*|S*)
	EXTCL=S
	;;
    e*|E*)
	EXTCL=E
	;;
    *)
	echo "what?"
	EXTCL=""
	;;
  esac
done

INTERPS=""
while [ "$INTERPS" = "" ]
do
  echo
  echo $EN "Do you want to build a tcl shell, wish, or both? (t/w/b) $EL"
  read INTERPS
  case $INTERPS in
    t*|T*)
	INTERPS=T
	;;
    w*|W*)
	INTERPS=W
	;;
    b*|B*)
	INTERPS=B
	;;
    *)
	echo "what?"
	INTERPS=""
	;;
  esac
done

# ask for names of interpreters
TCL_EXEC=""
WISH_EXEC=""
# set default names
if [ "$EXTCL" = "E" ]
then
  TCL_DEF=tcl
  WISH_DEF=wishx
else
  TCL_DEF=tclsh
  WISH_DEF=wish
fi

if [ "$INTERPS"  = "T" -o "$INTERPS" = "B" ]
then
  echo $EN "What do you want to name your Tcl shell (default=${TCL_DEF})? $EL"
  read ANS
  TCL_EXEC=${ANS:-$TCL_DEF}
fi
if [ "$INTERPS"  = "W" -o "$INTERPS" = "B" ]
then
  echo $EN "What do you want to name your Wish shell (default=${WISH_DEF})? $EL"
  read ANS
  WISH_EXEC=${ANS:-$WISH_DEF}
fi


# try to find tcl and tk source directories

# look below here
DIR_LEVEL=..
echo
echo "looking for Tcl/Tk/TclX libraries and extensions...."

LIB_TEST=`find $DIR_LEVEL -name 'lib*.a' -print`

if [ "$LIB_TEST" = "" ] 
then
  echo
  echo "oops! no libraries found in $DIR_LEVEL"
  echo "bailing out!"
  exit
fi

# now find libtcl.a, libtk.a, and maybe libtclx.a libtkx.a

LIB_LIST=""
TCLLIB=""
TKLIB=""
TCLXLIB=""
TKXLIB=""
TCL_DIR=""
TK_DIR=""
TCLX_DIR=""
IFS_SAVE="$IFS"

for L in $LIB_TEST
do
  # use IFS and set to break out directory paths
  IFS='/'
  case $L in
    */tclmaster/lib/libtcl.a)
	# ignore, this is just a copy of Tcl libtcl.a
	;;
    */tkmaster/lib/libtk.a)
	# ignore, this is just a copy of Tk libtk.a
	;;
    */tcl?.?/libtcl.a)
	if [ ! "$TCLLIB" ]
	then
	  set $L
	  TDIR=$DIR_LEVEL/$2
	  echo 
	  echo "Found libtcl.a as $L"
	  echo $EN "is $TDIR the correct Tcl directory (y/n) $EL"
  	  read ANS
  	  if [ "$ANS" = "y" -o "$ANS" = "Y" ]
	  then
	    TCLLIB=$L
	    set $L
	    TCL_DIR=$TDIR
	  fi
	fi
	;;
    */tk?.?/libtk.a)
	if [ ! "$TKLIB" ]
	then
	  set $L
	  TDIR=$DIR_LEVEL/$2
	  echo 
	  echo "Found libtk.a as $L"
	  echo $EN "is $TDIR the correct Tk directory  (y/n) $EL"
  	  read ANS
  	  if [ "$ANS" = "y" -o "$ANS" = "Y" ]
	  then
	    TKLIB=$L
	    set $L
	    TK_DIR=$TDIR
	  fi
	fi
	;;
    */tclX?.?*/tclmaster/lib/libtclx.a)
	if [ ! "$TCLXLIB" -a "$EXTCL" = "E" ]
	then
	  set $L
	  TDIR=$DIR_LEVEL/$2
	  echo 
	  echo "Found libtclx.a as $L"
	  echo $EN "is $TDIR the correct TclX directory and TclX lib (y/n) $EL"
  	  read ANS
  	  if [ "$ANS" = "y" -o "$ANS" = "Y" ]
	  then
	    TCLXLIB=$L
	    set $L
	    TCLX_DIR=$TDIR
	  fi
	fi
	;;
    */tclX?.?*/tkmaster/lib/libtkx.a)
	if [ ! "$TKXLIB"  -a "$EXTCL" = "E" ]
	then
	  set $L
	  TDIR=$DIR_LEVEL/$2
	  echo 
	  echo "Found libtkx.a as $L"
	  echo $EN "is $TDIR the correct TkX directory and TkX lib (y/n) $EL"
  	  read ANS
  	  if [ "$ANS" = "y" -o "$ANS" = "Y" ]
	  then
	    TKXLIB=$L
	  fi
	fi
	;;
    *)
	# other libs are candidates for extensions
	LIB_LIST="$LIB_LIST $L"
	;;
  esac
done

IFS="$IFS_SAVE"



# verify that the tcl/tk/tclX libs where found

if [ ! "$TCLLIB" ]
then
  echo
  echo "oops! didn't find libtcl.a"
  echo "bailing out"
  exit
fi
if [ "$INTERP" = "W" -o "$INTERP" = "B" -a ! "$TKLIB" ]
then
  echo
  echo "oops! didn't find libtk.a"
  echo "bailing out"
  exit
fi


if [ "$EXTCL" = "E" ]
then
  if [ ! "$TCLXLIB" ]
  then
    echo
    echo "oops! didn't find libtclx.a"
    echo "bailing out"
    exit
  fi
  if [ "$INTERP" = "W" -o "$INTERP" = "B" -a ! "$TKXLIB" ]
  then
    echo
    echo "oops! didn't find libtkx.a"
    echo "bailing out"
    exit
  fi
fi




# now ask which extensions to add in

echo
echo

INIT_LIST=""
EXT_LIBS=""
for E in $LIB_LIST
do
  echo
  echo "Make-a-wish found: $E  "
  echo $EN "Is this a Tcl/Tk extension you want to add (y/n) $EL"
  read ANS
  if [ "$ANS" = "y" -o "$ANS" = "Y" ]
  then
    EXT_LIBS="$EXT_LIBS $E"
    echo
    echo "   trying to find the Init function..."
    INIT=""
    INIT=`nm $E 2>/dev/null | egrep "_Init$" \
	  | awk '$2=="T" {print substr($3,2)}' | head -1`
    echo
    if [ "$INIT" ]
    then
      echo "   ...found     $INIT"
      echo
      echo "   If this is the Init function, press return.  Otherwise,"
    else
      INIT="$E.missing.init"
      echo "   Didn't find anything that looks like xxx_Init, so you'll have to"
    fi
    echo $EN "   type the name of the Init function: $EL"
    ANS=""
    read ANS
    if [ "$ANS" = "y" -o "$ANS" = "Y" ]
    then
      ANS=""
    fi
    INIT_LIST="$INIT_LIST ${ANS:-$INIT}"
    echo
  fi
done

# any other libs?
MORE_LIBS=""
echo
echo
echo "If any of the extensions require additional libraries not"
echo "already mentioned, please enter those now, if none then just"
echo "press return:"
echo
read MORE_LIBS
EXT_LIBS="$EXT_LIBS $MORE_LIBS"


echo
echo "Making sed script"

SEDHDR='/if.*Tcl[X]*_Init.*interp.*==.*TCL_ERROR/{
n
n
n
i\
\'

SEDBDY1='\ \ \ \ if ('

SEDBDY2='(interp) == TCL_ERROR) {\
\ \ \ \     return TCL_ERROR;\
\ \ \ \ }\'

SEDTLR='
}'


echo "$SEDHDR"                              >mawinit.sed
for I in $INIT_LIST
do
  echo  "$SEDBDY1 $I $SEDBDY2"             >>mawinit.sed
done

echo  "$SEDTLR"                            >>mawinit.sed


echo
echo "Making makefile target and libraries include file"
echo "TCL_DIR = $TCL_DIR"		           >mawtarget.mk
echo "TK_DIR = $TK_DIR"		   	          >>mawtarget.mk
echo "TCLX_DIR = $TCLX_DIR"		          >>mawtarget.mk
echo "TCL_LIBS = $TCLXLIB $TCLLIB"                >>mawtarget.mk
echo "TK_LIBS = $TKXLIB $TKLIB $TCLXLIB $TCLLIB"  >>mawtarget.mk
echo "EXT_LIBS = $EXT_LIBS"                       >>mawtarget.mk

echo "WISH_EXEC = $WISH_EXEC   "                  >>mawtarget.mk
echo "TCL_EXEC = $TCL_EXEC     "                  >>mawtarget.mk
if [ "$EXTCL" = "E" ]
then
  echo "TCL_INIT_OBJ=tclXAppInit.o"               >>mawtarget.mk
  echo "TK_INIT_OBJ=tkXAppInit.o"                 >>mawtarget.mk
else
  echo "TCL_INIT_OBJ=tclAppInit.o"                >>mawtarget.mk
  echo "TK_INIT_OBJ=tkAppInit.o"                  >>mawtarget.mk
fi
echo "all: $TCL_EXEC $WISH_EXEC"                  >>mawtarget.mk

echo
echo "Making initial tclflags.mk"
egrep '^MATH_LIBS'         $TCL_DIR/Makefile    >tclflags.mk
egrep '^AC_FLAGS'          $TCL_DIR/Makefile   >>tclflags.mk
egrep '^PROTO_FLAGS'       $TCL_DIR/Makefile   >>tclflags.mk
egrep '^MEM_DEBUG_FLAGS'   $TCL_DIR/Makefile   >>tclflags.mk
egrep '^CFLAGS'            $TCL_DIR/Makefile   >>tclflags.mk
egrep '^X11_INCLUDES'      $TK_DIR/Makefile    >>tclflags.mk
egrep '^X11_LIB_SWITCHES'  $TK_DIR/Makefile    >>tclflags.mk
egrep '^LIBS'              $TK_DIR/Makefile | \
    sed -e "s@libtk.a@$TK_DIR/libtk.a@p"       >>tclflags.mk



echo
echo
echo "Review your Makefile, mawtarget.mk, tclflags.mk, and mawinit.sed "
echo "If values are reasonable, try 'make'"
echo

exit
