#! /bin/tcsh -f

#usages
# swapbinaries  whichbinaries   (cc, gcc, wx.gcc)
#   Translates whichbinaries to NEWBINARIES with values
#    HPUX_cc, HPUX_gcc, or HPUX_gcc_wxWin
#   Moves *.o to binaries.`cat NAMEFORBINARIES`
#   Sets NAMEFORBINARIES to NEWBINARIES
#   Moves ./binaries.NEWBINARIES/*.o to .  

#980403 Modified to go with new structure of make and environment files
#010216 Modified for use on Linux
#

# swapbinaries   [cc | gcc | wx.gcc | wx.export.gcc | export.gcc]

#Directory name is constructed from ROOT by appending _gcc, _gcc_wxWin,
# _gcc_export, _gcc_wxWin_export

# These directories are where the binaries are kept and one of the names
# must be in file NAMEFORBINARIES.  NAMEFORBINARIES is automatically updated
# after a swap.

# If the contents of NAMEFORBINARIES indicates no swap is needed, only
# a message to that effect is printed.
#
# Otherwise, the binaries in the current directory are moved to the directory
# named in NAMEFORBINARIES;  then the contents of the appropriate directory
# are moved to the current directory and NAMEFORBINARIES is updated

# Each file of binaries must have the *.o file and a version of make.env
# with name built from $ROOT1.


#set ROOT = HPUX
set ROOT = Linux_x86

#ROOT1 is used to build list of legal names of *.env files by appending
# gcc.env, gccwx.env, cc.env, _export.env, wx_export.env
#set ROOT1 = hp
set ROOT1 = linux86

# Designed to swap between binaries.$ROOT_cc, binaries.wx.$ROOT_gcc
# and binaries.$ROOT_gcc_wxWin, etc

# It is an error if `cat NAMEFORBINARIES` is not one of the valid
# names ($ROOT_cc, $ROOT_gcc, $ROOT_gcc_wxWin, $ROOT_gcc_wxWin_export
# $ROOT_gcc_export)

# swapbinaries -D  [cc | gcc | wx.gcc]
# does no swapping, but prints the commands that would do the swapping.

# 980403 In addition to binaries, it also copies the appropriate make.env
# from the binaries.* file; make.env is not moved.

# 010720 Added additional options for export versions

# 010724 short cut name for ordinary wx export is wx.export.gcc

set PROG = $0
set PROG = $PROG:t
set TESTFILE = blas.o
set CURRENTBINARIES = NAMEFORBINARIES

# Customize the following to match your installation.
#set VALIDARGS = (gcc wx.gcc cc) # short cut names
set VALIDARGS = (gcc wx.gcc export.gcc wx.export.gcc) # short cut names
#set VALIDNAMES = ($ROOT{_}gcc $ROOT{_}gcc_wxWin $ROOT{_}cc)
set VALIDNAMES = ($ROOT{_}gcc $ROOT{_}gcc_wxWin $ROOT{_}gcc_export $ROOT{_}gcc_wxWin_export)

#set VALIDMAKEENV = ($ROOT1{}gcc.env $ROOT1{}gccwx.env $ROOT1{}cc.env) # make.env files
set VALIDMAKEENV = ($ROOT1{}.env $ROOT1{}wx.env $ROOT1{}_export.env $ROOT1{}wx_export.env) # make.env files

# The correct *.env file will be copied (not moved) to . and renamed make.env
# play it safe

if ($#VALIDARGS != $#VALIDNAMES || $#VALIDARGS != $#VALIDMAKEENV)then
	echo "$PROG:Lengths of $VALIDARGS, $VALIDNAMES & $VALIDMAKENV not all the same"
	echo Fix the script $PROG
	exit 1
endif


if ($#argv == 0 || A$1 =~ A-[hH]*)goto usage

if (A$1 == A-D)then
	set DEBUG
	shift
endif

set YOU = `whoami`
set HERE = `pwd`

if (!(-f $CURRENTBINARIES))then
	echo $PROG": File ./$CURRENTBINARIES does not exist"
	exit 1
endif

set CURRENT = (`cat $CURRENTBINARIES`)

if ($#CURRENT != 1)goto badcontents
set ICURRENT = 1
foreach arg ($VALIDNAMES)
	if ($CURRENT == $arg)break
	@ ICURRENT ++
end
if ($ICURRENT > $#VALIDNAMES)goto badcontents

if (-f make.env) then
	set ENVNAME =\
  (`grep '^NAMEFORBINARIES *=' make.env|sed 's/^[^=]*= *\([^ *]*\)/\1/'`)
else
	set ENVNAME = "$CURRENT"
endif

if ("$ENVNAME" != "$CURRENT")goto badmakenv

if ($#argv > 1)	goto usage

if ($#argv == 0)then
# no argument swaps between first two VALIDNAMES
	set INEW = 1
	if ("$CURRENT" == "$VALIDNAMES[1]") set INEW = 2
	set NEWBINARIES = $VALIDNAMES[$INEW]
endif

if ($#argv == 1)then
	set NEWBINARIES = $1
	set INEW = 1
	foreach arg ($VALIDARGS)
		if ($NEWBINARIES == $arg)break
		@ INEW ++
	end
	if ($INEW > $#VALIDARGS)goto badarg
	set NEWBINARIES = $VALIDNAMES[$INEW]
	if ("$NEWBINARIES" == "$CURRENT")goto nochange
endif

set MAKEENVNEW = $VALIDMAKEENV[$INEW]
set MAKEENVCURRENT = $VALIDMAKEENV[$ICURRENT]

set FROM = ./binaries."$NEWBINARIES"
set TO = ./binaries."$CURRENT"

if (-f $TO/$TESTFILE)then
	echo $PROG": information in $CURRENTBINARIES does not match where *.o files are"
	exit 1
endif

if (!(-f $FROM/$TESTFILE))then
	echo $PROG": apparently no binaries in $FROM"
	exit 1
endif

if (!(-f $FROM/$MAKEENVNEW))then
	echo $PROG": file $MAKEENVNEW not found in $FROM"
	exit 1
endif

if ($?DEBUG)then
	echo "mv *.o $TO"
	echo "echo $NEWBINARIES > $CURRENTBINARIES"
	echo $PROG": current binaries would have been moved to $TO"
else
	mv *.o $TO
	echo "$NEWBINARIES" > $CURRENTBINARIES
	echo $PROG": current binaries moved to $TO"
endif

done:
set YOU = `pwd`
if ($?DEBUG)then
	echo "mv $FROM/*.o ."
	echo "cp -p $FROM/$MAKEENVNEW ./make.env"
	echo $PROG": binaries in $FROM would now be in $HERE"
else
	mv $FROM/*.o .
	cp -p $FROM/$MAKEENVNEW ./make.env
	echo $PROG": binaries in $FROM now in $HERE"
endif
exit 0

nochange:
	echo $PROG": binaries appear already to be from binaries.$NEWBINARIES"
	exit 0

usage:
	set USAGE = "usage: $PROG $VALIDARGS[1]"
	if ($#VALIDARGS >= 2) then
		set USAGE = "$USAGE or $VALIDARGS[2]"
	endif
	if ($#VALIDARGS >= 3) then
		set USAGE = "$USAGE or $VALIDARGS[3]"
	endif
	if ($#VALIDARGS >= 4) then
		set USAGE = "$USAGE or $VALIDARGS[4]"
	endif
	if ($#VALIDARGS >= 5) then
		set USAGE = "$USAGE or $VALIDARGS[5]"
	endif
	if ($#VALIDARGS >= 6) then
		set USAGE = "$USAGE or $VALIDARGS[6]"
	endif
	echo $USAGE
	exit 1

badcontents:
	echo $PROG": contents of $CURRENTBINARIES must be one of $VALIDNAMES"
	exit 1

badarg:
	echo $PROG": argument be one of $VALIDARGS"
	exit 1

badmakenv:
	echo $PROG": contents of $CURRENTBINARIES does not go with make.env"
	exit 1
