#!/bin/bash
shopt -s expand_aliases
alias help='echo -e "Usage: admb [-d] [-r] [-s] model\n
Build AD Model Builder executable from TPL.\n
  -d     Create DLL
  -r     Create ADMB-RE
  -s     Use safe bounds and debugging symbols
  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
###########################################################################################################################
###                                                                                                                       #
### Script:   admb [-d] [-r] [-s] model                                                                                   #
###                                                                                                                       #
### Purpose:  Build ADMB executable from TPL, using tpl2cpp/tpl2rem, adcomp, and adlink                                   #
###                                                                                                                       #
### Args:     -d creates DLL                                                                                              #
###           -r creates ADMB-RE                                                                                          #
###           -s uses safe bounds and debugging symbols                                                                   #
###           model is the filename prefix, e.g. simple                                                                   #
###                                                                                                                       #
### Requires: ADMB header files and libraries, tpl2cpp, tpl2rem, adcomp, adlink                                           #
###                                                                                                                       #
### Returns:  Creates executable or DLL with same prefix                                                                  #
###                                                                                                                       #
### History:  23 May 2009  Arni Magnusson created                                                                         #
###                                                                                                                       #
###########################################################################################################################

# Pop args until model=$1
unset d
unset r
unset s
tpl2cpp=tpl2cpp
unset bounds
unset dll
while getopts "drs" A; do
  case $A in
    d) d=-d; dll=-dll;;
    r) r=-r; tpl2cpp=tpl2rem;;
    s) s=-s; bounds=-bounds;;
  esac
done
shift $[OPTIND-1]

echo -e "\n*** $tpl2cpp $bounds $dll $1"
               $tpl2cpp $bounds $dll $1
echo -e "\n*** adcomp $d $r $s $1"
               adcomp $d $r $s $1
echo -e "\n*** adlink $d $r $s $1"
               adlink $d $r $s $1
