#!/bin/sh

#
# (c) Copyright 1993 by Panagiotis Tsirigotis
#

#
# $Id: compile-src,v 1.5 1993/04/20 19:29:53 panos Exp panos $
#

script_name=`basename $0`

if test "$script_name" != "$0" -a "./$script_name" != "$0" ; then
   echo This script must be executed from the top level directory 
   exit 1
fi

#
# misc needs to precede sio in the list of libraries
#
lib_names="sio str"

catch_sigs="2 15"
inc=/usr/include

while test $# -gt 0
do
	option=$1 ; shift
	case $option in
		-os)
				if test $# -eq 0 ; then
					echo "Argument missing. Exiting..."
					exit 1
				fi
				osversion=$1 ;
				shift
				;;
		
      -libc)
            if test $# -eq 0 ; then
               echo "Argument missing. Exiting..."
               exit 1
            fi
            libc=$1
            shift
            if test ! -r $libc ; then
               echo "File is not readable: $libc"
               exit 1
            fi
            ;;

		-auto)
				autoconfig=yes
				;;

		-sf|-cf)
				if test $# -eq 0 ; then
					echo "Missing argument to $option. Exiting..."
					exit 1
				fi
					
				if test "$option" = "-sf" ; then
					v=yes
				else
					v=no
				fi

				specified_flags=yes
				flag=$1
				shift

				#
				# Line 1 has to do with the man pages (pstext and libraries)
				# Lines 3, 4 and 5 are for SIO.
				# Line 2 is for MISC.
				#
				case "$flag" in
					has_SB)										eval $flag=$v ;;
					has_mmap|has_memops|has_bcopy)		eval $flag=$v ;;
					has_isatty|has_bsdtty|has_sysvtty)	eval $flag=$v ;;
					has_onexit|has_atexit)					eval $flag=$v ;;
					*)	echo "Bad flag name: $flag. Exiting..." ; exit 1
				esac
				;;

		-verbose)
				verbose=yes
				;;
		#
		# THIS IS THE SCRIPT DOCUMENTATION
		#
		-help)
			echo "Usage: $script_name [options]"
			echo "Options:"
         echo "   -libc: specifies the location of the C library"
			echo "   -os osname: specifies the OS name and version"
			echo "	-auto: automatic configuration"
			echo "	-verbose: display info about configuration"
			echo "Possible arguments to -os:"
			echo "        sunos4, ultrix4"
			echo "   -sf flag or -cf flag:"
			echo "         set or clear flags that specify what facilities"
			echo "         are available from the operating system"
			echo "Possible arguments to -sf and -cf:"
			echo "         has_SB,"
			echo "         has_mmap, has_memops, has_bcopy, has_isatty,"
			echo "         has_bsdtty, has_sysvtty, has_onexit, has_atexit"
			echo "The -sf/-cf flags can be specified multiple times."
			echo "You can use the -os option to specify an operating system"
			echo "and then you can use -sf/-cf for further mods."
			exit 2
			;;

		*) echo "Bad argument: $1. Exiting..." ; exit 1
	esac
done

if test "$autoconfig" -a "$osversion" ; then
	echo "Only one of -auto, -os can be specified"
	exit 1
fi

if test ! "$osversion" -a ! "$autoconfig" ; then
	echo "I need to know the operating system you are using and the version."
	echo "I know about"
	echo "   SunOS 4.x (sunos4)"
	echo "   Ultrix 4.x (ultrix4)"
	echo "If your OS is among these, use the name in the parentheses, otherwise"
	echo "just hit <return> and I will try to figure out how to properly"
	echo -n "configure pstext for your system --> "
	read osversion
fi

#
# This if statement checks if we have a known osversion.
# If we don't, we set the autoconfig flag.
#
if test "$osversion" ; then
	case $osversion in
		sunos4|ultrix4) autoconfig= ;;
		*) echo "I don't know anything about this OS. Exiting..." ; exit 1
	esac
else
	autoconfig=yes
fi

#
# Get namelist of C library
#
if test "$autoconfig" ; then
   echo "Obtaining name list of C library"

   if test ! "$libc" ; then
      if test -r /lib/libc.a ; then
         libc=/lib/libc.a
      else
         if test -r /usr/lib/libc.a ; then
            libc=/usr/lib/libc.a
         else
            echo -n "Please specify the pathname of the C library -> "
            read libc
            if test ! "$libc" -o ! -r "$libc" ; then
               echo "Bad pathname. Exiting..."
               exit 1
            fi
         fi
      fi
   fi
   nmfile=/tmp/libc.nm.$$
   nm $libc > $nmfile 2>/dev/null
   if test $? -ne 0 ; then
      echo "Couldn't get the namelist of C library. Exiting..."
      exit 1
   fi
fi

#
# This is the autoconfiguration part. It works by setting appropriate flags
# for the C preprocessor (for example, -DHAS_MMAP)
#
if test "$specified_flags" -o "$autoconfig" ; then
	#
	# Check for mmap. Since it is not required, we do not try to find
	# if the system supports it.
	#
	case "$has_mmap" in
		yes)	v=-DHAS_MMAP ;;
		*)		v= ;;
	esac
	lib_defs="$lib_defs $v"

	case "$has_onexit" in
		yes)	v=-DHAS_ONEXIT ;;
		no)	v= ;;
		*)
				if test "$autoconfig" ; then
					if test "$verbose" ; then echo "Looking for on_exit" ; fi
					found=`grep on_exit $nmfile | grep T`
					if test "$found" ; then v=-DHAS_ONEXIT ; else v= ; fi 
				else
					v=
				fi
	esac
	lib_defs="$lib_defs $v"

	case "$has_atexit" in
		yes)	v=-DHAS_ATEXIT ;;
		no)	v= ;;
		*)
				if test "$autoconfig" ; then
					if test "$verbose" ; then echo "Looking for atexit" ; fi
					found=`grep atexit $nmfile | grep T`
					if test "$found" ; then v=-DHAS_ATEXIT ; else v= ; fi 
				else
					v=
				fi
	esac
	lib_defs="$lib_defs $v"

	case "$has_memops" in
		yes)	v=-DHAS_MEMOPS ;;
		no)	v= ;;
		*)
				if test "$autoconfig" ; then
					if test "$verbose" ; then echo "Looking for memcpy" ; fi
					found=`grep memcpy $nmfile | grep T`
					if test "$found" ; then v=-DHAS_MEMOPS ; else v= ; fi 
				else
					v=
				fi
	esac
	lib_defs="$lib_defs $v"

	case "$has_bcopy" in
		yes)	v=-DHAS_BCOPY ;;
		no)	v= ;;
		*)
				if test "$autoconfig" ; then
					if test "$verbose" ; then echo "Looking for bcopy" ; fi
					found=`grep bcopy $nmfile | grep T`
					if test "$found" ; then v=-DHAS_BCOPY ; else v= ; fi
				else
					v=
				fi
	esac
	lib_defs="$lib_defs $v"

	case "$has_isatty" in
		yes)	v=-DHAS_ISATTY ;;
		no)	v= ;;
		*)
				if test "$autoconfig" ; then
					if test "$verbose" ; then echo "Looking for isatty" ; fi
					found=`grep bcopy $nmfile | grep T`
					if test "$found" ; then v=-DHAS_ISATTY ; else v= ; fi
				else
					v=
				fi
	esac
	#
	# Since SIO cannot compile without the isatty function, if isatty is not 
	# in the C library, we have to provide our own. For this, we need to know 
	# if the system supports BSD or SysV tty handling.
	#
	if test ! "$osversion" -a ! "$v" ; then
		while test ! "$v"
		do
			echo "Is your OS based on System V or on BSD 4.x ? I am really"
			echo -n "interested in terminal handling. You can say sysv or bsd -->"
			read ans
			case $ans in
				sysv)	v=-DHAS_SYSVTTY ;;
				bsd)	v=-DHAS_BSDTTY ;;
				*) echo "Please say either 'sysv' or 'bsd'"
			esac
		done
	fi
	lib_defs="$lib_defs $v"
fi

if test "$autoconfig" ; then
   rm -f $nmfile
fi

#
# If we know the OS, we can provide the appropriate flags for SIO
#
case "$osversion" in
	sunos4) sio_defs="-DHAS_MMAP -DHAS_ONEXIT -DHAS_MEMOPS -DHAS_ISATTY" ;;
	ultrix4) sio_defs="-DHAS_MEMOPS -DHAS_ATEXIT -DHAS_ISATTY" ;;
	*) sio_defs="$lib_defs"
esac

PWD=`pwd`

incdir=$PWD/libs/include
mandir=$PWD/libs/man
libdir=$PWD/libs/lib

#
# Only Suns understand .SB in man pages, so if we are not dealing with
# a Sun we replace .SB with .B
#
if test "$osversion" = "sunos4" ; then
	modify_manpages=
else
	case "$has_SB" in
		yes) modify_manpages= ;;
		no)  modify_manpages=yes ;;
		*)
		echo -n "Can your machine handle .SB in a man page ? (if you are not sure, say no) -->"
		read x
		if test "$x" = "n" -o "$x" = "no" ; then
			modify_manpages=yes
		else
			modify_manpages=
		fi
	esac
fi

if test "$modify_manpages" ; then
	#
	# We can't take any interrupts while processing the manpages
	#
	trap 'interrupt_occured=yes' $catch_sigs

	echo "Replacing .SB in library man pages with .B (if a man page is changed"
	echo "the original will be kept by appending .orig to its name)"
	for i in $lib_names
	do
		cd $PWD/libs/$i
		for i in *.3
		do
			mv $i $i.orig											# save the original
			sed 's/[.]SB/.B/' $i.orig > $i					# do the replacement
			cmp -s $i $i.orig										# are they the same ?
			if test $? -eq 0 ; then rm $i.orig ; fi		# if yes, remove the copy
		done
		if test "$interrupt_occured" = "yes" ; then
			echo "Interrupt: quiting"
			exit 1
		fi
	done

	trap $catch_sigs		# no more trapping
fi

#
# Make the libraries
#
mvars="DEBUG=-O MANDIR=$mandir INCLUDEDIR=$incdir LIBDIR=$libdir"
for i in $lib_names
do
	cd $PWD/libs/src/$i
	echo Making library: $i
	if test "$i" = "sio" ; then
		make $mvars DEFS="$sio_defs" install
	else
		make $mvars DEFS="$lib_defs" install
	fi
	if test $? -ne 0 ; then
		echo "Failed to create library: $i"
		exit 2
	fi
done

#
# Now make pstext
# We remove pstext pstext executable, if there is one, in case the libraries
# have changed
#
cd $PWD/pstext
rm -f pstext
mvars="DEBUG=-O INCLUDEDIR=-I$incdir LDFLAGS=-L$libdir"
echo "Making pstext"
make $mvars
if test $? -ne 0 ; then
	echo Failed to create pstext
	exit 2
fi

if test "$modify_manpages" ; then

	trap 'interrupt_occured=yes' $catch_sigs

	echo "Replacing .SB in pstext man page with .B"
	i=pstext.man
	mv $i $i.orig
	sed 's/[.]SB/.B/' $i.orig > $i

	if test "$interrupt_occured" = "yes" ; then
		echo "Interrupt: quiting"
		exit 1
	fi
	trap $catch_sigs		# no more trapping
fi

