#!/bin/bash
# ^^^^^^^^^ if your installation of `bash' is elsewhere 
#           enter its location here!
#
# Startscript fuer den Hattrickorganizer v0.21 (2005-05-19)


# Settings


# Enter HO!'s directory. Default is the current directory
#
#HODIR=.

HODIR=.

# Enter the default backup-directory. It will be created
# in $HODIR/$DB/ if it doesn't exist. Default is `backup'
#
#BACKUPDIR=backup 

BACKUPDIR=backup

# Enter the maximum amount of backups you do want to store.
# Default is 5
#
#MAX_BACKUPS=5

MAX_BACKUPS=5

# Where can I find java?
# Default is just looking at $PATH
# (remember option -j)
#JAVA=`which java`

JAVA=`which java`

#
# DO NOT EDIT ANY MORE UNTIL YOU KNOW WHAT YOU'RE DOING!
#
# required java version

JAVAVERREQ=1.4.1

# database-directory:

DATABASEDIR=db

# Store the name of this script. 

HONAME=$0

# Which database files and name of backup file?

BACKUPLIST="database.data database.script database.backup"
PREFIX=database

# We need a date for the backup-file

DATE=`date "+%Y-%m-%d"`

# Check for javaversion by default?

CHECK=true

# Perform backup by default?

BACKUP=true

# Restore by default? 
# IMPORTANT! This is just for initialisation!
# If you set this to true strange things will occure!

RESTORE=false

# Which version of HO! is this?

HOVERSION=`cat $HODIR/version.txt`

# Output the help

function help(){
echo "Usage: $HONAME [option]"
echo -e "Options:\n"
echo "        -h  --help        This help text."
echo "        -v  --version     Show HO! version."
echo "        -f  --force       Start without checking java-version"
echo "        -nb --nobackup    Start without backup"
echo "        -j  --java <path> Use this java."
echo "        -r  --restore     Restore the last backup-file."
echo "        -rd --restoredate <date>"
echo "                          Restore the backup-file from <date>"
echo "        -bd --backupdir   Use this as the backup directory."
exit 0
}

# Start HO!

function start(){
  $JAVA -jar hocoded.jar
}

# Backup 

function backup(){
  cd ./$DATABASEDIR
  if [ ! -d $BACKUPDIR ] ; then
    echo "Creating $DATABASEDIR/$BACKUPDIR" 
    mkdir -p $BACKUPDIR
  fi
  cd $BACKUPDIR
  while [ `ls -r | wc -l` -gt $MAX_BACKUPS ] ; do
     rm -f `ls -r | tail -n 1`
  done
  cd ..
# THE BIG TRICK: ls gives false if one of the files is missing!!! ;-)  
  if ( ls $BACKUPLIST > /dev/null ) ; then
# f - is needed in case someone has set his $TAPE variable...
      tar -cf - $BACKUPLIST | gzip > $BACKUPDIR/$PREFIX-$DATE.tgz
# ...and this is shorter, but won't work if there's no GNU tar! :-(
#     tar -czf $BACKUPDIR/$PREFIX-$DATE.tgz $BACKUPLIST  
  else
    echo "Database files not found, so there is nothing to backup."
  fi
  cd ..
}

# Restore

function restore(){
  cd $DATABASEDIR
  if [ -z $RESTOREDATE ] ; then
    if [ `ls $BACKUPDIR/$PREFIX*.tgz | wc -l` -lt 1 ] ; then
      echo "No backup-file found."
      exit 1
    fi
    gunzip -c `ls $BACKUPDIR/$PREFIX*.tgz | tail -n 1` | tar -xf -
  else
    if [ -r "$BACKUPDIR/$PREFIX-$RESTOREDATE.tgz" ] ; then
      gunzip -c "$BACKUPDIR/$PREFIX-$RESTOREDATE.tgz" | tar -xf - 
      echo "Restored database from $BACKUPDIR/$PREFIX-$RESTOREDATE.tgz"
    else
      echo "Error reading backup-file $BACKUPDIR/$PREFIX-$RESTOREDATE.tgz!"
      cd ..
      exit 1
    fi
  fi
  cd ..
}

# Check java -version

function checkjava(){
  test -x "$JAVA" || { echo "Can't find java!"
                       exit 1 ; }
  JAVAVER=`$JAVA -version 2>&1 | head -n 1 | \
           awk -F\" '{print $2}' | sed s/[^0-9\.].*//g`
  JAVAMAJ=`echo $JAVAVER | awk -F. '{print $1}'`
  JAVAMIN=`echo $JAVAVER | awk -F. '{print $2}'`
  JAVAMINMIN=`echo $JAVAVER | awk -F. '{print $3}'`
  JAVAMAJREQ=`echo $JAVAVERREQ | awk -F. '{print $1}'`
  JAVAMINREQ=`echo $JAVAVERREQ | awk -F. '{print $2}'`
  JAVAMINMINREQ=`echo $JAVAVERREQ | awk -F. '{print $3}'`
  if ( [ $JAVAMAJ -lt $JAVAMAJREQ ] || \
     ( [ $JAVAMAJ -eq $JAVAMAJREQ ] && [ $JAVAMIN -lt $JAVAMINREQ ] ) || \
     ( [ $JAVAMAJ -eq $JAVAMAJREQ ] && [ $JAVAMIN -eq $JAVAMINREQ ] && [ $JAVAMINMIN -lt $JAVAMINMINREQ ] ) ) ; then
     echo "Java is too old!  Maybe you should try: $HONAME -j"
     exit 1
  fi
}

# MAIN

# Run throuh the params

until [ -z "$1" ]
do
  case "$1" in

   -j|--java)   
               JAVA=$2;
	       if [ -z $JAVA ] ; then
		 echo "Option j or java needs a parameter!"
		 exit 1
	       fi
	       shift;;
   -v|--version)
               echo "This is HO! version $HOVERSION";
               exit 0;;
   -nb|--nobackup)
               BACKUP=false;;
   -f|--force)
               CHECK=false;;
   -h|--help)
               help;;
   -bd|--backupdir)
               BACKUPDIR=$2;
	       shift;;
   -r|--restore)
               RESTORE=true;
               BACKUP=false;;
   -rd|--restoredate)
               RESTORE=true;
               RESTOREDATE=$2;
	       if [ -z $RESTOREDATE ] ; then
		 echo "Option rd or restoredate needs a date as parameter!" 
		 echo "Available backups:"
                 ls -1 $DATABASEDIR/$BACKUPDIR/$PREFIX-*.tgz	 
		 exit 1
	       fi
               BACKUP=false;
	       shift;;
   *)
               echo "Unknown parameter $1";
	       echo "Try $HONAME --help to get help."
	       exit 1;;
  esac
  shift
done

# cd to HO!'s home

cd $HODIR

# Check for java -version (if called without `force')

`$CHECK`   && checkjava

# Perform backups or restore only if $DATABASEDIR exists

if [ -d $DATABASEDIR ] ; then
  `$BACKUP`  && backup
  `$RESTORE` && restore
else
  echo "No directory $DATABASEDIR found. This is your first HO!-Session, isn't it? Have fun!"
fi

# Start HO!

start

# Exit this script

exit 0 
