#! /bin/sh
#+
#
# NAME
# ====
#
# docompileall.sh - compile all IDL modules which define PRO or FUNCTION
#
# SYNOPSIS
# ========
#
# ::
#
# $ docompileall.sh -i indir -idl_dir idl_dir
#
# DESCRIPTION
# ===========
#
# ``docompileall.sh`` scans indir to find .pro files which define PRO or FUNCTION.
#
# -i directory input
#
# On each file found, idl is asked to ++ compile.
#
# By default, indir is the current directory.
#
# CAUTIONS
# ========
#
# SAXO_DIR must be set. The value should be the directory where
# SAXO is available.
#
# for example, if you work at LOCEAN and work with default SAXO,
# SAXO_DIR=/usr/home/smasson/SAXO_DIR.
#
# For working in progress, you should checkout SAXO on a personnal
# directory and set SAXO_DIR to the working space.
#
# example for me : SAXO_DIR=${HOME}/SAXO_DIR_ws

# EXAMPLES
# ========
#
# For SAXO in working space ${HOME}/SAXO_DIR_ws/ :
#
# ::
#
# $ ${SAXO_DIR}/SRC/Documentation/xmldoc/docompileall.sh -i ${HOME}/SAXO_DIR_ws/SRC/ -idl_dir /usr/local_linux/idl/idl_6.4/idl/
#
# For VARAMMA in working space /usr/home/fplod/incas/varamma/varamma_ws/
#
# ::
#
# $ ${SAXO_DIR}/SRC/Documentation/xmldoc/docompileall.sh -i /usr/home/fplod/incas/varamma/varamma_ws/ -idl_dir /usr/local_linux/idl/idl_6.4/idl/
#
#
# For POST_IT in working space /usr/lodyc/incas/fplod/post_it_ws/
# with IDL 6.4
#
# ::
#
# $ ${SAXO_DIR}/SRC/Documentation/xmldoc/docompileall.sh -i /usr/lodyc/incas/fplod/post_it_ws/ -idl_dir /usr/local_linux/idl/idl_6.4/idl/
#
# For POST_IT in working space /usr/lodyc/incas/fplod/post_it_ws/
# with IDL 7.0
#
# ::
#
# $ ${SAXO_DIR}/SRC/Documentation/xmldoc/docompileall.sh -i /usr/lodyc/incas/fplod/post_it_ws/ -idl_dir /usr/local_linux/idl/idl70/
#
# TODO
# ====
#
# exclude .svn directory in find command
#
# better handling on DEMO mode (ie information cant check)
#	
# be less sensible to multiple / and trailing / in indir and SAXO_DIR when
# compare
#
# add a parameter excludedir to exclude unwanted files like ./tools/ in POST_IT.#
# EVOLUTIONS
# ==========
#
# $Id$
#
# - fplod 20091116T142010Z aedon.locean-ipsl.upmc.fr (Darwin)
#   
#   * remove dup if only SAXO
#
# - fplod 20091113T151637Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * idl_dir in parameter
#
# - fplod 20091110T134500Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * creation from savesaxo.sh docompileall part
#   * make it work from anywhere
#
#-
#
system=$(uname)
case "${system}" in
 AIX|IRIX64)
 echo " www : no specific posix checking"
 ;;
 *)
 set -o posix
 ;;
esac
command=$(basename ${0})
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
log=/tmp/$(basename ${command} .sh).log.${log_date}
#
usage=" Usage : ${command} -i indir -idl_dir idl_dir"
#
minargcount=4 #++
#echo " narg ${#}"
if [ ${#} -lt ${minargcount} ]
then
 echo "eee : not enought arguments (${#} vs ${minargcount})"
 echo "${usage}"
 exit 1
fi
#
# default
# default IDL command and directory
#idl_dir=/usr/local_linux/idl/idl64/ # overriding idl_dir for test ++
#
# default indir directory
indir=$(pwd)
#
# read parameters on command line
while [ ! -z "${1}" ]
do
 case ${1} in
 -i)
 indir=${2}
 shift
 ;;
 -idl_dir)
 idl_dir=${2}
 shift
 ;;
 -h)
 echo "${usage}"
 exit 0
 ;;
 *) # other choice
 echo "${usage}"
 exit 1
 ;;
 esac
 shift # next flag
done
#
set -u
#
# test in ${indir} exist
if [ ! -d ${indir} ]
then
 echo "eee : ${indir} not found"
 exit 1
fi
#
# ++ test si IDL (car GDL inutilisable)
application=${idl_dir}/bin/idl
#
# start to write IDL commands file
cat <<EOF > /tmp/docompileall${$}.pro
.reset_session
;
; Add SAXO sources to IDL path
!path = expand_path('+' + '${SAXO_DIR}/SRC/') + ':' + expand_path('+' + !dir)
EOF
#
# if indir is equal to SAXO_DIR/SRC there is no need to add it in IDL path
if [ "${indir}" != "${SAXO_DIR}/SRC/" ]
then
 cat <<EOF >> /tmp/docompileall${$}.pro
!path = expand_path('+' + '${indir}') + ':' + !path
EOF
fi
#
cat <<EOF >> /tmp/docompileall${$}.pro
resolve_all, resolve_either='def_myuniquetmpdir', skip_routines = ['xxx2ps','trends', 'label_gmt']
resolve_all, resolve_either = 'find', skip_routines = ['xxx2ps','trends', 'label_gmt']
def_myuniquetmpdir
oldcm = find('oldcm_empty')
;oldcm = find('oldcm_full')
oldcm = oldcm[0]
@cm_general
file_copy, oldcm, myuniquetmpdir + 'oldcm_used.pro', /overwrite
!path = myuniquetmpdir + ':' + !path
IF !journal NE 0 THEN journal
journal, myuniquetmpdir + 'cm_demomode_used.pro'
journal
path_cache, /rebuild
;
resolve_all, resolve_either='keep_compatibility', skip_routines = ['xxx2ps','trends', 'label_gmt']
;
EOF
#
# find .pro files
list_files=$(find ${indir} -name "*.pro" | grep -iv label_gmt | grep -iv report)
#
# check if at least one file found
if [ "${list_files}" = "" ]
then
 echo "eee : no . pro files found under ${indir}"
 exit 1
fi
# 
# loop on each file in the list to select PRO and FUNCTION modules
# ie exclude common files
for i in ${list_files}
do
 # does it contains pro or function ?
 egrep -iq '^ *	*(pro|function)' ${i}
 if [ ${?} -eq 0 ]
 then
  echo "print, '$( basename ${i} .pro ) :'" >> /tmp/docompileall${$}.pro
  echo "resolve_all, resolve_either = '$( basename ${i} .pro )', skip_routines = ['xxx2ps','trends', 'label_gmt', 'report']" >> /tmp/docompileall${$}.pro
 fi
done
echo "exit" >> /tmp/docompileall${$}.pro
#
#more /tmp/docompileall${$}.pro 
export IDL_STARTUP=/tmp/docompileall${$}.pro
${application} &> /tmp/docompileallerr_${$}
grep -i "error" /tmp/docompileallerr_${$}
# grep -q "Compilation error(s)" /tmp/docompileallerr_${$}
if [ ${?} -ne 0 ]
then
 echo "all files are OK"
 # clean
 rm -f /tmp/docompileallerr_${$} /tmp/docompileall${$}.pro
else
 grep "Compilation error(s)" /tmp/docompileallerr_${$}
 echo "eee : see /tmp/docompileallerr_${$}"
 more /tmp/docompileallerr_${$}
 exit 1
fi

# end
exit 0
