#!/bin/bash
#
# Create root user public key, and copy to backup server.

# Get all postgresql databases, backuppasswd, backupserver and dbversion.
. /usr/local/astium/bin/astium-backup-lib.sh
declare_info
get_database_info

# Test if the astium serial file exists.
if [ ! -f /etc/astium-serial ]
then
        echo "Fatal Error : /etc/astium-serial does not exists."
        exit 1
fi

# Test if the astium serial contains 5 digits.
test=`cat /etc/astium-serial|grep -v ^#|grep -v ^$|grep "[0-9]\{5\}"`
if [ -z $test ]
then
        echo "Fatal Error : Astium serial number is a invalid astium serial. "
        exit 1
fi

if [ -z $BACKUPPASSWD ]
then
        echo "Fatal Error : The astium does not have a valid backup password, see astglobals."
        exit 1
fi

SERIAL=`cat /etc/astium-serial`
echo $SERIAL

if [ ! -f /root/.ssh/id_dsa_astium_backup ]
then
	ssh-keygen -t dsa -N '' -f /root/.ssh/id_dsa_astium_backup
	chmod 600 /root/.ssh/id*
        scp  -P $BACKUPSERVERPORT /root/.ssh/id_dsa_astium_backup.pub backup@$BACKUPSERVER:/home/backup/$SERIAL-authorized_keys2
	chmod 600 /root/.ssh/id_*
fi
echo ""
echo "Astium Serial: $test"
