#!/bin/bash
shopt -s expand_aliases
alias help='echo -e "Usage: adcomp [-d] [-g] [-r] [-s] model\n
Compile AD Model Builder C++ to object code.\n
  -d     Create DLL
  -g     Insert debugging symbols
  -r     Create ADMB-RE
  -s     Enforce safe bounds
  model  Filename prefix, e.g. simple\n"'
if [[ "$1" == "" ]]; then help; exit; fi
if [[ "$1" == "-help" ]]; then help; exit; fi
if [[ "$1" == "--help" ]]; then help; exit; fi

# Pop args until model=$1
unset dll
opt=-DOPT_LIB
sym=-O3
while getopts "dgrs" A; do
  case $A in
    d) dll=-DBUILDING_DLL;;
    g) sym=-g;;
    r) ;;
    s) opt=-DSAFE_ALL;;
  esac
done
shift $((OPTIND-1))

model="${1%.*}"

CMD="g++ -c $CXXFLAGS $sym -Wno-deprecated -D__GNUDOS__ $dll -Dlinux $opt\
 -DUSE_LAPLACE -fpermissive -I. -I\"$ADMB_HOME\"/include -I\"$ADMB_HOME\"/contrib $model.cpp"
echo $CMD
eval $CMD

exit 0

### r259 [2012-02-29] arnima  logged revision history
### r222 [2012-01-12] johnoel added $ADMB_HOME/contrib as include directory
### r 42 [2011-06-22] arnima  improved handling of CXXFLAGS
### r 39 [2011-06-22] johnoel improved handling of CXXFLAGS
### r982 [2010-02-16] arnima  rewrite, updated OPTIND structure, if -g then not
###                           -O3, fixed spaces, simplified echo
### r938 [2010-12-28] johnoel put CXXFLAGS before other options
### r901 [2010-12-22] johnoel moved to 'g++' dir
### r860 [2010-11-18] johnoel hardwired -O3
### r784 [2010-09-24] johnoel removed -O3
### r662 [2010-06-17] johnoel added CXXFLAGS and exit status, simplified echo
### r640 [2010-05-22] johnoel added -DSAFE_ALL
### r524 [2010-03-17] arnima  added support for filename extension like
###                           simple.cpp
### r503 [2010-02-08] johnoel split -s option into separate -g and -s options,
###                           if -g then not -O3
### r243 [2009-05-27] arnima  created
