#!/bin/sh
#
#	This script is used to set up a sub-directory to contain
#	everything that is needed for a new architecture and operating
#	system.  Refer to the Condor Installation Guide for a list of
#	currently supported architectures and operating systems.
#
TEST=/bin/test
XLIBDIR=/usr/misc/X11/lib
NDBM=
LIBX=


if $TEST $# != 2 ; then
	echo Usage: $0 Architecture Operating-System
	echo For example: $0 SPARC SUNOS40
	exit
fi

ARCH=$1
OPSYS=$2

#
#	Check to make sure that we know about the specified architecture
#	and operating system
#
case "$ARCH" in
	IBM032 | VAX | I386 | SPARC | MC68020 | MIPS )
		break
		;;
	
	*)
		echo Unknown architecture: $ARCH
		echo Known architectures are: VAX, I386, SPARC, MC68020, and MIPS
		echo You will need to modify this $0 as well as the rest of Condor.
		exit
esac

case "$OPSYS" in
	ULTRIX | BSD43 | DYNIX | SUNOS32 | SUNOS40 )
		break
		;;
	
	*)
		echo Unknown operating system: $OPSYS
		echo Known operating systems are: ULTRIX, BSD43, DYNIX, SUNOS32, and SUNOS40
		echo You will need to modify this $0 as well as the rest of Condor.
		exit
esac

#
# Check to make sure that we are on the archetecture/operating system
# that we are trying to build.
#
echo Checking validity of $ARCH/$OPSYS
rm -f C.$ARCH.$OPSYS
cc -o C.$ARCH.$OPSYS GENERIC/CheckMachType.c -D$ARCH -D$OPSYS
./C.$ARCH.$OPSYS

if $TEST $? != 0 ; then
	rm -f C.$ARCH.$OPSYS
	exit
fi

rm -f C.$ARCH.$OPSYS

CONDIRS="condor condor_collector condor_lib \
	condor_master condor_negotiator condor_q condor_rm condor_schedd \
	condor_shadow condor_startd condor_starter condor_tools \
	ckpt_lib client h release release/bin release/lib standalone \
	util_lib xdr_lib condor_kbdd"

SYSNAME=$ARCH"_"$OPSYS
CONFIG=$SYSNAME/release/lib/condor_config
SYSBIN=$SYSNAME/release/bin

#
# Make directories
#
echo Making directories
mkdir $SYSNAME
cd $SYSNAME
mkdir $CONDIRS
cd ..

#
# Make condor_config
#
echo Making condor_config
echo "#" 													 > $CONFIG
echo "# Machine Description" 								>> $CONFIG
echo "#" 													>> $CONFIG
echo "ARCH=$ARCH"											>> $CONFIG
echo "OPSYS=$OPSYS"											>> $CONFIG
echo "" 													>> $CONFIG
cat GENERIC/condor_config									>> $CONFIG

RELDIR=`grep "^RELEASEDIR" $CONFIG`

#
# Make Condor.on, Condor.off, and Condor.init
#
echo Making Condor.on, Condor.off, and Condor.init
cp GENERIC/Condor.off $SYSBIN/Condor.off
cp GENERIC/Condor.on $SYSBIN/Condor.on
echo ""													>> $SYSBIN/Condor.on
echo set $RELDIR										>> $SYSBIN/Condor.on
echo \$"RELEASEDIR"/bin/condor_master					>> $SYSBIN/Condor.on
echo echo Condor has been started						>> $SYSBIN/Condor.on

echo "#!/bin/csh -f"									 > $SYSBIN/Condor.init
echo ""													>> $SYSBIN/Condor.init
echo set CHMOD=/bin/chmod								>> $SYSBIN/Condor.init
echo set CHOWN=/etc/chown								>> $SYSBIN/Condor.init
echo set CHGRP=/bin/chgrp								>> $SYSBIN/Condor.init
echo set $RELDIR										>> $SYSBIN/Condor.init
echo set CONFIG_VAL=\$"RELEASEDIR"/bin/config_val		>> $SYSBIN/Condor.init
echo ""													>> $SYSBIN/Condor.init
echo rm -f ~condor/condor_config						>> $SYSBIN/Condor.init
echo ln -s \$"RELEASEDIR"/lib/condor_config ~condor/condor_config \
														>> $SYSBIN/Condor.init
echo 'eval `$CONFIG_VAL LOG EXECUTE SPOOL`'				>> $SYSBIN/Condor.init
echo mkdir \$"LOG" \$"EXECUTE" \$"SPOOL"				>> $SYSBIN/Condor.init
echo \$"CHMOD" 700 \$"EXECUTE"							>> $SYSBIN/Condor.init
echo \$"CHMOD" 770 \$"SPOOL"							>> $SYSBIN/Condor.init
echo \$"CHMOD" 775 \$"LOG"								>> $SYSBIN/Condor.init
echo \$"CHOWN" condor \$"LOG" \$"EXECUTE" \$"SPOOL"		>> $SYSBIN/Condor.init
echo \$"CHGRP" condor \$"LOG" \$"EXECUTE" \$"SPOOL"		>> $SYSBIN/Condor.init
chmod 775 $SYSBIN/Condor.on $SYSBIN/Condor.off $SYSBIN/Condor.init
echo echo Condor has been initialized, but not started.	>> $SYSBIN/Condor.init

#
# Make INSTALL script
#
echo Making INSTALL script
echo "#!/bin/csh -f"										 > $SYSNAME/INSTALL
echo ""														>> $SYSNAME/INSTALL
echo set $RELDIR											>> $SYSNAME/INSTALL
echo "set FILESERVER=YourFavoriteFileServer"				>> $SYSNAME/INSTALL
echo "#"													>> $SYSNAME/INSTALL
echo "# Uncomment one of the following lines."				>> $SYSNAME/INSTALL
echo "# If the executables are going to be on"				>> $SYSNAME/INSTALL
echo "# another machine, you should perform"				>> $SYSNAME/INSTALL
echo "# an/some rdist(s).  If the executables"				>> $SYSNAME/INSTALL
echo "# are on this machine, you should remove"				>> $SYSNAME/INSTALL
echo "# any existing symbolic link and make a"				>> $SYSNAME/INSTALL
echo "# new one."											>> $SYSNAME/INSTALL
echo "#"													>> $SYSNAME/INSTALL
echo ""														>> $SYSNAME/INSTALL
echo "#rdist" -c release \$"FILESERVER"'":"'\$"RELEASEDIR"	>> $SYSNAME/INSTALL
echo "#rm" -f \$"RELEASEDIR"								>> $SYSNAME/INSTALL
echo "#ln" -s `pwd`/$SYSNAME/release \$"RELEASEDIR"			>> $SYSNAME/INSTALL
echo "echo You must edit this INSTALL script."				>> $SYSNAME/INSTALL
chmod 775 $SYSNAME/INSTALL

#
# Make a machine specific Makefile
#
echo Making $SYSNAME/Makefile.mach
echo TILDE=`pwd`									 > $SYSNAME/Makefile.mach
echo DSTDIR="$(TILDE)"/$SYSNAME/release				>> $SYSNAME/Makefile.mach

if $TEST -s /usr/include/ndbm.h ; then
    NDBM=-DNDBM
fi

if $TEST -s $XLIBDIR/libX11.a; then
	echo LIBX=-lX11								   >> $SYSNAME/Makefile.mach ;
	echo XPROGS=condor_kbdd						   >> $SYSNAME/Makefile.mach ;
fi

echo IDENT=-D$ARCH -D$OPSYS $NDBM					>> $SYSNAME/Makefile.mach ;

if $TEST -s /usr/lib/libndbm.a ; then
	echo LIBDBM=-lndbm								>> $SYSNAME/Makefile.mach ;
elif $TEST -s /usr/lib/libdbm.a ; then
	echo LIBDBM=-ldbm								>> $SYSNAME/Makefile.mach ;
else
	echo LIBDBM=									>> $SYSNAME/Makefile.mach ;
fi

if $TEST "$OPSYS" = "SUNOS40" ; then
	echo "SUN_SIGVEC = _SIGVEC.o"					>> $SYSNAME/Makefile.mach ;
	echo "LDOPTS=-dc -e start -Bstatic"				>> $SYSNAME/Makefile.mach ;
	echo "COPTS="									>> $SYSNAME/Makefile.mach ;
	echo "# Don't optimize, causes problems on Sun4's for  some reason"\
													>> $SYSNAME/Makefile.mach ;
else
	echo "LDOPTS="									>> $SYSNAME/Makefile.mach ;
	echo "COPTS= -O"								>> $SYSNAME/Makefile.mach ;
fi


cat GENERIC/Makefile.mach							>> $SYSNAME/Makefile.mach

#
# Make a program to make sure that we are compiling on the
# correct architecture/operating system.
#
echo Making $SYSNAME/CheckMachType.c
rm -f $SYSNAME/CheckMachType.c
ln GENERIC/CheckMachType.c $SYSNAME/CheckMachType.c

cd $SYSNAME
rm -f Makefile
ln -s ../Makefile Makefile
cd ..

if $TEST ! -s $XLIBDIR/libX11.a; then
	echo ""
	echo "         ***  WARNING  ***"
	echo "I have searched for the X library libX11.a in $XLIBDIR,"
	echo "but can't find it.  If you are not running X, this is OK."
	echo "If you are running X, you should remove $SYSNAME, edit"
	echo "NewArch and set \$XLIBDIR to reflect the correct pathname"
	echo "for your site, and re-run NewArch."
fi
