#!/bin/bash

#
# ScoDoc:  restore data (saved by save_scodoc_data) into current install
# 
#  Utile pour migrer ScoDoc d'un serveur a un autre
#  A executer en tant que root sur le nouveau serveur
#
# E. Viennet, Sept 2011, Nov 2013, Mar 2017, Aug 2020
#


INSTANCE_DIR=/opt/scodoc/
SCODOC_DIR="${INSTANCE_DIR}/Products/ScoDoc"
SCODOC_VAR_DIR="${INSTANCE_DIR}/var/scodoc"

source utils.sh
check_uid_root "$0"

# Safety check
echo "Ce script va remplacer les donnees de votre installation ScoDoc par celles"
echo "enregistrees dans le fichier fourni."
echo "Ce fichier doit avoir ete cree par le script save_scodoc_data.sh, sur une autre machine."
echo 
echo "Attention: TOUTES LES DONNEES DE CE SERVEUR SERONT REMPLACEES !"
echo "Notamment, tous les utilisateurs et departements existants seront effaces !"
echo
echo "TOUTES LES BASES POSTGRESQL SERONT EFFACEES !!!"
echo 
echo -n "Voulez vous poursuivre cette operation ? (y/n) [n]"
read -r ans
if [ ! "$(norm_ans "$ans")" = 'Y' ]
then
   echo "Annulation"
   exit 1
fi

# Usage
if [ ! $# -eq 1 ]
then
  echo "Usage: $0 directory_or_archive"
  exit 1
fi

SRC=$1

if [ "${SRC:0:1}" != "/" ]
then
  echo "Usage: $0 directory_or_archive"
  echo "Erreur: utiliser un chemin absolu (commencant par /)"
  exit 1
fi

# Source directory
if [ "${SRC##*.}" = 'tgz' ]
then
  echo "Opening tgz archive..."
  tmp=$(mktemp -d)
  chmod a+rx "$tmp"
  cd "$tmp" || terminate "directory error"
  tar xfz "$SRC" 
  SRC=$(ls -1d "$tmp"/*)
  IS_TMP=1
  # If source is a tgz, can use mv
  COPY="mv"
else
  IS_TMP=0
  # If source is a directory, does not modify its content
  COPY="cp -rp"
fi

echo "Source is $SRC"
echo "Stopping ScoDoc..."
scodocctl stop

# Erase all postgres databases and load data
chmod a+rx "$SRC"
chmod a+r "$SRC"/scodoc.dump.txt
PG_DUMPFILE="$SRC/scodoc.dump.txt"

su -c "$SCODOC_DIR/config/psql_restore_databases.sh $PG_DUMPFILE" postgres

# 
echo Copying data files...

rm -rf "${INSTANCE_DIR:?}/var"
$COPY "$SRC/var" "$INSTANCE_DIR"

if [ ! -e "${SCODOC_VAR_DIR:?}/config/" ]
then
  mkdir "${SCODOC_VAR_DIR:?}/config/"
  chown www-data.www-data "${SCODOC_VAR_DIR:?}/config/"
  chmod 775 "${SCODOC_VAR_DIR:?}/config/"
fi

rm -rf "${SCODOC_DIR:?}/config/depts" 
if [ -e "${SRC:?}/depts" ]
then
  # legacy depts => move them to var
  $COPY "$SRC/depts" "${SCODOC_VAR_DIR}/config/"
fi

rm -rf  "$SCODOC_DIR/static/photos" 
if [ -e "$SRC/photos" ]
then
  # legacy photos (in <src>/static/photos) => move them to var
  $COPY "$SRC/photos" "${SCODOC_VAR_DIR}/"
fi 

rm -rf "${SCODOC_DIR:?}/logos"
$COPY "$SRC/logos" "$SCODOC_DIR/"

mv "$SCODOC_DIR/config/scodoc_config.py"  "$SCODOC_DIR/config/scodoc_config.py.$(date +%Y%m%d-%H%M%S)" 
$COPY "$SRC/scodoc_config.py" "$SCODOC_DIR/config/"
# Verifie le codage de ce fichier:
if [ -z "$(file $SCODOC_DIR/config/scodoc_config.py | grep -i UTF-8)" ] 
then
   mv "$SCODOC_DIR/config/scodoc_config.py" "$SCODOC_DIR/config/scodoc_config.py.orig"
   iconv -f iso8859-15 -t utf-8 "$SCODOC_DIR/config/scodoc_config.py.orig" > "$SCODOC_DIR/config/scodoc_config.py"
fi

rm -rf "${INSTANCE_DIR:?}/log"
$COPY "$SRC/log" "$INSTANCE_DIR/"

# Fix file ownership and access rights
chown -R www-data.root "$INSTANCE_DIR/log" 
chown -R www-data.root "$INSTANCE_DIR/var" 
chmod 775 "$INSTANCE_DIR./log" "$INSTANCE_DIR./var" 
chown -R  www-data.root "$SCODOC_DIR"
chmod -R 775 "$SCODOC_DIR"

# Remove tmp directory
if [ "$IS_TMP" = "1" ]
then
  rm -rf "${tmp}"
fi

# Mise a jour BD ScoDoc
cd ${SCODOC_DIR:?}/config || terminate "no config directory"
./upgrade.sh

#
echo
scodocctl start