#!/bin/bash
banner[1]='_______________________________________________________________________________'
banner[2]='                                                                               '
banner[3]='                        INSTALLGETS = Install GETSOURCES                       '
banner[4]='              Alexander Men`shchikov, SAp IRFU CEA Saclay, France              '
banner[5]='                                Version 1.140127                               '
banner[6]='_______________________________________________________________________________'
#___________________________________________________________________________________________________________________________________

chk_rc ()
{
# Checking return code and exiting if abnormal termination.
 
  local rc=$?
  if [[ "$rc" != "0" ]]
  then where=$script; if [[ $fun != "" ]]; then where=$where': '$fun; fi; echo; echo ' ERROR in '$where': Aborted.'; exit "$rc"
  fi
}
#___________________________________________________________________________________________________________________________________
#
executing="$0"; scriptname=`basename "$executing"`; script=`echo $scriptname | tr a-z A-Z`; hell='/dev/null'
revision=''; default=''; verb=''

arch=$1; what=$2

  if [[ "$OSTYPE" == "linux-gnu" ]]; then osystem='LINUX-GNU'
elif [[ "$OSTYPE" == "linux"     ]]; then osystem='LINUX'
elif [[ "$OSTYPE" == "darwin"*   ]]; then osystem='MACOSX'
elif [[ "$OSTYPE" == "cygwin"    ]]; then osystem='CYGWIN'
elif [[ "$OSTYPE" == "win32"     ]]; then osystem='WIN32'
elif [[ "$OSTYPE" == "freebsd"*  ]]; then osystem='FREEBSD'
else osystem='UNKNOWN'
fi
if [[ $arch == "" || $arch == "-debug" || $arch == "-revision" || $arch == "-default" ]]; then what=$arch; arch='64'; fi
if [[ $what == "" ]]; then revision='-revision'; default='-default'; fi
if [[ $what == "-debug" ]]; then revision='-revision'; default=''; fi
if [[ $what == "-revision" ]]; then revision='-revision'; default=''; fi
if [[ $what == "-default" ]]; then revision=''; default='-default'; fi

for (( b=1; b <= 6; b++ ))
do echo "${banner[b]}"; done

if [[ $arch != "32" && $arch != "64" && $arch != "-debug" && $arch != "-revision" && $arch != "-default" ]]
then
  echo; echo ' ERROR: Invalid architecture specified on the command line.'; echo; exit 99
fi

if [[ $GETSOURCES_HOME == "" ]]
then
  echo; echo ' ERROR: Environment variable GETSOURCES_HOME not set.'
        echo '        It must point to the home directory (...)/+GETSOURCES.'; echo; exit 99
fi
if [[ $GETSOURCES_BIN == "" ]]
then
  echo; echo ' ERROR: Environment variable GETSOURCES_BIN not set.'
        echo '        It must point to the binary directory (...)/bin/+GETSOURCES.'; echo; exit 99
else
  if [[ $revision == "-revision" ]]
  then
    path=`pwd`; revisiondir=`basename "$path"`; if [[ ${revisiondir:0:1} != "v" ]]; then revisiondir=''; fi
  fi
fi

GETSOURCES_REV=$GETSOURCES_BIN'/'$revisiondir

if [[ -e $GETSOURCES_BIN ]]
then
  echo; echo ' This script will install '$arch'-bit GETSOURCES in:'
  echo
  if [[ $default == "-default" ]]
  then
    echo ' '$GETSOURCES_BIN
  fi
  if [[ $revision == "-revision" ]]
  then
    echo ' '$GETSOURCES_REV
  fi
  if [[ $default == "" && $revision == "" ]]
  then
    echo ' NOWHERE... Code will just be compiled but not installed anywhere.'
  fi
  echo; echo -n ' Press Enter to continue...'; read -s ans; echo
fi
if [[ ! -e '+tools_lib' ]]
then
  echo; echo ' ERROR: This script must be run from the home directory of GETSOURCES.'; echo; exit 99
fi

\cd '+tools_lib' >$hell
'./+complib' $arch tools -verb0; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
cd ..
'./compile' 'cleanbg'   $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'ellipses'  $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'expanda'   $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'fftconv'   $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'fitfluxes' $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'finalcat'  $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'fmeasure'  $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'imgstat'   $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'modfits'   $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'operate'   $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'readhead'  $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'separate'  $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'sfinder'   $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'smeasure'  $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi
'./compile' 'splitcube' $arch $what $verb; if [[ $? -gt 0 ]]; then echo -n ' Press Enter to continue...'; read -s ans; echo; fi

if [[ -e $GETSOURCES_BIN ]]
then
  echo
  echo ' Finishing installation:'
  if [[ $default == "-default" ]]
  then
    echo
    echo ' Copying GETIMAGES    to '$GETSOURCES_BIN'...'; \cp 'getimages'  $GETSOURCES_BIN; chk_rc
    echo ' Copying GETSOURCES   to '$GETSOURCES_BIN'...'; \cp 'getsources' $GETSOURCES_BIN; chk_rc
    echo ' Copying PREPAREOBS   to '$GETSOURCES_BIN'...'; \cp 'prepareobs' $GETSOURCES_BIN; chk_rc
    echo ' Copying RESAMPLE     to '$GETSOURCES_BIN'...'; \cp 'resample'   $GETSOURCES_BIN; chk_rc
    echo ' Copying CONVOLVE     to '$GETSOURCES_BIN'...'; \cp 'convolve'   $GETSOURCES_BIN; chk_rc
    echo ' Copying IOSPEED      to '$GETSOURCES_BIN'...'; \cp 'iospeed'    $GETSOURCES_BIN; chk_rc
    echo ' Copying LIBCFITSIO.A to '$GETSOURCES_BIN'...'; \cp $GETSOURCES_HOME'/libcfitsio.a' $GETSOURCES_BIN; chk_rc
    echo ' Copying TOOLS.A      to '$GETSOURCES_BIN'...'; \cp '+tools_lib/tools.a'  $GETSOURCES_BIN; chk_rc
  fi
  if [[ $revision == "-revision" ]]
  then
    echo
    echo ' Copying GETIMAGES    to '$GETSOURCES_REV'...'; \cp 'getimages'  $GETSOURCES_REV; chk_rc
    echo ' Copying GETSOURCES   to '$GETSOURCES_REV'...'; \cp 'getsources' $GETSOURCES_REV; chk_rc
    echo ' Copying PREPAREOBS   to '$GETSOURCES_REV'...'; \cp 'prepareobs' $GETSOURCES_REV; chk_rc
    echo ' Copying RESAMPLE     to '$GETSOURCES_REV'...'; \cp 'resample'   $GETSOURCES_REV; chk_rc
    echo ' Copying CONVOLVE     to '$GETSOURCES_REV'...'; \cp 'convolve'   $GETSOURCES_REV; chk_rc
    echo ' Copying IOSPEED      to '$GETSOURCES_REV'...'; \cp 'iospeed'    $GETSOURCES_REV; chk_rc
    echo ' Copying LIBCFITSIO.A to '$GETSOURCES_REV'...'; \cp $GETSOURCES_HOME'/libcfitsio.a' $GETSOURCES_REV; chk_rc
    echo ' Copying TOOLS.A      to '$GETSOURCES_REV'...'; \cp '+tools_lib/tools.a'  $GETSOURCES_REV; chk_rc
  fi
  if [[ $default == "-default" ]]
  then
    echo
    echo ' Copying INSTALLATION.GUIDE to '$GETSOURCES_BIN'...'; \cp 'INSTALLATION.GUIDE' $GETSOURCES_BIN; chk_rc
    echo ' Copying QUICK.START.GUIDE  to '$GETSOURCES_BIN'...'; \cp 'QUICK.START.GUIDE'  $GETSOURCES_BIN; chk_rc
    echo ' Copying USERS.CHECKLIST    to '$GETSOURCES_BIN'...'; \cp 'USERS.CHECKLIST'    $GETSOURCES_BIN; chk_rc
  fi
  if [[ $revision == "-revision" ]]
  then
    if [[ $default == "" ]]
    then
      echo
    fi
    echo ' Copying INSTALLATION.GUIDE to '$GETSOURCES_REV'...'; \cp 'INSTALLATION.GUIDE' $GETSOURCES_REV; chk_rc
    echo ' Copying QUICK.START.GUIDE  to '$GETSOURCES_REV'...'; \cp 'QUICK.START.GUIDE'  $GETSOURCES_REV; chk_rc
    echo ' Copying USERS.CHECKLIST    to '$GETSOURCES_REV'...'; \cp 'USERS.CHECKLIST'    $GETSOURCES_REV; chk_rc
  fi
fi
echo '_________________________________________________________________'
echo
echo ' Please read the files QUICK.START.GUIDE and USERS.CHECKLIST     '
echo '_________________________________________________________________'

echo
echo ' Done.'
exit
