#!/bin/ksh
# %W% %U% %E%
#
# build			- no args - link all known *.o
# build obj1.o obj2.o..	- link specified objects in this directory
#
# The output directory is bin<n> where n is the build number.
#
# It prompts to install a build by soft linking all commands to
# /usr/RCS/v[Release]/bin when the command is a user command.


. /usr/RCS/common2 # Error, fail, Input, Query, Select.

###############################################################################
#
# DoWhom - get system characteristics
#
DoWhom()
{
    HostType=$1
    Processor=$2
    Unix=$3
}
DoWhom `./whom`


###############################################################################
# Determine $UCROOT, $UCINSTANCE, $UCOBJMODE
###############################################################################

Objects=$*

BuildDir=`pwd`
cd `dirname $0`
MyRoot=`pwd`

if [ ! -r Version ]
then
    fail "Typically do in ~rosi/UXC/[Revision], not $MyRoot"
fi

MyVersion=`cat Version`						# eg. V2.0.0.0
Release=`echo $MyVersion | sed "s/.//"`				# Remove T or V	
Release=`echo $Release | awk -F"." '{printf("%d.%d",$1,$2)}'`	# Get a.b from a.b.c.d

RCSDIR=/usr/RCS
UCROOT=/usr/RCS/v$Release	# Where we will build this version

# Get $UCINSTANCE
. $UCROOT/etc/rcsrc

MyName=`grep "SW UNIX-Connect" $UCROOT/Name`
set $MyName
# Example: SW UNIX-Connect T2.0.0.0 (64BIT) base - Northgate Information Solutions
UCOBJMODE=${4#'('}		# Strip off '('
UCOBJMODE=${UCOBJMODE%'BIT)'}	# & 'BIT)'


###############################################################################
# Set up the defaults
###############################################################################

UCBIN=$UCROOT/bin
UCLIB=$UCROOT/lib
USRBIN=/usr/bin
USRLIB=/usr/lib

if [ "$UCOBJMODE" = "64" ]
then
    ObjSuffix=_64
else
    ObjSuffix=""
fi

LIBCURSES=-lcurses

ONELIB=/usr/osidp/lib/libosidp${ObjSuffix}.a	# Do not expect 64 bit version
REALHBSLIB=$HBROOT/hbs${ObjSuffix}.a		# Exists if heartbeat installed

###############################################################################
# Create lists of commands to be built, both will have referencing links in the
# /usr/RCS/v[Release]/bin directory, USRCMDS used to have referencing links in
# /usr/bin, this is no longer done. Left here in case it still needs to be done.
###############################################################################

USRCMDS="sft		\
      ROSFS		\
      rush		\
      rushserver	\
      plid		\
      remlog		\
      synctime		\
      syncserv"

RCSCMDS="SMANAGER	\
      REMSERVER		\
      CHARSERVER	\
      CHARMOD		\
      plidid		\
      REALPRINT		\
      RUFM		\
      aci.exec		\
      replug		\
      smtr		\
      telnetd_lbs	\
      rcslisten		\
      ADI_FROM_ICL	\
      ADI_TO_ICL	\
      npu"


###############################################################################
#
# StopSM - stop session manager for $UCINSTANCE, corresponding to the release
# that we are re-building.
#
StopSM()
{
    $UCROOT/bin/rcs stop $UCINSTANCE
}


###############################################################################
#
# InstallBuild - Install rebuilt commands in /usr/RCS/v[Release]/bin
#		 We no longer create links in /usr/bin but if historical links
#		 still exist then they are removed.
#
InstallBuild() # BuildNo
{
    IsDefault=false
    if [ -r $RCSDIR/Name ] && diff $RCSDIR/Name $UCROOT/Name
    then
	# This is the default installation so update /usr/lib etc.

	IsDefault=true
    fi

    BinDir=$MyRoot/bin${1}
    StopSM

    for i in $USRCMDS
    do
	rm $UCBIN/$i 2>/dev/null
	ln -s $BinDir/$i $UCBIN

	if [ "$i" = remlog ]	# create upper case REMLOG for RealityX
	then
	    rm $UCBIN/REMLOG 2>/dev/null
	    ln -s $BinDir/$i $UCBIN/REMLOG
	fi
    done

    for i in $RCSCMDS
    do
	rm $UCBIN/$i 2>/dev/null
	ln -s $BinDir/$i $UCBIN
    done

    # link the libraries to the build directory

    for i in $BinDir/lib/*
    do
	lib=`basename $i`

	rm $UCLIB/$lib
	ln -s $BinDir/lib/$lib $UCLIB

	# And also to /usr/lib if this is the default installation

	if [ $lib != libhbs${ObjSuffix}.a ] &&
	   [ $IsDefault == true ]
	then
	    rm $USRLIB/$lib 2>/dev/null
	    ln -s $BinDir/lib/$lib $USRLIB
	fi
    done
}


##############################################################################
#
#				MAINLINE CODE
#
###############################################################################

if id | grep -v root > /dev/null
then
    fail "You must be super user to run this script"
fi

if [ -z "$Objects" ]
then
    # No args so install an already built version

    Input "Install which build number?"
    Buildno=$REPLY

    BIN=bin$Buildno

    if [ -d $BIN ]
    then
	echo "The comment associated with this build is"
	cat $BIN/Comment
	Query "Continue?" n y || fail "Ok, not installing build $Buildno"
	InstallBuild $Buildno
    else
	fail "Directory bin$reply does not exist"
    fi
    exit 0
fi

###############################################################################
#
#				BUILD THE WORLD
#
###############################################################################

# Generate the build directories

BLDNOFILE="BuildNo"

if [ -f $BLDNOFILE ]
then
    buildno=`cat $BLDNOFILE`
    ((buildno += 1))
else
    # First time
    buildno=1
fi

echo $buildno > $BLDNOFILE

BIN=bin$buildno
mkdir $BIN $BIN/lib

###############################################################################
# Sort out the libraries
###############################################################################

LIBRCS=$BIN/lib/librcs${ObjSuffix}.a
LIBIFA=$BIN/lib/libifa${ObjSuffix}.a
LIBHBS=$BIN/lib/libhbs${ObjSuffix}.a

cd $MyRoot

# Check if O.N.E. stack installed, in which case archive their lib

if [ -r $ONELIB ]
then
    cd lib
    ar -x $ONELIB					# Extract all *.o
    ar -d librcs${ObjSuffix} one_stub.o 2>/dev/null	# Delete one_stub.o
    ar -r librcs${ObjSuffix} *.o			# Add *.o from $ONELIB
    rm *.o
    cd ..
fi

# Put the libraries to be used under the build directory

# Use the real heartbeat library if it exists, $HBROOT would be in /etc/profile

if [ -f $REALHBSLIB.a ]
then
    cp $REALHBSLIB.a $LIBHBS
    echo "Using the real Heartbeat library $REALHBSLIB"
else
    cp lib/libhbs${ObjSuffix}.a $LIBHBS
fi

cp lib/librcs${ObjSuffix}.a lib/libifa${ObjSuffix}.a $BIN/lib

# Adjust the libraries and flags depending on the system type.

case $HostType in

"sun" )	PATH=/opt/SUNWspro/bin:/usr/ccs/bin:$PATH
	SYSLIBS="-lsocket -lnsl -lpam -lc"
	LD=ld
	;;

"aix" )	PATH=/usr/ccs/bin:$PATH
	SYSLIBS="/usr/ccs/lib/crt0${ObjSuffix}.o -lxti -lc"
	LD="ld -b$UCOBJMODE"
	;;

"hp" )	PATH=/usr/ccs/bin:$PATH
	LD="ld -L /usr/ccs/lib"
	SYSLIBS=/usr/ccs/lib/crt0${ObjSuffix}.o -lxti -lc
	;;

l?x)	# LINUX both Redhat 7 and Redhat 9 and both 32 and 64 bit systems share same.
	SYSLIBS="-lcrypt -lpam -lc"
	LD=ld
	;;

"sx" )	# 
	# Using delivered libraries
	SYSLIBS="/lib/crt1${ObjSuffix}.o /usr/lib/stat_lib.o	\
		-lnsl_s -lsocket -lc /lib/crtn${ObjSuffix}.o"
	LD=idld
	;;

#"DG" )	FIRSTLIBS=
#	LASTLIBS=
#	LIBS="$LIBRCS -lsocket -lnsl"
#	LD=cc
#	;;
#
#"xm" ) 	if [ "$Unix" = "Vr4" ]
#	then
#	    PATH=/usr/ccs/bin:$PATH
#	    export PATH
#	    LD=/usr/ccs/bin/ld
#	    LDFLAGS=""
#	    FIRSTLIBS="/usr/ccs/lib/crt1.o /usr/ccs/lib/crti.o /usr/ccs/lib/values-Xt.o"
#	    LASTLIBS="/usr/ccs/lib/crtn.o"
#	    LIBS="$LIBRCS -lsocket -lnsl -YP,/usr/ccs/lib:/usr/lib -L /usr/ccs/lib/gcc2 -lgcc -lc"
#	else
#	    FIRSTLIBS=/lib/crt1.o
#	    LASTLIBS="/lib/crtn.o"
#	fi
#	;;
#

* )	echo Default library set being used
	SYSLIBS="/lib/crt1.o /lib/crtn.o"
	LD=ld
	;;
#
esac	# End of setting dependencies on the system type

LIBS="$LIBRCS $LIBHBS $SYSLIBS"

export PATH

###############################################################################
# Build the executables
###############################################################################

cd $MyRoot

for Object in $Objects
do
    outfile=`basename $Object ".o"`

    # Add additional specific libs where required

    case $outfile in
	sft) LDLIBS="$LIBIFA $LIBS"
	    ;;
	remlog) LDLIBS="$LIBS $LIBCURSES"
	    ;;
	SMANAGER|aci.exec|npu|ADI_TO_ICL) LDLIBS=$LIBS
	    ;;
	*) LDLIBS=$LIBS
	    ;;
    esac
    printf "Execs, pwd "
    pwd
    echo $LD $Object $LDLIBS -o$BIN/$outfile
    if $LD $Object $LDLIBS -o$BIN/$outfile
    then
	echo "$outfile successfully built\n"
    else
	fail "$LD FAILED, please contact your CSG support representative"
    fi
done

# Set the setuserid bit where needed

chmod +s $BIN/RUFM* $BIN/npu

Input "build $buildno made, please describe this build\nComment:" \
      "Built by Quiet-Install"

echo "$REPLY" > $BIN/Comment

Query "build $buildno made, would you like this to be installed, in $UCROOT" y y ||
    fail 1 "Ok, not installing build $buildno"

# Link the commands into the release's bin directory, & libraries into the lib
# directory and also to the /usr/lib directory.

InstallBuild $buildno


