#! /bin/sh
#+
#
# NAME
# ====
#
# docompileall.sh - compile all IDL modules which define PRO or FUNCTION
#
# SYNOPSIS
# ========
#
# ::
#
# $ docompileall.sh -i indir
#
# 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/
#
# 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/
#
#
# For POST_IT in working space /usr/home/fplod/incas/post_it/post_it_ws/
#
# ::
#
# $ ${SAXO_DIR}/SRC/Documentation/xmldoc/docompileall.sh -i /usr/home/fplod/incas/post_it/post_it_ws/
#
# TODO
# ====
#
# exclude .svn directory in find command
#
# better handling on DEMO mode (ie information cant check)
#	
# EVOLUTIONS
# ==========
#
# $Id$
#
# - 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"
#
minargcount=0 #++
#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_command=$(which idl)
idl_dir=$(dirname ${idl_command})
#
# default indir directory
indir=$(pwd)
#
# read parameters on command line
while [ ! -z "${1}" ]
do
 case ${1} in
 -i)
 indir=${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
#
cat <<EOF > /tmp/docompileall${$}.pro
; test DEMO mode
demo_on=LMGR(/DEMO)
IF demo_on EQ 1 THEN BEGIN
 exit 
ENDIF
.reset_session
;
!path = expand_path('+' + '${SAXO_DIR}/SRC/') + ':' + expand_path('+' + !dir)
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
#
more /tmp/docompileall${$}.pro
read a
# 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
#
IDL_STARTUP=/tmp/docompileall${$}.pro
${idl_command} &> /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
