#!/bin/csh -f

# simple script to avoid having to type several commands
# this also cleans up the directory leaving only a shared object

# if a file "fname.stub" is present, it will use that to generate the wrapper
#      else  
# it will assume that nclfortstart/nclend are in the fcode.f file

# USAGE:   Wrapit fname.f
# EXAMPLE: Wrapit poly.f      
# OUTPUT:  poly.so   [a shared object: ".so" suffix is arbitrary]

set EXTEN = $1:e
if ($EXTEN != "f") then
    echo "Wrapit: {$1} does not have the fortran extension"
    exit
endif
set FNAME = $1:r

#  create the C interface between NCL and fortran
if (-e {$FNAME}.stub) then
    echo "$FNAME.stub"
    wrapit77 < {$FNAME}.stub >! {$FNAME}_W.c
else
    echo " "
    echo "wrapit77 < {$FNAME}.f >! {$FNAME}_W.c"
    echo " "
    wrapit77 < $FNAME.f >! {$FNAME}_W.c
endif

# C compile
echo " "
echo "nhlcc  -c {$FNAME}_W.c"
echo " "
nhlcc  -c {$FNAME}_W.c

# fortran compile   [nhlf77 also can be used]
echo " "
echo "nhlf90 -c  $1"
echo " "
nhlf90 -c  $1

#  remove any previous shared object
if (-e {$FNAME}.so) then
    'rm' {$FNAME}.so
endif

#  ld all the required files
echo " "
echo "ld -64 -o {$FNAME}.so -shared {$FNAME}_W.o {$FNAME}.o -lfortran"
echo " "
ld -64 -o {$FNAME}.so -shared {$FNAME}_W.o {$FNAME}.o -lfortran

#  clean uo
'rm' {$FNAME}_W.c
'rm' {$FNAME}_W.o
'rm' {$FNAME}.o
'rm' so_locations

exit
