#!/bin/sh
# -*- ksh -*-

# THIS SCRIPT IS ONLY FOR DEMONSTRATION PURPOSE.
# It WILL BE MORE SECURE IF YOU WRITE YOUR OWN.


# Install TOMUSS on production server
# Arguments:
#   * Storage directory
#   * Backup storage directory
#   * nocron
#   * nostart

if [ ! -f tomuss.py ]
then
	echo "This script must be launched from TOMUSS dir" >&2
	exit 1
fi

if [ "" != "$1" ]
then
  REAL="$1"
else
  REAL="/DATA"
fi

if [ "" != "$2" ]
then
  BACK="$2"
else
  BACK="/BACKUP"
  BACK="" # No backup
fi

shift 2

CRON='1'
START='1'
for I in $*
do
  case "$I" in
    nocron) CRON=0 ;;
    nostart) START=0 ;;
  esac
done

if [ ! -d $REAL -o \( "$BACK" != "" -a ! -d "$BACK" \) ]
then
  echo "Missing directories to store application and its backup" >&2
  echo $REAL
  echo $BACK
  exit 1
fi >&2

make translations

if [ "$(pwd)" = "$REAL/SERVEUR_NOTES" -o \( "$BACK" != "" -a "$(pwd)" = "$BACK/SERVEUR_NOTES" \) ]
then
  echo "Can't install version to itself" >&2
  exit 1
fi

# Old version
HIST=$(date "+TOMUSS.%Y-%m-%d_%H:%M:%S")

for I in $REAL $BACK
do
  if [ ! -d $I/TOMUSS_DB ]
  then
    mkdir $I/TOMUSS_DB
    touch $I/TOMUSS_DB/__init__.py
  fi
  if [ -d $I/SERVEUR_NOTES.new ]
  then
    echo 'Problem, Please, do some manual clean up' >&2
    exit 1
  fi
  # Copy sources
  cp -a . $I/SERVEUR_NOTES.new

  for J in TMP LOGS Trash PICTURES
  do
    # Copy informations from files for older TOMUSS versions
    if [ ! -d $I/$J ]
    then
      if [ -d $REAL/SERVEUR_NOTES/$J ]
         then
         cp -a $REAL/SERVEUR_NOTES/$J $I/$J
         fi
      if [ ! -d $I/$J ]
      then
	mkdir $I/$J
      fi
    fi
    # Create symbolic links.
    rm -rf $I/SERVEUR_NOTES.new/$J
    ln -s $I/$J $I/SERVEUR_NOTES.new/$J
  done

  # Test if history is free
  if [ -d $I/$HIST ]
  then
    echo 'History yet created ???' >&2
    exit 1
  fi

  # Create symbolic links
  if [ -h $I/SERVEUR_NOTES.new/DB ]
  then
    rm $I/SERVEUR_NOTES.new/DB
  fi
  ln -s $REAL/TOMUSS_DB $I/SERVEUR_NOTES.new/DB
  if [ "$BACK" != "" ]
  then
    if [ -h $I/SERVEUR_NOTES.new/BACKUP_DB ]
    then
      rm $I/SERVEUR_NOTES.new/BACKUP_DB
    fi
    ln -s $BACK/TOMUSS_DB $I/SERVEUR_NOTES.new/BACKUP_DB
  fi
  
  # Move current to history
  if [ -d $I/SERVEUR_NOTES ]
  then
    mv $I/SERVEUR_NOTES $I/$HIST
  fi

  # Move new to current
  mv $I/SERVEUR_NOTES.new $I/SERVEUR_NOTES
done

# Stop current
if [ -d $REAL/$HIST ]
then
  echo "Stop current TOMUSS"
  (cd $REAL/$HIST ; make stop)
fi

# Let some time to stop, just in case...
sleep 1

# Database conversion
for I in  $REAL $BACK
do
  if [ -d "$REAL/TOMUSS_DB/CHARTES" ]
  then
    SCRIPTS/convert_2_8_12_to_2_8_13 "I/TOMUSS_DB"
  fi
  SCRIPTS/convert_2_12_11_to_2_12_12 "$I/TOMUSS_DB"
done

# Do not start server
if [ "$START" != "1" ]
then
  exit 0
fi

# Start new
echo "Start new TOMUSS"
(cd $REAL/SERVEUR_NOTES ; make translations ; make)

###############################################################################
# CRON
###############################################################################

if [ "$CRON" = "1" ]
then
echo "Install crontab"

# Install crontab
echo "
# Crontab generated by the install script

# Every 5 minutes : (re)launch servers if stalled
*/5 * * * * (. $HOME/.profile ; cd $REAL/SERVEUR_NOTES ; SCRIPTS/crontab_run.py crontab) >$REAL/SERVEUR_NOTES/LOGS/xxx_crontab_5m 2>&1

# Every hours : Make a backup
30  * * * * (. $HOME/.profile ; cd $REAL/SERVEUR_NOTES ; rsync --archive --no-g --delete --exclude '*.pyc' DB/. thierry.excoffier@demo710.univ-lyon1.fr:/home/thierry.excoffier/BACKUP_1H ) >$REAL/SERVEUR_NOTES/LOGS/xxx_crontab_1h 2>&1

# Once per night
5 5 * * * (. $HOME/.profile ; cd $REAL/SERVEUR_NOTES ; SCRIPTS/night_job ; ) >$REAL/SERVEUR_NOTES/LOGS/xxx_crontab_night_job 2>&1

" | crontab -

fi

###############################################################################

exit 0
