#!/bin/sh
#########################################################################################
##
##  configtool
##
##  Commands:
##
##      backup    <modName>
##      restore   <modName>
##      getconfig <modName> <filename>
##      putconfig <modName> <filename>
##
##  2007-01-16  Andreas Vogel (omega)
##
#########################################################################################

PreBackup () {
    if [ -x $modDir/system/configprepare ] ; then
        $modDir/system/configprepare backup $modName
        ret=$?
    else
        ret=0
    fi

    return $ret
}

PostRestore () {
    if [ -x $modDir/system/configprepare ] ; then
        $modDir/system/configprepare restore $modName
        ret=$?
    else
        ret=0
    fi

    return $ret
}

#########################################################################################
##
##  Main
##
#########################################################################################

cmd=$1
modName=$2
filename=$3
warn=${4:-warn}

##
##  Use standard backup file if command is "backup" or "restore"
##
if [ "$cmd" == backup -o "$cmd" == restore ] ; then
    filename="/raid/data/module/backup/$modName/backup-$modName.tar.gz"

    if [ "$cmd" == backup ] ; then
        cmd=getconfig
    elif [ "$cmd" == restore ] ; then
        cmd=putconfig
        warn=nowarn
    fi
fi

if [ -z "$filename" -o -z "$modName" ] ; then
    echo "ERROR: illegal commandline: $0 $*"
    exit 1
fi
    
MAGIC="CONFIG-$modName"
modDir=/raid/data/module/$modName
modVersion=$(awk '{print $2}' < "$modDir/VERSION")
        
case "$cmd" in
    getconfig|get)
        dstFile=$filename
        
        if ! PreBackup ; then
            echo "ERROR: PreBackup script failed!"
            exit 1
        fi

        echo "$MAGIC $modVersion $(date '+%Y-%m-%d_%H:%M:%S')" > "$modDir/system/etc/MAGIC"
        tar czpf "$dstFile" -C "$modDir/system" etc
        status=$?
        rm "$modDir/system/etc/MAGIC"
        exit $status
        ;;

    putconfig|put)
        srcFile=$filename
        tmpdir=/tmp/tmp.configtool
        
        if [ ! -r "$srcFile" ] ; then
            if [ "$warn" != nowarn ] ; then
                echo "ERROR: source file '$sourceFile' not readable!"
                exit 1
            else
                exit 0
            fi
        fi
            
        rm -rf "$tmpdir"
        mkdir -p "$tmpdir"
        if ! tar xzf "$srcFile" -C "$tmpdir" ; then
            echo "ERROR: cannot unpack tar file!"
            rm -rf "$tmpdir"
            exit 1
        fi
            
        cfgMagic=$(awk '{print $1;exit}' < "$tmpdir/etc/MAGIC")
        cfgVersion=$(awk '{print $2;exit}' < "$tmpdir/etc/MAGIC")
        cfgDate=$(awk '{print $3;exit}' < "$tmpdir/etc/MAGIC")
            
        if [ "$cfgMagic" != "$MAGIC" ] ; then
            echo "ERROR: wrong magic '$cfgMagic' - expected '$MAGIC'!"
            rm -rf "$tmpdir"
            exit 1
        fi
        rm "$tmpdir/etc/MAGIC"

        if ! tar cpf - -C "$tmpdir" etc | (cd "$modDir/system" ; tar xpf -) ; then
            echo "ERROR: cannot copy from '$tmpdir' to '$modDir/system'!"
            rm -rf "$tmpdir"
            exit 1
        fi

        rm -rf "$tmpdir"

        if ! PostRestore ; then
            echo "ERROR: PostRestore script failed!"
            exit 1
        fi
        ;;

    *)
        echo "ERROR: invalid command '$cmd'!"
        echo "Usage: $0 {backup|restore|getconfig|putconfig} <modname> <filename>"
        exit 1
        ;;
esac

exit 0
