#!/bin/sh
#
# This script copies data from old to new installation
#

# 1 if license is in its own license.xml file otherwise 0 if in server.xml
haslicensefile=1;

error() { # $1: error-message
        local msg=${1:-"Need a message"}
        echo "Error: $msg"
        exit 1;
}

help()
{
       /bin/echo "Usage: $0 [-h] -t {mysql|postgresql|sqlite} [-o {path}] -n {path}";
       /bin/echo " -h Print this text";
       /bin/echo " -l Move license in server.xml to a license.xml file";
       /bin/echo " -t Database type (mysql, postgresql or sqlite)";
       /bin/echo " -o Path to old M/Monit installation";
       /bin/echo " -n Path to new M/Monit installation";
}

movelicense() {
# Assert that awk is installed and in the PATH
awk 'BEGIN { exit }' 2>/dev/null || error "The awk program is needed by the upgrade script. Please install awk\nto continue\n"
# Extract embedded <License> from server.xml and print to license.xml
awk '
BEGIN {flag = 1}
/<License owner[^>]*>/ {
    flag = 0
    print $0
    next
}
/<\/License>/ {
    flag = 1
    print $0
    next
}
{
    if (flag == 0) {
        print $0
    }
}
' $MMONIT_NEW/conf/server.xml > $MMONIT_NEW/conf/license.xml
# Replace embedded <License> with path to license.xml
awk '
BEGIN {flag = 0}
/<License owner[^>]*>/ {
    flag = 1
    print "    <License file=\"license.xml\" />"
    next
}
/<\/License>/ {
    flag = 0
    next
}
{
    if (flag == 0) {
        print $0
    }
}
' $MMONIT_NEW/conf/server.xml > $MMONIT_NEW/conf/server.tmp; mv $MMONIT_NEW/conf/server.tmp $MMONIT_NEW/conf/server.xml
}


# arguments
while getopts "hlt:o:n:" c
do
case $c in
        t) case $OPTARG in
                mysql)
                        DBTYPE=$OPTARG;
                        ;;
                postgresql)
                        DBTYPE=$OPTARG;
                        ;;
                sqlite)
                        DBTYPE=$OPTARG;
                        ;;
                *) 
                        error "Unknow database type: $OPTARG";
                        ;;
           esac;;
        o) MMONIT_OLD=$OPTARG;;
        n) MMONIT_NEW=$OPTARG;;
        l) haslicensefile=0;;
        *) help; exit 0;;
esac
done

if test x$DBTYPE = "x"
then
        error "-t option required"
fi

if test x$MMONIT_OLD = "x"
then
        error "-o option required"
fi

if test x$MMONIT_NEW = "x"
then
        error "-n option required"
fi


# Database tasks
case $DBTYPE in
        mysql)
        ;;
        postgresql)
        ;;
        sqlite)
                /bin/echo -n "Copying database file from $MMONIT_OLD/db/mmonit.db to $MMONIT_NEW/db/ ... ";
                cp $MMONIT_OLD/db/mmonit.db $MMONIT_NEW/db/ || error "Could not copy mmonit.db to $MMONIT_NEW/db/";
                /bin/echo "done";
                ;;
esac


# Copy tasks
if test $MMONIT_OLD != $MMONIT_NEW
then
        /bin/echo -n "Copying configuration files from $MMONIT_OLD/conf/ to $MMONIT_NEW/conf/ ... "
        cp -rp $MMONIT_OLD/conf/* $MMONIT_NEW/conf/ || error "Could not copy configuration"
        /bin/echo "done"

        if [ -d $MMONIT_OLD/docroot/uploads ]
        then
                /bin/echo -n "Copying uploaded files from $MMONIT_OLD/docroot/uploads/ to $MMONIT_NEW/docroot/uploads/ ... "
                cp $MMONIT_OLD/docroot/uploads/* $MMONIT_NEW/docroot/uploads/ 2>&1 | grep -v "No such file or directory"
                /bin/echo "done"
        fi
fi

# License seperation task
if [ $haslicensefile -eq 0 ]; then
    printf "Moving license key embedded in $MMONIT_NEW/conf/server.xml to $MMONIT_NEW/conf/license.xml\n"
    movelicense;
fi

exit 0

