#!/bin/sh
#
# This script should only run on the supercell backup server.
# It creates the default config to backup an supercell.
# A user is created for each Supercell. 
# If a user already exists, the Supercell's root user public key
# is allowed access to this users' directories.
#
# This script should be run twice.
#
# Perry 1-8-2007
#
#set -x

print_point()
{
   for (( i=0; i<2; i++ ))
   do
	sleep 1
        echo -en "."
   done
   echo ""
}

add_user()
{
   adduser $SERVERID -s /bin/bash > /dev/null 2>&1
   echo "added user " $SERVERID " to passwd"

   if [ $? -gt 0 ]
   then
      echo "User probably exists"
      echo -en "Exiting."
      print_point
   fi
}

display_comment()
{
   echo ""
   echo -en "\\033[1;31m" 
   echo "Run on the Supercell (backup client)"
   echo -en "\\033[0;39m"
   echo "/usr/local/supercell/sbin/supercell-backup-activate"
   echo -en "\\033[1;31m" 
   echo "Then on the backup server"
   echo -en "\\033[0;39m"	
   echo "/usr/local/supercell/sbin/new-backupsupercell"
}

SERVERID=`cat /etc/astium-serial`

if [ -z $SERVERID ]
then
	SERVERID=`/usr/local/astium/bin/get_db_val astglobals value name astserial`
fi

if [ -z $SERVERID ]
then
	echo "Please fill /etc/astium-serial with the nbs id for your system"
	exit 1
fi

if [ ! -d /home/$SERVERID ]
then
	add_user
fi

if [ ! -d /home/$SERVERID/.ssh ]
then
	mkdir /home/$SERVERID/.ssh
	chmod 700 /home/$SERVERID/.ssh
	chown $SERVERID:$SERVERID /home/$SERVERID/.ssh
	echo "Added .ssh dir to user home dir"
fi


if [ ! -f /root/.ssh/id_dsa_neonova_backup.pub ]
then
        ssh-keygen -t dsa -N '' -f /root/.ssh/id_dsa_neonova_backup
        chmod 600 /root/.ssh/id*
	echo "no key generated so far.. just generated one"
fi

cat /root/.ssh/id_dsa_neonova_backup.pub >> /home/$SERVERID/.ssh/authorized_keys
chmod 600 /home/$SERVERID/.ssh/authorized_keys
chown $SERVERID:$SERVERID /home/$SERVERID/.ssh/authorized_keys
echo "Copied pub key to user $SERVERID .ssh authorized_keys file"

exit 0
