#! /bin/sh
#
#+
# .. _geomag_profile.sh:
#
# =================
# geomag_profile.sh
# =================
#
# -------------------------
# define GEOMAG environment
# -------------------------
#
# SYNOPSIS
# ========
#
# Online usage
#
# .. code-block:: bash
#
#    . geomag_profile.sh -d directory -i indir -o outdir -t tempdir
#
# In :file:`${HOME}/.profile`, add the following line:
#
# .. code-block:: bash
#
#    . geomag_profile.sh -d directory -i indir -o outdir -t tempdir
#
# DESCRIPTION
# ===========
#
# .. option:: -d <directory>
# .. option:: -i <indir>
# .. option:: -o <outdir>
# .. option:: -t <tempdir>
#
# define GEOMAG environment
#
# ${GEOMAG} is the base directory of tools.
#
# ${GEOMAG_LOG} is the directory where log files will be written.
#
# ${GEOMAG_ID} is the directory where input files must be.
#
# ${GEOMAG_OD} is the directory where output files will be written.
#
# MANPATH++
#
# EXAMPLES
# ========
#
# For fplod, on aedon.locean-ipsl.upmc.fr:
#
# .. code-block:: bash
#
#    cd /usr/home/fplod/incas/geomag/geomag_ws
#    . geomag_profile.sh -d /usr/home/fplod/incas/geomag/geomag_ws/ -i ${HOME}/geomag_d/ -o ${HOME}/geomag_d/ -t /usr/temp/${LOGNAME}/log/
#
# For reee522 on rhodes.idris.fr:
#
# .. code-block:: bash
#
#    cd ${HOME}/incas/geomag/geomag_ws/
#    . geomag_profile.sh -d ${HOME}/incas/geomag/geomag_ws/ -i ${HOMEGAYA}/geomag_d/ -o ${HOMEGAYA}/geomag_d/ -t /tmp/${LOGNAME}/log/
#
# TODO
# ====
#
# ++ option bavarde
#
# ++ machine dependant
#
# ++ besoin de posix
#
# ++ pas de MANPATH defini par défaut sur zeus
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# - fplod 20100426T094821Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * improve PATH and MANPATH modification
#
# - fplod 20100423T143134Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * add error handling on mkdir
#
# - fplod 20100318T073619Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * unset
#
# - reee522 2007-06-14T14:25:00Z rhodes (IRIX64)
#
#   * add -i, -o and -t parameters to be more flexible
#   * some bug fixes
#   * more checking
#
# - fplod 2007-03-13T16:12:41Z zeus.locean-ipsl.upmc.fr (Linux)
#
#   * add creation of ${GEOMAG_ID} and ${GEOMAG_OD} if they don't exist
#
# - reee522 2006-11-23T11:41:41Z rhodes (IRIX64)
#
#   * pas de set -o posix sur rhodes (IRIX64)
#
# - fplod 2006-03-22T10:13:38Z aedon.lodyc.jussieu.fr (Darwin)
#
#   * creation
#
#-
#
command=geomag_profile.sh
#
usage=" Usage : ${command} -d directory -i indir -o outdir -t tempdir"
#
set -u
pb=0
#
minargcount=8
maxargcount=8
narg=${#}
#
if [ ${narg} -lt ${minargcount} ]
then
    echo "eee : not enought arguments (${narg} vs ${minargcount})"
    echo "${usage}"
    # nb : no exit because this file should be launched by login process
    pb=1
fi
unset minargcount
#
if [ ${narg} -gt ${maxargcount} ]
then
    echo "eee : too many arguments (${narg} vs ${maxargcount})"
    echo "${usage}"
    # nb : no exit because this file should be launched by login process
    pb=1
fi
unset maxargcount
unset narg
#
if [ ${pb} -eq 0 ]
then
    #
    while [ ${#} -gt 0 ]
    do
       case ${1} in
          -d)
             # directory for application choosen by user 
             # (see svn checkout command used)
             directory=${2}
             shift
          ;;
          -i)
             # directory for inputs choosen by user
             indir=${2}
             shift
          ;;
          -o)
             # directory for outputs choosen by user
             outdir=${2}
             shift
          ;;
          -t)
             # directory for temporary outputs choosen by user
             tempdir=${2}
             shift
          ;;
          *)
             # other choice
             echo "eee : unknown option ${1}"
             echo "${usage}"
             # nb : no exit because this file should be launched by login process
          ;;
       esac
       # next flag
       shift
    done
    unset usage
fi
if [ ${pb} -eq 0 ]
then
    #
    # check for ${directory}
    if [ ! -d ${directory} ]
    then
       echo " eee : ${directory} not found"
       # nb : no exit because this file should be launched by login process
    fi
    #
    # check for permission on directory
    if [ ! -x ${directory} ]
    then
       echo " eee : ${directory} not reachable"
       # nb : no exit because this file should be launched by login process
    fi
    #
    system=$(uname)
    case "${system}" in
       IRIX64)
          echo " www : no specific posix checking"
       ;;
       *)
          set -o posix
       ;;
    esac
    unset system
    #
    GEOMAG=${directory}
    export GEOMAG
    unset directory
    #
    # add GEOMAG tools to PATH
    # if not already done
    suppath=$(echo ${GEOMAG} | tr -s "/")
    echo ${PATH} | grep -q "${suppath}:"
    test_path=${?}
    if [ ${test_path} -ne 0 ]
    then
       PATH=${suppath}:${PATH}
       export PATH
    else
       # option bavarde oui/non pas encore implantée ++
       echo "${command} : iii : ${suppath}/ already in \${PATH}"
    fi
    unset test_path
    unset suppath
    #
    # add GEOMAG manuals to MANPATH
    # if not already done
    suppath=$(echo ${GEOMAG}/doc/manuals/man/ | tr -s "/")
    echo ${MANPATH} | grep -q "${suppath}:"
    test_manpath=${?}
    if [ ${test_manpath} -ne 0 ]
    then
       MANPATH=${suppath}:${MANPATH}
       export MANPATH
    else
       # option bavarde oui/non pas encore implantée ++
       echo "${command} : iii : ${suppath} already in \${MANPATH}"
    fi
    unset test_manpath
    unset suppath
    #
    GEOMAG_LOG=${tempdir}
    export GEOMAG_LOG
    unset tempdir
    if [ ! -d ${GEOMAG_LOG} ]
    then
       mkdir -p ${GEOMAG_LOG}
       status=${?}
       if [ ${status} -ne 0 ]
       then
          echo "${command} : eee : can not create \${GEOMAG_LOG}"
          # nb : no exit because this file should be launched by login process
       else
          echo "${command} : iii : creation of \${GEOMAG_LOG}"
       fi
       unset status
    fi
    # check for permission on GEOMAG_LOG
    if [ ! -x ${GEOMAG_LOG} ]
    then
       echo " eee : ${GEOMAG_LOG} not reachable"
       # nb : no exit because this file should be launched by login process
    fi
    #
    # check for permission on GEOMAG_LOG
    if [ ! -w ${GEOMAG_LOG} ]
    then
       echo " eee : ${GEOMAG_LOG} not writable"
       # nb : no exit because this file shouldreachable be launched 
       # by login process
    fi
    #
    EDITOR=vi
    export EDITOR
    #
    # io directories
    GEOMAG_ID=${indir}
    export GEOMAG_ID
    unset indir
    if [ ! -d ${GEOMAG_ID} ]
    then
       mkdir -p ${GEOMAG_ID}
       echo "${command} : iii : creation of \${GEOMAG_ID}"
    fi
    # check for permission on GEOMAG_ID
    if [ ! -x ${GEOMAG_ID} ]
    then
       echo " eee : ${GEOMAG_ID} not reachable"
       # nb : no exit because this file should be launched by login process
    fi
    #
    GEOMAG_OD=${outdir}
    export GEOMAG_OD
    unset outdir
    if [ ! -d ${GEOMAG_OD} ]
    then
       mkdir -p ${GEOMAG_OD}
       echo "${command} : iii : creation of \${GEOMAG_OD}"
    fi
    # check for permission on GEOMAG_OD
    if [ ! -x ${GEOMAG_OD} ]
    then
       echo " eee : ${GEOMAG_OD} not reachable"
       # nb : no exit because this file should be launched by login process
    fi
    if [ ! -w ${GEOMAG_OD} ]
    then
       echo " eee : ${GEOMAG_OD} not writable"
       # nb : no exit because this file should be launched by login process
    fi
    #
fi
unset pb
# end
unset command
# nb : no exit because this file should be launched by login process
