#!/bin/sh

# Make-a-wish   (aka  Tcl-my-fancy)
# version 1.3, November 1995
#
# Tom Poindexter    tpoindex@nyx.cs.du.edu

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

# check find
if [ `find . -follow  -name who.cares -print ` >/dev/null 2>&1 ]
then
  # can follow links
  FOLLOW="-follow"
else
  # cannot follow links
  FOLLOW=""
fi



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

echo
echo "Make-a-wish  (aka Tcl-my-fancy)"
echo "version 1.3"
echo
echo "Tom Poindexter"
echo "tpoindex@nyx.cs.du.edu"
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...."

# "-follow" to follow symbolic links
LIB_TEST=`find $DIR_LEVEL $FOLLOW  -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=""
TCL_MAIN=""
TK_MAIN=""
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
	    TCL_DIR=$TDIR
	  fi
	fi
	;;
    */tk?.?/libtk.a)
	if [ \( "$INTERPS" = "W" -o "$INTERPS" = "B" \) -a ! "$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
	    TK_DIR=$TDIR
	  fi
	fi
	;;
    */tclX?.?*/src/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
	    TCLX_DIR=$TDIR
	  fi
	fi
	;;
    */tclX?.?*/tksrc/libtkx.a)
	if [ \( "$INTERPS" = "W" -o "$INTERPS" = "B" \) -a ! "$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 [ \( "$INTERPS" = "W" -o "$INTERPS" = "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 [ \( "$INTERPS" = "W" -o "$INTERPS" = "B" \) -a ! "$TKXLIB" ]
  then
    echo
    echo "oops! didn't find libtkx.a"
    echo "bailing out"
    exit
  fi
fi


# verify AppInit files

if [ "$EXTCL" = "E" ]
then
  if [ \( "$INTERPS" = "T" -o "$INTERPS" = "B" \) -a ! -f $TCLX_DIR/src/tclXAppInit.c ]
  then
    echo
    echo "oops ! didn't find $TCLX_DIR/src/tclXAppInit.c"
    echo "bailing out"
    exit
  else
    TCL_MAIN=$TCLX_DIR/src/tclXAppInit.o
  fi
  if [ \( "$INTERPS" = "W" -o "$INTERPS" = "B" \) -a ! -f $TCLX_DIR/tksrc/tkXAppInit.c ]
  then
    echo
    echo "oops ! didn't find $TK_DIR/tksrc/tkXAppInit.c"
    echo "bailing out"
    exit
  else
    TK_MAIN=$TCLX_DIR/tksrc/tkXAppInit.o
  fi
else
  if [ \( "$INTERPS" = "T" -o "$INTERPS" = "B" \) -a ! -f $TCL_DIR/tclAppInit.c ]
  then
    echo
    echo "oops ! didn't find $TCL_DIR/tclAppInit.c"
    echo "bailing out"
    exit
  else
    TCL_MAIN=$TCL_DIR/tclAppInit.o
  fi
  if [ \( "$INTERPS" = "W" -o "$INTERPS" = "B" \) -a ! -f $TK_DIR/tkAppInit.c ]
  then
    echo
    echo "oops ! didn't find $TK_DIR/tkAppInit.c"
    echo "bailing out"
    exit
  else
    TK_MAIN=$TK_DIR/tkAppInit.o
  fi
fi


# test what nm output looks like
NMOUT=`nm $TCLLIB | grep FUNC | head -1`
if [ "$NMOUT" ]
then
  # sysv.4 style nm
  NMAWK='/FUNC/ {print substr($NF,2)}'
else
  # regular nm
  NMAWK='$2=="T" {if (substr($3,1,1)=="_") print substr($3,2); else print $3}'
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 "$NMAWK" | 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"


# disclose which main() 
echo
echo
if [ "$INTERPS" = "T" -o "$INTERPS" = "B" ]
then
  echo "Using as $TCL_EXEC main(): $TCL_MAIN"
fi
echo
if [ "$INTERPS" = "W" -o "$INTERPS" = "B" ]
then
  echo "Using as $WISH_EXEC main(): $TK_MAIN"
fi


# done asking questions
echo
echo
echo "Making sed scripts"

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

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

SEDBDY1='\ \ \ \ if ('

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

SEDTLR='
}'


echo "$TCLSEDHDR"                           >mawtcl.sed
echo "$TKSEDHDR"                            >mawtk.sed
for I in $INIT_LIST
do
  echo  "$SEDBDY1 $I $SEDBDY2"             >>mawtcl.sed
  echo  "$SEDBDY1 $I $SEDBDY2"             >>mawtk.sed
done

echo  "$SEDTLR"                            >>mawtcl.sed
echo  "$SEDTLR"                            >>mawtk.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 \$(TCLXLIBS)"  \
						  >>mawtarget.mk
echo "TK_LIBS  = $TKXLIB $TKLIB $TCLXLIB $TCLLIB \$(TKXLIBS)" \
			 			  >>mawtarget.mk
echo "EXT_LIBS = $EXT_LIBS"                       >>mawtarget.mk

echo "WISH_EXEC = ${WISH_EXEC:-nowish}   "        >>mawtarget.mk
echo "TCL_EXEC = ${TCL_EXEC:-notclsh}    "        >>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

if [ "$INTERPS" = "W" -o "$INTERPS" = "B" ]
then
  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
else
  echo "X11_INCLUDES = "			>>tclflags.mk
  echo "X11_LIB_SWITCHES = "			>>tclflags.mk
  echo "LIBS = "				>>tclflags.mk
fi

if [ "$EXTCL" = "E" ]
then
  cat  $TCLX_DIR/tksrc/tkxlibs.mk		>>tclflags.mk
  cat  $TCLX_DIR/src/tclxlibs.mk		>>tclflags.mk
else
  echo "TKXLIBS = "				>>tclflags.mk
  echo "TCLXLIBS = "				>>tclflags.mk
fi


echo
echo
echo "Review files: Makefile, mawtarget.mk, tclflags.mk, mawtcl.sed, mawtk.sed"
echo "If values are reasonable, try 'make'"
echo

exit
