#!/bin/sh
# usage: AS o_to_O_file src_file dst_file cpp_options...
#
# This script is used to assemble Gambit's kernel file "src/LIB/_kernel.s"
# to produce the file "_kernel.O".  The kernel file is written in a way that
# accounts for the assemblers of the SUN3 and HP300 computers.  For
# other machines, the assembler can not be used so one of the two
# following strategies must be used:
#
# 1) The file is transferred to a SUN3 or HP300 and assembled there.
# 2) No assembly is actually performed, a pre-assembled file is used instead.
#
# The CPP symbol "asm_loc" selects which strategy is used when the
# machine is not a SUN3 or HP300.  If it is not defined, strategy 2 is used,
# otherwise "asm_loc" is the name of the host that will do the assembling.

script=/tmp/ASscript$$

# Use 'cpp' to figure out what machine this is...

/lib/cpp -P > $script << EOF

 # *** if remote assembling, define "asm_loc" here, for example: #define asm_loc filbert

#ifdef sunZZZZZ

 # *** SUN3 ***   <----- sun3 assembly is no longer possible

tmp=/tmp/AS\$\$
/lib/cpp -P $4 $5 $6 $2 | /bin/sed -e "s/%NEWLINE%/\\\\
/g" > \$tmp.s
as -o \$tmp.o \$tmp.s
cc -o \$tmp $1 \$tmp.o
\$tmp > $3

#else
#ifdef hpux

 # *** HP300 ***

tmp=/tmp/AS\$\$
/lib/cpp -P $4 $5 $6 $2 | /bin/sed -e "s/%NEWLINE%/\\\\
/g" > \$tmp.s
as -w -d -o \$tmp.o \$tmp.s
cc -o \$tmp $1 \$tmp.o
\$tmp > $3

#else

 # *** Not a SUN3 or HP300 ***

#ifdef asm_loc

 # *** Use remote assembling ***

remote_AS=/tmp/AS1_\$\$
remote_o_to_O_file=/tmp/AS2_\$\$.c
remote_src_file=/tmp/AS3_\$\$.s
remote_dst_file=/tmp/AS4_\$\$
rcp AS asm_loc:\$remote_AS
rcp $1 asm_loc:\$remote_o_to_O_file
rcp $2 asm_loc:\$remote_src_file
#ifdef butterfly
rsh asm_loc "/bin/chmod +x \$remote_AS;\$remote_AS \$remote_o_to_O_file \$remote_src_file \$remote_dst_file -Dbutterfly -DELOG $4 $5 $6"
#else
rsh asm_loc "/bin/chmod +x \$remote_AS;\$remote_AS \$remote_o_to_O_file \$remote_src_file \$remote_dst_file $4 $5 $6"
#endif
rcp asm_loc:\$remote_dst_file $3

#else

 # *** Use pre-assembled file ***

#ifdef butterfly

echo "*** Not a SUN3 or HP300; using pre-assembled file ${3}_butterfly"

cp ${3}_butterfly $3

#else

echo "*** Not a SUN3 or HP300; using pre-assembled file ${3}_other"

cp ${3}_other $3

#endif
#endif
#endif
#endif
EOF

chmod +x $script
$script
