#!/bin/bash
die()
{
  echo "FATAL ERROR: $* (status $?)" 1>&2
  exit 1
}

usage()
{
cat<<EOF
usage: $0 options

The script installs a Salome virtual application in a specified directory.
A desktop shortcut and a menu entry are added.

Options:
 -h    Show this message
 -a    Application directory (default is $HOME/salome/appli_V6_6_0)
 -d    Do not install desktop file
 -m    Do not install menu entry
 -D    Equivalent to -dm. Do not install desktop file and menu entry
 -l    Choose language (English or French, default is English)
 -p    Do not install default preference file
 -q    Quiet installation
 -v    Verbose mode
EOF
}

check_path()
{
  if test ! -z $1 ; then
    arr=$(echo $1 | tr ":" "
")
    for x in $arr 
    do
      if test ! -d $x ; then
       echo "Missing repertory: $x" 
      fi
    done
  fi
}

APPLIDIR=""
DESKTOP="y"
MENU="y"
LANGUAGE=""
PREFS="y"
QUIET=""
VERBOSE=""
while getopts ":ha:dmDl:pqv" OPTION
do
  case $OPTION in
    h)
      usage
      exit 1
      ;;
    a)
      APPLIDIR=$OPTARG
      ;;
    d)
      DESKTOP=""
      ;;
    m)
      MENU=""
      ;;
    D)
      DESKTOP=""
      MENU=""
      ;;
    l)
      LANGUAGE=$OPTARG
      ;;
    p)
      PREFS=""
      ;;
    q)
      QUIET="y"
      ;;
    v)
      VERBOSE="y"
      ;;
    \?)
      usage
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

if test -z "$SALOMEDIR"; then
  SALOMEDIR=`python2 -c "import os; print os.path.normpath(os.path.dirname(os.path.abspath(os.path.realpath('$0'))))" 2>/dev/null`
fi

# Change this directory as wanted
DEFAULTAPPLIDIR=$HOME/salome/appli/V6_6_0

test -z "$APPLIDIR" && echo -n "Enter Salome virtual application directory [default=$DEFAULTAPPLIDIR] : " && read -e -p "" APPLIDIR
test -z "$APPLIDIR" && APPLIDIR=$DEFAULTAPPLIDIR
if test "$QUIET" = ""; then
  echo "Creating SALOME application in $APPLIDIR ..."
fi
mkdir -p $APPLIDIR || die "mkdir failed"

cd $APPLIDIR
sed "s#\${ROOT_SALOME}#${SALOMEDIR}#g" ${SALOMEDIR}/salome_prerequisites.sh > ${SALOMEDIR}/salome_prerequisites_appli.sh
sed "s#\${ROOT_SALOME}#${SALOMEDIR}#g" ${SALOMEDIR}/.config_appli_template.xml > config_appli.xml

KERNELDIR=`cat config_appli.xml |grep KERNEL|awk -v FS=\" '{print $4}'`
if test "${VERBOSE}" = "y"; then
  python2 ${KERNELDIR}/bin/salome/appli_gen.py
else
  python2 ${KERNELDIR}/bin/salome/appli_gen.py > appli_V6_6_0.log 2>&1
fi

cd $SALOMEDIR

# Install the menu and desktop files if requested
if test -e ${SALOMEDIR}/.salome_V6_6_0.desktop; then
  sed "s#SALOMEDIR#${SALOMEDIR}#g" ${SALOMEDIR}/.salome_V6_6_0.desktop > ${SALOMEDIR}/salome_V6_6_0.desktop
  sed -i "s#APPLIDIR#${APPLIDIR}#g" ${SALOMEDIR}/salome_V6_6_0.desktop
  sed -i "s#HOME#${HOME}#g" ${SALOMEDIR}/salome_V6_6_0.desktop
fi

if test -n "$MENU"; then
  if test -e ${SALOMEDIR}/salome_V6_6_0.desktop; then
    if test ! -d $HOME/.local/share/applications; then
      mkdir -p $HOME/.local/share/applications  || die "mkdir failed"
    fi
    desktop-file-install --dir=$HOME/.local/share/applications --vendor=salome --copy-name-to-generic-name                          --add-category=Education --add-category=Science --add-mime-type=application/x-hdf                           ${SALOMEDIR}/salome_V6_6_0.desktop > desktop-file-install-menu.log 2>&1
    chmod +x $HOME/.local/share/applications/salome_V6_6_0.desktop
  fi
fi

if test -n "$DESKTOP"; then
  DESKTOPDIR="$HOME/Desktop"
  if test ! -e "$DESKTOPDIR"; then
    DESKTOPDIR="$HOME/Bureau"
    if test ! -e "$DESKTOPDIR"; then
      echo "Desktop directory not found."
      DESKTOPDIR=""
    fi
  fi
  if test -n "${DESKTOPDIR}"; then
    if test -e ${SALOMEDIR}/salome_V6_6_0.desktop; then
    desktop-file-install --dir=${DESKTOPDIR} --vendor=salome --copy-name-to-generic-name                          --add-category=Education --add-category=Science --add-mime-type=application/x-hdf                           ${SALOMEDIR}/salome_V6_6_0.desktop > desktop-file-install-desktop.log 2>&1
    chmod +x ${DESKTOPDIR}/salome_V6_6_0.desktop
    fi
  fi
fi

# Install the preferences file if requested
if test -n "$PREFS"; then
  if test -e ${SALOMEDIR}/.config/SalomeApprc.6.6.0.default; then
    if test ! -d $HOME/.config/salome; then
      mkdir -p $HOME/.config/salome  || die "mkdir failed"
    else
      if test -e $HOME/.config/salome/SalomeApprc.6.6.0; then
        cp $HOME/.config/salome/SalomeApprc.6.6.0 $HOME/.config/salome/SalomeApprc.6.6.0.backup
      fi
      if test -e $HOME/.config/salome/SalomeMaterialrc; then
        cp $HOME/.config/salome/SalomeMaterialrc $HOME/.config/salome/SalomeMaterialrc.backup
      fi
    fi
    if test "$QUIET" = ""; then
      echo "Installing the preferences file in $HOME/.config/salome/SalomeApprc.6.6.0"
    fi
    cp ${SALOMEDIR}/.config/SalomeApprc.6.6.0.default $HOME/.config/salome
    cp ${SALOMEDIR}/.config/SalomeApprc.6.6.0.default $HOME/.config/salome/SalomeApprc.6.6.0
    echo "Installing the custom materials file in $HOME/.config/salome/SalomeApprc.6.6.0"
    cp ${SALOMEDIR}/.config/SalomeMaterialrc.default $HOME/.config/salome
    cp ${SALOMEDIR}/.config/SalomeMaterialrc.default $HOME/.config/salome/SalomeMaterialrc
  fi
fi

# Install CatalogResources file
if test "$QUIET" = ""; then
  echo "Installing the CatalogResources file in ${APPLIDIR}/CatalogResources.xml"
fi
cp ${SALOMEDIR}/.config/CatalogResources.xml.default ${APPLIDIR}
cp ${SALOMEDIR}/.config/CatalogResources.xml.default ${APPLIDIR}/CatalogResources.xml

# Change language if requested anf if the preferences file is installed
if test -n "$LANGUAGE" && test -n "$PREFS"; then
  if test -e $HOME/.config/salome/SalomeApprc.6.6.0; then
    if test "$QUIET" = ""; then
      echo "Changing the language to $LANGUAGE in the preferences file"
    fi
    sed -i -e 's#value="en"#value="'${LANGUAGE}'"#g' $HOME/.config/salome/SalomeApprc.6.6.0
  fi
fi

. salome_prerequisites.sh
. salome_modules.sh

# Put here additional commands after creation of the appli
# Replace the keyword: #APPLI_CUSTOM_COMMANDS

python2.6 ${SALOMEMECA_EXTRA_ROOT_DIR}/custom/postinstall_appli.py ${APPLIDIR}

# End of additional commands

# check paths
if test "$QUIET" = ""; then
  check_path ${LD_LIBRARY_PATH}
  check_path ${PATH}
  check_path ${PYTHONPATH}
else
  check_path ${LD_LIBRARY_PATH} 1>/dev/null 2>&1
  check_path ${PATH} 1>/dev/null 2>&1
  check_path ${PYTHONPATH} 1>/dev/null 2>&1
fi

# apply patches
if test -d .patches; then
  ./.patches/patch.sh
  # remove patch directory
  rm -rf .patches
fi

# apply post install script
python2.6 ./salome_post_install.py

if test "$QUIET" = ""; then
  echo "The SALOME application was successfully created."
  echo "To run the application:"
  echo "\$ ${APPLIDIR}/runAppli"
fi

#./runAppli $*