#!/bin/sh
#
# This file is part of SALI
#
# SALI is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SALI is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SALI.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2010-2013 SURFsara

if [ -z "${1}" ]
then
    echo "Usage: $0 <imageserver hostname>"
    exit 1
fi

if [ -z "$(echo $1 | egrep '^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' -o)" ]
then
	DIG=$(dig +short $1 | tail -n1)
	if [ -z "${DIG}" ]
	then
		DOMAIN=$(cat /etc/resolv.conf | awk '/domain/ {print $2}')
        if [ -z "${DOMAIN}" ]
        then
		    DOMAIN=$(cat /etc/resolv.conf | awk '/search/ {print $2}')
        fi
		DIG=$(dig +short "${1}.${DOMAIN}" | tail -n1)
	fi

	if [ -z "${DIG}" ]
	then
		echo "Unable to find ip address for host '${1}'"
		exit 1
	fi
	
	SERVER_IP=$DIG
else
	SERVER_IP=$1
fi

echo "Using IP $SERVER_IP for host ${1}"

RSYNC_CFG=/tmp/sali_cfg.$$
RSYNC_LOG=/tmp/sali_log.$$
RSYNC_MAGIC_STRING="__cloning_completed__"
SYSTEMIMAGER_DIR_CREATED=0

## Saving current mounts
if [ ! -e /etc/systemimager ]
then
    mkdir -p /etc/systemimager
		SYSTEMIMAGER_DIR_CREATED=1
fi

mount > /etc/systemimager/mounted_filesystems
## End saving mounts

cat > "${RSYNC_CFG}" <<DELIM
#
# sali_prepare
#
#  This file: ${RSYNC_CFG}
#
list = yes
timeout = 900
dont compress = *.gz *.tgz *.zip *.Z *.ZIP *.bz2 *.deb *.rpm *.dbf
uid = root
gid = root
hosts allow = ${SERVER_IP}/32
log file = ${RSYNC_LOG}

[root]
    path = /
    exclude = ${RSYNC_CFG} ${RSYNC_LOG}

DELIM

echo "Created a config file in ${RSYNC_CFG}"
echo "   starting rsync daemon: rsync --daemon --config=${RSYNC_CFG}"
rsync --daemon --config="${RSYNC_CFG}"

RSYNC_PID=$(pidof rsync)

tail -f "${RSYNC_LOG}" | while read line
do
    if [ -n "$(echo $line | grep "${RSYNC_MAGIC_STRING}")" ]
    then
        echo "Server says done, killing rsync daemon"
        kill "${RSYNC_PID}"
        break
    fi
done

## Cleaning
rm -rf "${RSYNC_CFG}"
rm -rf "${RSYNC_LOG}"

## Only remove if we have created it!!
if [ $SYSTEMIMAGER_DIR_CREATED -eq 1 ]
then
	rm -rf /etc/systemimager
fi
