#
#
# build_shlib for Linux Ver.0.1 Wed Jun  1 11:53:37 MESZ 1994
#
# Jens Claussen <claussen@db.fmi.uni-passau.de> solved the shlib problem
# This shell script is wrapped around his log and should make
# things easier. Hermann Dunkel <hedu@cul-ipn.uni-kiel.de>
#
# make_shlib.old is much more readable to understand the idea
# this script is full with if's etc.

# !!! tools-2.x (currently x = 11) must be installed !!!

# Warning: the library in its current extent is not registered for
# address space. So, especially with libandrew.so and libUil.so, there
# could theoretically occur problems!

# editable variables
WXDIR=/usr/src/wx
GUI=xview GUI_SUFFIX=_ol
GUI=motif GUI_SUFFIX=_motif
DEST_DIR=/usr/local/lib
# end of editable variables, I hope :-)

# 1. preparation
VERSION=1.5	# suffix for the shared lib


if [ ! -d /usr/dll/bin ]
then
	echo "Could not find /usr/dll/bin!"
	echo "Is tools-2.x x>=11 installed ?"
	exit 1
fi
if [ ! -d $WXDIR ]
then
	echo "WXDIR $WXDIR not found!"
	exit 1
fi

PATH=/usr/dll/bin:$PATH
export PATH

#  prepare jumpdir

JUMP_DIR=$WXDIR/jump_$GUI
JUMP_LIB=libwx$GUI_SUFFIX
export JUMP_LIB JUMP_DIR

if [ ! -d $JUMP_DIR ]
then
	mkdir $JUMP_DIR
fi

cd $JUMP_DIR

echo "WXDIR=$WXDIR GUI=$GUI"
echo "JUMP_DIR=$JUMP_DIR JUMP_LIB=$JUMP_LIB"
echo

#
# find the existing libs, checks for two different libX...
#
LIB_C=/usr/lib/libc.sa
LIB_M=/usr/lib/libm.sa

LIB_X11=/usr/lib/libX11.sa
if [ ! -f $LIB_X11 ]; then 
	LIB_X11=/usr/X386/lib/libX11.sa
fi
if [ ! -f $LIB_X11 ]; then 
	echo "Could not find libX11.sa"; exit
fi
LIB_XT=/usr/lib/libXt.sa
if [ ! -f $LIB_XT ]; then 
	LIB_XT=/usr/X386/lib/libXt.sa
fi
if [ ! -f $LIB_XT ]; then 
	echo "Could not find libXt.sa"; exit
fi

echo "Using $LIB_C $LIB_M"
echo "      $LIB_X11 $LIB_XT"

if [ $GUI = "motif" ]
then
	LIB_XM=/usr/lib/libXm.sa
	if [ ! -f $LIB_XM ]; then 
		LIB_XM=/usr/X386/libXm.sa
	fi
	if [ ! -f $LIB_XM ]; then 
		echo "Could not find libXm.sa"; exit
	fi
	echo "      $LIB_XM"
else
	# openwin libs
	LIB_XVIEW=/usr/openwin/lib/libxview.sa
	LIB_OLGX=/usr/openwin/lib/libolgx.sa
	if [ ! -f $LIB_XVIEW ]; then 
		echo "could not find libxview.sa";exit
	fi
	if [ ! -f $LIB_OLGX ]; then
		echo "could not find libolgx.sa";exit
	fi
	echo "      $LIB_XVIEW $LIB_OLGX"
fi

if [ -d /usr/lib/gcc-lib/i486-linux/2.5.8 ]
then
	LIB_GCC=/usr/lib/gcc-lib/i486-linux/2.5.8
elif [ -d /usr/lib/gcc-lib/i486-linux/2.5.7 ]
then
	LIB_GCC=/usr/lib/gcc-lib/i486-linux/2.5.7
else
	echo "could not find /usr/lib/gcc-lib/..."
	echo "Only know of 2.5.7 2.5.8"
	exit 
fi
echo "LIB_GCC is $LIB_GCC"

#
#
echo "The following files are in JUMP_DIR"
ls .
echo "Press RETURN to continue"
read

laststage=""

while true
do
echo "p)reconditions q)uit"
echo "l)ist files in JUMP_DIR cj) clean jump* cl) clean lib* cs) clean *.s"
echo
echo "1) compile stage 1: make clean; make"
echo "1a)retry   stage 1: make"
echo "2) build jump.import"
echo "3) compile stage 2: make clean; make"
echo "3a)retry   stage 2: make"
echo "4) build shared library"
echo "5) install in $DEST_DIR/"
if [ "$laststage"!="" ]
then
echo "laststage was: $laststage"
fi

read cmd

case $cmd in
    p) 
	echo "Read README.build_shlib to get the idea."
	echo "add -D_LINUX_DLL -B/usr/dll/jump/ to OPTIONS in the makefile src/x/makefile.unx"
	echo "(src/base/makefile.unx should inherit these)"
	echo "The traling / in -B/usr/dll/jump/ is required!!!"
	echo "You should save your existing WX_DIR/lib/libwx$GUI_SUFFIX.a"
	echo "else you have to rebuild it after building the shared libs."
		;;
    cs) rm -f *.s; echo "In JUMP_DIR are:";ls . ;;
    cl) rm -f lib*; echo "In JUMP_DIR are:";ls . ;;
    cj) rm -f jump*; echo "In JUMP_DIR are:";ls . ;;
    l) echo "In JUMP_DIR are:"; ls . ;;
    *1) echo "compile stage 1, logged to make.log" 
	(cd $WXDIR/src/x; 
	 make -f makefile.unx clean$GUI_SUFFIX; 
	 make -f makefile.unx $GUI ) 2>&1 | tee make.log
	more make.log
	;;
    *1a) echo "compile stage 1, retry, logged to make.log"
	(cd $WXDIR/src/x; 
	 make -f makefile.unx $GUI ) 2>&1 | tee make.log
	more make.log
	;;
    *2) echo "generate list of imported/ignored Symbols (jump.import)"
getvars
getfuncs

echo "$LIB_C"
nm --no-cplus -o $LIB_C |grep __GOT_ | sed 's/__GOT_/_/' >jump.import
echo "$LIB_X"
nm --no-cplus -o $LIB_X |grep __GOT_ | sed 's/__GOT_/_/' >>jump.import
echo "$LIB_XT"
nm --no-cplus -o $LIB_XT |grep __GOT_ | sed 's/__GOT_/_/' >>jump.import
echo "$LIB_M"
nm --no-cplus -o $LIB_M |grep __GOT_ | sed 's/__GOT_/_/' >>jump.import
#	nm --no-cplus -o /usr/lib/libg++.sa |grep __GOT_ | sed 's/__GOT_/_/' >>jump.import

if [ $GUI = "motif" ]; then

echo "$LIB_XM"
nm --no-cplus -o $LIB_XM |grep __GOT_ | sed 's/__GOT_/_/' >>jump.import

else
echo "$LIB_XVIEW"
nm --no-cplus -o $LIB_XVIEW |grep __GOT_ | sed 's/__GOT_/_/' >>jump.import
echo "$LIB_OLGX"
nm --no-cplus -o $LIB_OLGX |grep __GOT_ | sed 's/__GOT_/_/' >>jump.import

fi
	;;
    *3) echo "compile stage 2: make clean;make"
	(cd $WXDIR/src/x; 
	 make -f makefile.unx clean$GUI_SUFFIX; 
	 make -f makefile.unx $GUI ) 2>&1 | tee make.log
	more make.log
	;;
    *3a) echo "retry stage 2: make"
	(cd $WXDIR/src/x; 
	 make -f makefile.unx $GUI ) 2>&1 | tee make.log
	more make.log
	;;
    *4) echo "mkimage"
	cd $JUMP_DIR
# getsize, build the image and lib

getsize >jump.vars-new
mv -f jump.vars jump.vars-old
mv jump.vars-new jump.vars

# create image and stubs

# (I'm not shure about the table size (-j) I used, maybe this is wrong !!)

if [ $GUI = "motif" ]; then

mkimage -f -l libwx_motif -v $VERSION -a 0x63400000 -g 0x2000 -j 0x4000 \
-- -L$LIB_GCC /usr/src/wx/lib/libwx_motif.a -lXm -lXt -lX11 -lg++ -lm -lgcc -lc 
mkstubs -l libwx_motif -v $VERSION -a 0x63400000 -g 0x2000 -j 0x4000 \
-- libwx_motif

else

mkimage -f -l libwx_ol -v $VERSION -a 0x63400000 -g 0x2000 -j 0x4000 \
-- -L$LIB_GCC -L/usr/openwin/lib /usr/src/wx/lib/libwx_ol.a \
-lxview -lolgx -lX11 -lg++ -lm -lgcc -lc
mkstubs -l libwx_ol -v $VERSION -a 0x63400000 -g 0x2000 -j 0x4000 \
-- libwx_ol

fi
	;;
    *5) echo "Installing in $DEST_DIR (using mv)"
		mv libwx$GUI_SUFFIX.so.$VERSION $DEST_DIR
		mv libwx$GUI_SUFFIX.sa $DEST_DIR
	 	ldconfig -n $DEST_DIR
	;;
    *q) echo "Don't forget -D_LINUX_DLL in your project!"
	echo "And add the code from linux_main.cc to it."
	exit 0
	;;
     *) echo "wrong command"
	cmd=laststage # save last stage
	;;
	
esac
echo ""

laststage=$cmd
done # main loop
exit

# this does not show any reasonable results
#verify-shlib -l libwx_ol.so.$VERSION /usr/X386/lib/libX11.sa /usr/lib/libc.sa \
#/usr/lib/libm.sa /usr/openwin/lib/libolgx.sa /usr/openwin/lib/libxview.sa 

# install libwx*.sa and libwx*.so* to an appropriate place and run
# /etc/ldconfig

# prepare your applications by adding -D_LINUX_DLL to compiler options
# and add the following lines to one file of each of your
# applications:

