#!/bin/bash

compil_mode="prod"
svnbranch="/trunk"
doc="F"
extdir="."

interactive=false

has_interactive=false
has_rev=false
has_user=false
has_branch=false
has_compil_mode=false
has_arch=false
has_doc=false
has_extdir=false


echo -e "Installing XIOS library... "

# Traitement de la ligne de commande
while (($# > 0))
   do
      case $1 in
         "-h")
            # A modifier
            exit;;
         "--interactive") interactive=true    ; has_interactive=true; shift ;;
         "--rev")         svnrev=$2           ; has_rev="true"; shift; shift ;;
         "--user")        svnuser=$2          ; has_user="true"; shift; shift ;;
         "--branch")      svnbranch=$2        ; has_branch="true"; shift; shift ;;
         "--prod")        compil_mode="prod"  ; has_compil_mode="true"; shift ;;
         "--dev")         compil_mode="dev"   ; has_compil_mode="true"; shift ;;
         "--debug")       compil_mode="debug" ; has_compil_mode="true"; shift ;;
         "--arch")        arch=$2             ; has_arch="true"; shift; shift ;;
         "--doc")         withdoc="Y"         ; has_doc="true"; shift ;;
         *)               extdir="$1"           ; has_extdir=true; shift ;;
      esac
   done

# Récupération du répertoire local d'installation
if  [[  $interactive == "true" && $has_extdir == "false"  ]]
   then
      read -p "insert directory path to extract XIOS library : "$PWD"/" extdir
fi

install_dir=$PWD/$extdir
mkdir --parents $install_dir

# Documentation de la distribution
if  [[ $interactive == "true" && $has_doc == "false" ]]
   then
      read -p "Do you want to extract documentation ? [Y/N]: " withdoc
fi

# Architecture de compilation
if  [[ $interactive == "true" && $has_arch == "false" ]]
   then
      echo -e "\r\n XIOS may be install on different computing architecture, choose witch one...\r\n "
      echo -e "\tIA64_PLATINE        => Configuration for CCRT supercomputer Platine\r\n"
      echo -e "\tX86_LOCGNU_FEDORA   => Local configuration for gnu compiler under fedora\r\n"
      echo -e "\tX86_LOCINTEL_FEDORA => Local configuration for intel compiler under fedora \r\n"
      echo -e "\tX64_TITANE          => Configuration for CCRT supercomputer Titane \r\n"
      
      read -p "On which architecture do you want to work ? (default : none): " arch
fi

case $arch in
   IA64_PLATINE)        
      echo "=> Configuration for CCRT supercomputer Platine" ;;
   X86_LOCGNU_FEDORA)   
      echo "=> Local configuration for gnu compiler under fedora" ;;
   X86_LOCINTEL_FEDORA) 
      echo "=> Local configuration for intel compiler under fedora" ;;
   X64_TITANE) 
      echo "=> Configuration for CCRT supercomputer Titane " ;;
   *)                   
      echo "=> no specific architecture choosen"
      arch=
esac

# Récupération du login de connexion svn
if  [[ $interactive == "true" && $has_user  == "false" ]]
   then
      echo -e -n "insert your svn login"
      read -p " forge.ipsl.jussieu.fr (nothing if anonymous) : " svnuser
fi

# Récupération de la branche svn de travail
if  [[ $interactive == "true" && $has_branch == "false" ]]
   then
      read -p "Which branch do tou want to extract ? (default : /trunk): " svnbranch
fi

if [[ -z $svnbranch ]]
   then
      svnbranch="/trunk"
fi

if [[ -z $svnuser ]]
   then
      connect="http://forge.ipsl.jussieu.fr/ioserver/svn/XIOS"$svnbranch
   else
      connect="svn+ssh://"$svnuser"@forge.ipsl.jussieu.fr/ipsl/forge/projets/ioserver/svn/XIOS"$svnbranch
fi

# Extraction de la distribution
echo -e "=> Connecting to svn server via \""$connect"\"..."


if [[ $withdoc == [Yy]* ]]
   then
      installopt=$installopt" --doc"
fi

svn -q co $connect  $install_dir

echo -e "- uncompress archives ..."
cd $install_dir
for tarname in $install_dir/tools/archive/*.tar.gz; do
   tar -xzf "$tarname"
done

if [[ !(-z $arch) && -f $install_dir/make_xios ]]
   then
      echo -e "Running installation script for architecture \""$arch"\" ..."
      installopt=$installopt" --"$compil_mode" --arch "$arch
      cd $install_dir && ./make_xios $installopt
fi


