#! /bin/sh -x
#+
#
# .. program:: install_superbib.sh
#
# .. _install_superbib.sh:
#
# ===================
# install_superbib.sh
# ===================
#
# DESCRIPTION
# ===========
#
# installation of project
#
# SYNOPSIS
# ========
#
# .. code-block:: sh
#
#    install_superbib.sh -lf loginforgeipsl -d directory
#
# DESCRIPTION
# ===========
#
# .. option:: -lf  <login on forge.ipsl.jussieu.fr>
#
# .. option:: -d   <directory>
#
#    where executable, input and output will be written
#
# On the first usage of this tool, :file:`${tempdir}/for_install_superbib/`
# will be created.
# svn forge will be checked out in this directory.
#
# On locean-ipsl.upmc.fr, :file:`${tempdir}` is set to
# :file:`/usr/temp/${LOGNAME}/`.
#
# Elsewhere, :file:`${tempdir}` is set to :file:`/tmp/${LOGNAME}/`.
#
# ++Once done, experience can be launched using :ref:`project_run.sh`.
#
# ++To reset everything, you need to manualy remove :file:`${tempdir}/for_install_superbib/`.
#
# ++Log file is written on
# :file:`${PROJECT_LOG}/install_superbib.sh.log.{YYYYMMDDTHHMMSSZ}`.
#
# CAUTIONS
# ========
#
# Must be launch interactively because of password issue.
#
# EXAMPLES
# ========
#
# For installation on cratos
#
# .. code-block:: sh
#
#    install_superbib.sh \
#    -lf pinsard \
#    -d ${HOME}/SUPERBIB/
#
# SEE ALSO
# ========
#
# :ref:`install`
#
# TODO
# ====
#
# ipsl climserv

# EVOLUTIONS
# ==========
#
# $Id: install_superbib.sh 106 2011-12-20 16:20:29Z pinsard $
#
# $URL$
#
# - fplod 20120227
#
#   * creation
#
#-
command=$(basename ${0})
system=$(uname)
case "${system}" in
    AIX|IRIX64)
        echo "${command} : www : no specific posix checking"
    ;;
    *)
        set -o posix
    ;;
esac
unset system
#
set -u
#
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
#
# check for ${PROJECT_LOG} definition
if [ "${PROJECT_LOG}" = "" ]
then
    echo "${command} : eee : \${PROJECT_LOG} not defined"
    exit 1
fi
#
# check for ${PROJECT_LOG} existence
if [ ! -d ${PROJECT_LOG} ]
then
    echo "${command} : eee : ${PROJECT_LOG} not found"
    exit 1
fi
#
# check for permission access on PROJECT_LOG
if [ ! -x ${PROJECT_LOG} ]
then
    echo "${command} : eee : ${PROJECT_LOG} not reachable"
    exit 1
fi
#
# check for write permission on PROJECT_LOG
if [ ! -w ${PROJECT_LOG} ]
then
    echo "${command} : eee : ${PROJECT_LOG} not writable"
    exit 1
fi
#
log=${PROJECT_LOG}/$(basename ${command} .sh).log.${log_date}
#
usage=" Usage : ${command} -lf loginforgeipsl -d directory"
#
# default
hostname=$(hostname --fqdn)
make_command=gmake
svn_forge=1
#
minargcount=0
if [ ${#} -lt ${minargcount} ]
then
    echo "${command} : eee : not enough arguments"
    echo "${usage}"
    exit 1
fi
unset minargcount
#
while [ ${#} -gt 0 ]
do
    case ${1} in
        -lf)
            loginforgeipsl=${2}
            shift
        ;;
        -h)
            hostname=${2}
            shift
        ;;
        -d)
            directory=${2}
            shift
        ;;
        *)
            # anything else
            echo "${command} : eee : unknown option ${1}"
            echo "${command} : eee : ${usage}"
            exit 1
        ;;
    esac
    # next flag
    shift
done
#
unset usage
#
echo "[Context]" 1>> ${log}
echo "command=${command}" 1>> ${log}
echo "hostname=${hostname}" 1>> ${log}
echo "runtime=${log_date}" 1>> ${log}
#
echo "" 1>> ${log}
echo "[Parameters]" 1>> ${log}
echo "directory=${directory}" 1>> ${log}
echo "svn_forge=${svn_forge}" 1>> ${log}
echo "" 1>> ${log}
#
# check parameters
#
if [ ${svn_forge} -eq 1 ]
then
    if [ "${loginforgeipsl}" = "" ]
    then
        echo "${command} : eee: loginforgeipsl must be defined"
        exit 1
    fi
fi
#
# define tempdir where the tar file will be put and compilation done
# set if local svn will be used for checkout cdf sources files
# it can not be so on IDRIS.
domain=$(nslookup $(hostname) | grep Name: | cut -f 2 | cut -f 2- -d '.')
case ${domain} in
    locean-ipsl.upmc.fr)
        tempdir=/usr/temp/${LOGNAME}/
        local_svncfd=1
    ;;
    idris.fr)
        tempdir=${WORKDIR}/
        local_svncfd=0
    ;;
   *)
       tempdir=/tmp/${LOGNAME}/
       local_svncfd=1
   ;;
esac
unset domain
#
project=superbib
#
# get files
if [ -d ${tempdir}/for_install_superbib/ ]
then
    # If ${tempdir}/for_install_superbib/ exists, we suppose it already
    # contains files.
    if [ ${svn_forge} -eq 1 ]
    then
        # We ask for update this working space
        echo "${command} : iii : ${tempdir}/for_install_superbib/ already exists" 1>> ${log} 2>&1
        echo "iii : You will be asked ${loginforgeipsl}@forge.ipsl.jussieu.fr passwd"
        svn update ${tempdir}/for_install_superbib/
        svn_status=${?}
        if [ ${svn_status} -ne 0 ]
        then
            echo "${command} : eee : pb with svn update forge" 1>> ${log} 2>&1
            echo "${command} : iii : log is ${log}"
            exit 1
        fi
    else
        echo "${command} : iii : no svn forge update is asked" 1>> ${log} 2>&1
    fi
else
    mkdir -p ${tempdir}/for_install_superbib/
    if [ ${svn_forge} -eq 1 ]
    then
        # If ${tempdir}/for_install_superbib/ does not exist, we checkout
        # files
        echo "iii : You will be asked ${loginforgeipsl}@forge.ipsl.jussieu.fr passwd"
        svn checkout svn+ssh://${loginforgeipsl}@forge.ipsl.jussieu.fr/ipsl/forge/projets/${project}/svn/trunk/ ${tempdir}/for_install_superbib/ 1>> ${log} 2>&1
        svn_status=${?}
        if [ ${svn_status} -ne 0 ]
        then
            echo "${command} : eee : pb with svn forge" 1>> ${log} 2>&1
            echo "${command} : iii : log is ${log}"
            exit 1
        fi
    else
        echo "${command} : iii : no svn forge checkout is asked" 1>> ${log} 2>&1
    fi
fi
#
# installation of files needed
mkdir -p ${directory}/
cp -rp ${tempdir}/for_install_superbib/* ${directory}/
#
echo "${command} : iii : everything you need is under"
echo "${command} : iii : ${directory}/"
echo "${command} : iii : log is ${log}"
#
# cleaning
unset log
#++
#
# end
exit 0
