#! /bin/csh
#----------
#********************************************************
#
#   Copyright (c) 1995-2002  Ralf Giering
#
# purpose:      use TAMC remotely
# by:		Ralf Giering
# written:	 7. 6.1996
# last changes	18.3.1999
#
# usage: stamc [OPTIONS] <filenames>
#
#********************************************************

#---------------------------------------
# set server
#---------------------------------------
set SERVER      = autodiff.com
set TAMC_EMAIL  = tamc@${SERVER}
set SERVER_ID   = tamc

#---------------------------------------
# set full path of sendmail program
#---------------------------------------
set SENDMAIL    = /usr/lib/sendmail

#---------------------------------------
# set remote shell command
# default is 'rsh' on Cray 'remsh'
#---------------------------------------
set REMSH = `which remsh`
if ( -x "${REMSH}" ) then
  set RSH = remsh
else
  set RSH = rsh
endif

#---------------------------------------
# TAMC output
#---------------------------------------
echo '_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/'
echo '_/                                                                        _/'
echo '_/  T angent linear and                                                   _/'
echo '_/   A djoint                                            Version 5.3.2    _/'
echo '_/    M odel                                                              _/'
echo '_/     C ompiler                                                          _/'
echo '_/                                                                        _/'
echo '_/  Copyright (c) 1995-2002                              Ralf Giering     _/'
echo '_/                                                                        _/'
echo '_/ THE AUTHOR DOES NOT MAKE  ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES _/'
echo '_/ ANY LEGAL LIABILITY OR  RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, _/'
echo '_/ OR USEFULNESS  OF ANY INFORMATION OR PROCESS  DISCLOSED, OR REPRESENTS _/'
echo '_/ THAT ITS USE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS.                _/'
echo '_/                                                                        _/'
echo '_/ THE GENERATED CODE IS  FOR NON-PROFIT-ORIENTATED ACADEMIC RESEARCH AND _/'
echo '_/ EDUCATION  ONLY.  ANY  COMMERCIAL  OR OTHER  PROFIT-ORIENTATED USE  OR _/'
echo '_/ EVALUATION IS STRICTLY FORBIDDEN.  PASSING THE CODE TO ANY THIRD PARTY _/'
echo '_/ IS NOT ALLOWED.                                                        _/'
echo '_/                                                                        _/'
echo '_/ FOR COMMERCIAL OR  OTHER PROFIT-ORIENTATED APPLICATIONS PLEASE CONTACT _/'
echo '_/  info@FastOpt.com                                                      _/'
echo '_/                                                                        _/'
echo '_/ By using TAMC you are accepting the conditions above.                  _/'
echo '_/ Any usage is logged.                                                   _/'
echo '_/                                                                        _/'
echo '_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/'
echo

#---------------------------------------
# give usage if no argument was given
#---------------------------------------
if ($#argv == 0) then
   echo ' error: no file names specified'
   echo ''
   echo " usage:   `basename $0` [options] <filenames>"
   echo ''
   echo " Use '`basename $0` -help' for a complete list of options."
   echo ''
   exit 1
endif

#---------------------------------------
# check options
#---------------------------------------
set ERROR	= 0
set FILES       =
set COMMAND	= tamc
set OPTIONS	=
set REPLY	=

while ("X$argv[1]" != "X--")
	switch ($argv[1])

	case -help:
echo ' options :   kind     [default]  comment'
echo ''
echo ' -reply      string   [NONE]     set reply address, i.e. your email'
echo ''
echo ' -l          string   [NONE]     set name of listing file'
echo ' -u          boolean  [off]      make the default type be undefined'
echo ' -i2         boolean  [off]      make integers be two bytes by default'
echo ' -i4         boolean  [off]      make integers be four bytes by default'
echo ' -i8         boolean  [off]      make integers be eight bytes by default'
echo ' -r4         boolean  [off]      make reals be four bytes by default'
echo ' -r8         boolean  [on]       make reals be eight bytes by default'
echo ' -e          boolean  [off]      accept and write extended source lines (132)'
echo ' -N          integer  [72]       line width in columns'
echo ' -U          boolean  [off]      destinguish between upper and lower case'
echo ' -V          boolean  [off]      print TAMC version and date'
echo ' -maxerrors  integer  [30]       maximal number of errors reported'
echo ' -toplevel   string   [model]    name of routine to differentiate'
echo ' -module     string   [model]    synonym for -toplevel'
echo ' -input      string   []         list of independent (control) variables'
echo ' -output     string   []         list of dependent variables'
echo ' -reverse    boolean  [off]      operate in reverse mode'
echo ' -adjoint    boolean  [off]      generate adjoint code synonym for -reverse'
echo ' -forward    boolean  [off]      operate in forward mode'
echo ' -pure       boolean  [off]      generate code to compute derivatives only'
echo ' -jacobian   integer  [0]        number of vectors multiplied by jacobian'
echo ' -append     boolean  [off]      append additional index'
echo ' -ldg        boolean  [off]      add leading dimension arguments'
echo ' -arglist    boolean  [off]      fixed argument list of adjoint top-level'
echo ' -unit       integer  [60]       lowest unit number used for file tapes'
echo ' -closetapes boolean  [off]      close tape files after each operation'
echo ' -split      boolean  [off]      additional expression splitting'
echo ' -depend     boolean  [on]       apply data dependence analysis'
echo ' -auxmark    string   [h]        set mark for auxiliary names'
echo ' -admark     string   [ad]       set mark for adjoint names'
echo ' -modmark    string   [md]       set mark for modified routine names'
echo ' -ftlmark    string   [g_]       set mark for tangent linear names'
echo ' -postfix    boolean  [off]      append mark to name'
echo ' -f90        boolean  [off]      read and write code for Fortran-90 compiler'
echo ' -hpf        boolean  [off]      read and write High Performance Fortran'
echo ' -free       boolean  [off]      all input files are in free source format'
echo ' -fixed      boolean  [off]      all input files are in fixed source format'
echo ' -dynamic    boolean  [off]      Fortran compiler supports dynamic arrays'
echo ' -vector     boolean  [off]      Fortran compiler supports array assignments'
echo ' -doenddo    boolean  [on]       Fortran compiler supports DO ENDDO statement'
echo ' -dowhile    boolean  [on]       Fortran compiler supports DO WHILE statement'
echo ' -ivdep      boolean  [off]      write CDIR$ IVDEP directives'
echo ' -keep       boolean  [off]      keep not required statements'
echo ' -direc      string   [NONE]     write new directives into file'
echo ' -info       boolean  [on]       give all information'
echo ' -warning    boolean  [on]       give all warnings'
echo ' -warn_step  boolean  [off]      notice non differentiable statements'
echo ''
exit 1
	  set argv        = --
	  breaksw

	case -reply:
	  shift argv
	  set REPLY = "-f $argv[1]"; shift argv; breaksw

	case -[ueUV]:
	case -i[248]:
	case -r[48]:
	case -reverse:
	case -noreverse:
	case -adjoint:
	case -noadjoint:
	case -forward:
	case -pure:
	case -append:
	case -ldg:
	case -arglist:
	case -closetapes:
	case -split:
	case -depend:
	case -postfix:
	case -f90:
	case -fixed:
	case -free:
	case -dynamic:
	case -nodynamic:
	case -vector:
	case -novector:
	case -doenddo:
	case -nodoenddo:
	case -dowhile:
	case -nodowhile:
	case -ivdep:
	case -keep:
	case -nokeep:
	case -info:
	case -noinfo:
	case -warning:
	case -nowarning:
	case -warn_step:
	case -nowarn_step:
	case -common:
	  set OPTIONS = "$OPTIONS $argv[1]"; shift argv;
	  breaksw

	case -l:
	case -N:
	case -maxerrors:
	case -module:
	case -toplevel:
	case -jacobian:
	case -unit:
	case -auxmark:
	case -admark:
	case -modmark:
	case -ftlmark:
	case -direc:
	  set OPTIONS = "$OPTIONS $argv[1]"; shift argv;
	  set OPTIONS = "$OPTIONS $argv[1]"; shift argv;
	  breaksw

	case -input:
	case -output:
	  set OPTIONS = "$OPTIONS $argv[1]"; shift argv;
	  set OPTIONS = "$OPTIONS `echo ${argv[1]}|sed -e 's/ /,/g'`"; shift argv;
	  breaksw

	case -*:
	  echo " undefined option $argv[1]"
	  echo ''
	  set ERROR	= 1
	  shift argv
	  breaksw
	default:
	  set FILES       = "$argv[*]"
	  set argv        = --
	  breaksw

	endsw
   if ( $#argv == 0) then
     set argv = --
   endif
end

#---------------------------------------
# get all file names
#---------------------------------------
shift argv

if ( "$FILES" == "" ) then
   set FILES	= "$argv[*]"
else
   set FILES	= "$FILES $argv[*]"
endif

#---------------------------------------
# check on files exist
#---------------------------------------
if ( "$FILES" == "" ) then
   echo ' error: no file names specified'
   echo ''
   exit 2
endif

foreach i (`echo $FILES`)
  if ( ! -f $i ) then
    echo " file not found: $i"
    echo ''
    exit 3
  endif
end

#---------------------------------------
# exit on unknown option error
#---------------------------------------
if ( $ERROR != 0 ) then
   exit 4
endif

#---------------------------------------
# save command line
#---------------------------------------
echo "${COMMAND} ${OPTIONS} ${FILES}" >! tamc_command

#---------------------------------------
# set info line
#---------------------------------------
set  IDENT = tamc_ident
set  DATE = `date +'%y%m%d:%H.%M'`
set  WAMI = `whoami`
set  HOST = `hostname`
set  DOMA = `domainname`
set  INET = `grep ${HOST} /etc/hosts | head -1 | awk '{print $1}'`
set  NAME = `finger -ms ${WAMI} | tail -1 | awk '{print $2,$3}'`
set  COMM = "${COMMAND} ${OPTIONS}"
echo "${DATE} ${WAMI} ${HOST} ${DOMA} ${INET} '${NAME}' ${COMM}" >! ${IDENT}

#---------------------------------------
# email access
#---------------------------------------
  gzip -h >& /dev/null
  if ( ${status} == 0 ) then
    set COMPRESS  = 'gzip'
    set COMPR_EXT = gz
  else
    set COMPRESS  = 'compress'
    set COMPR_EXT = Z
  endif
  set ENCODE      = "uuencode tamc_mail.tar.${COMPR_EXT}"

  tar cf - ${IDENT} ${FILES} tamc_command \
  | ${COMPRESS} | ${ENCODE} >! tamc_mail

  echo 'Subject: apply tamc' | cat - tamc_mail | ${SENDMAIL} ${REPLY} ${TAMC_EMAIL}

  echo "  Mail to ${TAMC_EMAIL} sent."
  echo "  Use rtamc <mail_file> to extract reply."
  echo "  Command line is saved in tamc_command."
  echo ""

#---------------------------------------
# normal exit
#---------------------------------------
rm -f ${IDENT}

exit
