#!/bin/bash

. ${TOOLKIT}-functions.sh


setbackupname()
{
    local name=
    local myname=`uname -n`
    local myname_short=`uname -n | sed -e 's/\..*//'`
    local class=
    local model=

    jtconf backup.$myname.dirs >/dev/null 2>&1 && name="backup.$myname"

    [ "$name" = "" ] && {
	jtconf backup.$myname_short.dirs >/dev/null 2>&1 && name="backup.$myname_short"
    }

    [ "$name" = "" ] && {
	for class in $CLASS
	do
	  case $class in
	    server*|*-core)
		jtconf backup-template.server.dirs >/dev/null 2>&1 && name="server"
		;;
	    laptop)
		jtconf backup-template.laptop.dirs >/dev/null 2>&1 && name="laptop"
		;;
	  esac
	  [ "$name" != "" ] && {
		name="backup-template.$name"
		break
	  }
	done
    }

    [ "$name" = "" ] && {
	jtconf backup-template.$KUSA_MODEL.dirs >/dev/null 2>&1 && name="backup-template.$KUSA_MODEL"
    }

    [ "$name" = "" ] && {
	name="backup"
    }

    echo "  using model $name" >&2
    echo "$name"
}

# (MAIN)

label=$(jtconf backup-server.label 2>/dev/null) || :
label=${label:-"rbackup"}

file="kubackup-rbackup.conf"
dest="/etc/kubackup-$label.conf"

backupname=$(setbackupname)


desc=$(		jtconf $backupname.desc 2>/dev/null)	|| :
dirs=$(		jtconf $backupname.dirs)		|| exit_missing_define $backupname.dirs
excludes=$(	jtconf $backupname.excludes)		|| exit_missing_define $backupname.excludes
saveperms=$(	jtconf $backupname.saveperms 2>/dev/null) || :

echo "# $dest - kubackup client on ::network.hostname::.::network.domain::
#" >$file.tmp

cat $file >> $file.tmp

echo "
# model: $desc
#
bck_common_excludes='$excludes'
bck_dirs='$dirs'
bck_saveperms='$saveperms'
" >> $file.tmp


# remove std file if not needed
#
[ "$label" != "rbackup" ] && rm -f /etc/kubackup-rbackup.conf

echo "  using label $label" >&2
installfile $file.tmp $dest root:root 644 || exit $?


$SOMETHING_CHANGED && {
	restart_service xinetd xinetd	|| exit $?
}
exit 0
