#!	/bin/csh
#			pss123:~sinqhm/wind/SinqHM/<instr>/bld	V01G
#
#	    Script to compile and link the SinqHM_srv programs for vxWorks
#
#	  rcp -p "lnsa04:sinqhm/bld" ~/wind/SinqHM/bld

##   printf 'Arg 0 = "%s"\n' $0
##   printf 'Arg 1 = "%s"\n' $1
##   printf 'Arg 2 = "%s"\n' $2
##   printf 'Arg 3 = "%s"\n' $3

	set proc = "$0"
	if (`expr match "$proc" "\/"` != 1) set proc = "./$0"
	cd $proc:h		# Build in bld's directory!
#------------------------------------------------------------------------------
#    Uncomment the following command to disable remote-copy operations
#
## alias rcp 'echo "  Warning -- remote copy disabled"; echo \!\!:* > /dev/null'
#------------------------------------------------------------------------------
	if ("`whoami`" == "maden") then
	  set src = "maden@lnsa06:sinqhm/"
	else if ("`whoami`" == "sinqhm") then
	  set src = "koenneck@lnsa17:src/sics/sinqhm/"
	else
	  echo Sorry, you are not recognised by this script.
	  exit
	endif
#------------------------------------------------------------------------------
	alias  ccvx   'ccppc -O0 -mcpu=603 -I${WIND_BASE}/target/h ' \
		'-fno-builtin -fno-for-scope ' \
		'-nostdinc -DCPU=PPC603 -D_GNU_TOOL -gdwarf -c -Wimplicit'
#
	set Insts = (dev dmc sans focus hrpt trics poldi amor)

	if (("$1" == "") ||	\
	    ("$1" == "?") ||	\
	    ("$1" == "h") ||	\
	    ("$1" == "he") ||	\
	    ("$1" == "hel") ||	\
	    ("$1" == "help"))	goto show_help

	printf "\n"
	printf "Working directory is %s\n" "$cwd"
	printf 'Source is "%s"\n' "$src"
	printf "\n"

	if ("$1" == "bld")	goto bld
	if ("$1" == "startup")	goto startup
	if ("$1" == "basic")	goto basic

	foreach inst ($Insts)
	  if ("$1" == "$inst")  goto $inst
	end
	  printf '\aI do not know how to build "%s"\!\n' $1
	  goto show_help
#===========================================================================
 show_help_0:
	printf '\n\007You must specify what you want to build\!\n\n'
 show_help:
#=========
	echo "Usage:"
	echo "    bld bld         - copy over new version of bld script."
	echo "    bld startup     - copy over new version of startup script."
	echo "    bld basic       - build basic system."
	echo "    bld <inst> all  - build all modules for instrument <inst>."
	echo "    bld <inst> SinqHM_srv [<module>]   - build SinqHM_srv module"
	echo "    bld <inst> lwl_server              - build lwl_server module"
	echo "    bld <inst> SinqHM_bootParamsConfig - build auto-config module"
	echo "    bld <inst> SinqHM_bootUtil         - build boot-utility module"
	echo "    bld focus  FOCUS_srv [<module>]    - build FOCUS_srv module"
	echo "    bld <inst> auto                    - build auto-spawn system"
	echo " "
	echo "Valid instruments are:"
	echo "    $Insts"
	echo " "
	exit
#-----------------------------------------------------------------------------
 bld:
#===
	echo "   Copying bld (previous version is bld.save) ..."
	mv bld bld.save
	rcp -p "${src}bld" ./bld
	rcp -p "${src}bld2" ./bld2
	rcp -p "${src}bld604" ./bld604
	exit
#-----------------------------------------------------------------------------
 startup:
#=======
	echo "   Copying startup ..."
	rcp -p "${src}startup.template" ./startup
	exit
#-----------------------------------------------------------------------------
 basic:
#=====
	set dflt = "$cwd"
	cd $WIND_BASE/target/config/mv1603
	echo " "
	echo "Default directory changed to:"
	echo "            $cwd"
	printf "Building vxWorks_basic system image ...\n\n"
	touch config.h		# Force a complete re-compilation

	make 	ADDED_CFLAGS=" -DSINQHM_SPAWN=0" \
		ADDED_MODULES=""

	mv vxWorks     vxWorks_basic
	mv vxWorks.sym vxWorks.sym_basic
	cp -p vxWorks_basic      $dflt/vxWorks_basic
	cp -p vxWorks.sym_basic  $dflt/vxWorks.sym
	cd $dflt
	echo "   Done. Result is $dflt/vxWorks_basic"
	exit
#=============================================================================
#		Now follow labels for all the valid instruments.
 dev:
#----
	set instr = Devel
	set defn = '-DINST="Devel" -DDevel'
	set direc = Devel/NEW
	goto comn
 dmc:
#---
	set instr = DMC
	set defn = '-DINST="DMC" -DDMC'
	set direc = DMC/NEW
	goto comn
 sans:
#----
	set instr = SANS
	set defn = '-DINST="SANS" -DSANS'
	set direc = SANS/NEW
	goto comn
 focus:
#-----
	set instr = FOCUS
	set defn = '-DINST="FOCUS" -DFOCUS'
	set direc = FOCUS/NEW
	goto comn
 hrpt:
#----
	set instr = HRPT
	set defn = '-DINST="HRPT" -DHRPT'
	set direc = HRPT/NEW
	goto comn
 trics:
#-----
	set instr = TriCS
	set defn = '-DINST="TRICS" -DTRICS'
	set direc = TRICS
	goto comn
 poldi:
#-----
	set instr = POLDI
	set defn = '-DINST="POLDI" -DPOLDI'
	set direc = POLDI/NEW
	goto comn
 amor:
#----
	set instr = AMOR
	set defn = '-DINST="AMOR" -DAMOR'
	set direc = AMOR/NEW
	goto comn
 comn:
#----
	echo The selected instrument is ${instr}.

	if ("$2" == "")				goto show_help_0

	cd $direc
	printf "Working directory changed to %s\n" "$cwd"
	if ("$2" == "SinqHM_srv") 		goto SinqHM_srv
	if ("$2" == "lwl_server") 		goto lwl_server
	if ("$2" == "SinqHM_bootParamsConfig") 	goto SinqHM_bootParamsConfig
	if ("$2" == "SinqHM_bootUtil")		goto SinqHM_bootUtil
	if (("$instr" == "FOCUS") && ("$2" == "FOCUS_srv")) goto FOCUS_srv
	if ("$2" == "auto") 			goto auto
	if ("$2" != "all") then
	  printf '\007I do not know how to build "%s" for %s\!\n' "$2" $instr
	  printf 'Type "bld help" for help\!\n'
	  exit 
	endif
#--------------------------------------------------------------
 SinqHM_srv:
#==========
	set Mods = (SinqHM_gbl.h \
		    SinqHM_def.h \
		    vmio10_def.h \
		    SinqHM_srv_main.c \
		    SinqHM_srv_routines.c \
		    SinqHM_srv_filler.c \
		    SinqHM_srv_server.c)

	if (("$3" == "") || ("$3" == "all")) goto SinqHM_srv_all

	foreach module ($Mods)
	  if ("$3" == "$module") goto SinqHM_srv_cpy
	end

	if (("$3" != "?") &&	\
	    ("$3" != "h") &&	\
	    ("$3" != "he") &&	\
	    ("$3" != "hel") &&	\
	    ("$3" != "help")) then
	  printf '"%s" is not a recognised module of SinqHM_srv\!\n\n' "$3"
	endif

	printf 'To build SinqHM_srv, use the command:\n'
	printf '\n'
	printf '      bld <instr> SinqHM_srv [module]\n'
	printf '\n'
	printf 'Valid instruments are:\n'
	printf '\n'
	printf '      %s\n' "$Insts"
	printf '\n'
	printf 'Valid modules are:\n'
	printf '\n'
	foreach module ($Mods)
	  printf '      %s\n' "$module"
	end
	printf '\n'
	exit
#----------------------------------------------------------------------------
 SinqHM_srv_cpy:
#--------------
	echo "   Copying $3 ..."
	rcp -p  "${src}$3" ./$3

	if ("$3" == "SinqHM_gbl.h")		goto SinqHM_srv_compile_all
	if ("$3" == "SinqHM_def.h")		goto SinqHM_srv_compile_all
	if ("$3" == "vmio10_def.h")		goto SinqHM_srv_compile_all

	echo "   Compiling $3 ..."
	ccvx $defn $3
	goto SinqHM_srv_link

 SinqHM_srv_all:
#--------------
	echo " "
	echo "Building SinqHM_srv ..."
	echo "   Copying header files ..."
	  rcp -p "${src}SinqHM_gbl.h"	./SinqHM_gbl.h
	  rcp -p "${src}SinqHM_def.h"	./SinqHM_def.h
	  rcp -p "${src}vmio10_def.h"	./vmio10_def.h
	echo "   Copying source files ..."
	  rcp -p "${src}SinqHM_srv_main.c"	./SinqHM_srv_main.c
	  rcp -p "${src}SinqHM_srv_routines.c"	./SinqHM_srv_routines.c
	  rcp -p "${src}SinqHM_srv_filler.c"	./SinqHM_srv_filler.c
	  rcp -p "${src}SinqHM_srv_server.c"	./SinqHM_srv_server.c

 SinqHM_srv_compile_all:
#----------------------
	echo "   Compiling SinqHM_srv_main.c ..."
		ccvx $defn SinqHM_srv_main.c
	echo "   Compiling SinqHM_srv_routines.c ..."
		ccvx $defn SinqHM_srv_routines.c
	echo "   Compiling SinqHM_srv_filler.c ..."
		ccvx $defn SinqHM_srv_filler.c
	echo "   Compiling SinqHM_srv_server.c ..."
		ccvx $defn SinqHM_srv_server.c

 SinqHM_srv_link:
#---------------
	echo "   Linking ..."
		ldppc -o SinqHM_srv.o -r	\
				SinqHM_srv_main.o SinqHM_srv_routines.o \
				SinqHM_srv_filler.o SinqHM_srv_server.o
	echo "   Done. Result is $cwd/SinqHM_srv.o"
	if ("$2" != "all") exit
#---------------------------------------------------------------------------
 lwl_server:
#==========
	echo " "
	echo "Building lwl_server ..."
	echo "   Copying header files ..."
		rcp -p "${src}vmio10_def.h" ./vmio10_def.h
	echo "   Copying source files ..." 
		rcp -p "${src}lwl_server.c" ./lwl_server.c
	echo "   Compiling lwl_server.c ..."
		ccvx $defn lwl_server.c
	echo "   Done. Result is $cwd/lwl_server.o"
	if ("$2" != "all") exit
#---------------------------------------------------------------------------
 SinqHM_bootParamsConfig:
#=======================
	echo " "
	echo "Building SinqHM_bootParamsConfig ..."
	echo "   Copying header files ..."
		rcp -p	"${src}SinqHM_def.h" ./SinqHM_def.h
	echo "   Copying source files ..."
		rcp -p "${src}SinqHM_bootParamsConfig.c" \
			./SinqHM_bootParamsConfig.c
	echo "   Compiling SinqHM_bootParamsConfig.c ..."
		ccvx $defn -I${WIND_BASE}/target/config/all \
			SinqHM_bootParamsConfig.c
	echo "   Done. Result is $cwd/SinqHM_bootParamsConfig.o"
	if ("$2" != "all") exit
#---------------------------------------------------------------------------
 SinqHM_bootUtil:
#===============
	echo " "
	echo "Building SinqHM_bootUtil ..."
	echo "   Copying header file ..."
		rcp -p	"${src}SinqHM_def.h" ./SinqHM_def.h
	echo "   Copying source file ..."
		rcp -p "${src}SinqHM_bootUtil.c" \
			./SinqHM_bootUtil.c
	echo "   Compiling SinqHM_bootUtil.c ..."
		ccvx $defn -I${WIND_BASE}/target/config/all SinqHM_bootUtil.c
	echo "   Done. Result is $cwd/SinqHM_bootUtil.o"
	if ("$2" != "all") exit
#---------------------------------------------------------------------------
 FOCUS_srv:
#==========
	if ($instr != "FOCUS") goto FOCUS_srv_skp
	  set Mods = ( FOCUS_gbl.h \
		    SinqHM_def.h \
		    FOCUS_srv_main.c )

	  if (("$3" == "") || ("$3" == "all")) goto FOCUS_srv_all

	  foreach module ( $Mods )
	    if ("$3" == "$module") goto FOCUS_srv_cpy
	  end

	  if (("$3" != "?") &&	\
	      ("$3" != "h") &&	\
	      ("$3" != "he") &&	\
	      ("$3" != "hel") &&	\
	      ("$3" != "help")) then
	    printf '"%s" is not a recognised module of FOCUS_srv\!\n\n' "$3"
	  endif

	  printf 'To build FOCUS_srv, use the command:\n'
	  printf '\n'
	  printf '      bld <instr> FOCUS_srv [module]\n'
	  printf '\n'
	  printf 'Valid instruments are:\n'
	  printf '\n'
	  printf '      %s\n' "focus"
	  printf '\n'
	  printf 'Valid modules are:\n'
	  printf '\n'
	  foreach module ($Mods)
	    printf '      %s\n' "$module"
	  end
	  printf '\n'
	  exit
#----------------------------------------------------------------------------
 FOCUS_srv_cpy:
#--------------
	echo "   Copying $3 ..."
	rcp -p  "${src}$3" ./$3

	if ("$3" == "FOCUS_gbl.h")		goto FOCUS_srv_compile_all
	if ("$3" == "SinqHM_def.h")		goto FOCUS_srv_compile_all

	echo "   Compiling $3 ..."
	ccvx $defn $3
	goto FOCUS_srv_link

 FOCUS_srv_all:
#--------------
	echo " "
	echo "Building FOCUS_srv ..."
	echo "   Copying header files ..."
	  rcp -p "${src}FOCUS_gbl.h"	./FOCUS_gbl.h
	  rcp -p "${src}SinqHM_def.h"	./SinqHM_def.h
	echo "   Copying source files ..."
	  rcp -p "${src}FOCUS_srv_main.c"	./FOCUS_srv_main.c

 FOCUS_srv_compile_all:
#----------------------
	echo "   Compiling FOCUS_srv_main.c ..."
		ccvx $defn FOCUS_srv_main.c

 FOCUS_srv_link:
#---------------
	echo "   Linking ..."
		ldppc -o FOCUS_srv.o -r	\
				FOCUS_srv_main.o
	echo "   Done. Result is $cwd/FOCUS_srv.o"
 FOCUS_srv_skp:
#-------------
	if ("$2" != "all") exit
#---------------------------------------------------------------------------
 auto:
#====
	set dflt = "$cwd"
	cd $WIND_BASE/target/config/mv1603
	echo " "
	echo "Default directory changed to:"
	echo "            $cwd"
	printf "\n\n==================================================\n"
	printf \
	  "\n\nBuilding vxWorks_auto system image for $instr Instrument ...\n\n"
	touch config.h		# Force a complete re-compilation

	set mods = (	$dflt/SinqHM_srv.o \
			$dflt/lwl_server.o \
			$dflt/SinqHM_bootParamsConfig.o \
			$dflt/SinqHM_bootUtil.o	)
	if ($instr == FOCUS) set mods = ( $mods $dflt/FOCUS_srv.o )

	make 	ADDED_CFLAGS=" -DSINQHM_SPAWN=1" 	\
		ADDED_MODULES="$mods"
 			
	mv vxWorks     vxWorks_auto
	mv vxWorks.sym vxWorks.sym_auto
	cp -p vxWorks_auto      $dflt/vxWorks_auto
	cp -p vxWorks.sym_auto  $dflt/vxWorks.sym
	cd $dflt
	echo "   Done. Result is $dflt/vxWorks_auto"
	exit
#------------------------------------------------------------------ End of bld
