#!/bin/ksh

#
# medsrvclone	-	Pack ImageMedical medsrv user for clone
#			create medsrv.tar.gz in current directory. It
#                       must be extracted in $HOME on the new server
# environment:          HOME variable
#

. $HOME/etc/setenv.sh
. $HOME/etc/func.shi

#
# --- Init settings
#
BASENAME=basename
TAR=tar
GREP=grep
GZIP=gzip

Self=`$BASENAME $0`
LoggerTag=$Self[$$]
export Self LoggerTag


CDIR="$PWD"
TARFILE=$CDIR/medsrv.tar
cd $HOME

#
# Enter Version
#
echo
echo "--- Please enter the Main Version of the package just cloned."
echo "--- Previous versions:"
ls -xrt $DATA/clone.ver 2>/dev/null
echo
read Version
echo
echo "--- Creating medsrv.tar.gz package '$Version'"

echo from `hostname`, Version $Version >$ETC/component.ls
ls -l $HOME/component | tail -n +2 >>$ETC/component.ls
mkdir $DATA/clone.ver 2>/dev/null
cp -p $ETC/component.ls $DATA/clone.ver/$Version
rm $TARFILE $TARFILE.gz 2>/dev/null

#
# --- Create clone tape archive
#
# --- Do not use $TMP here !
[ "" = "$Err" ] && $TAR -cf $TARFILE --exclude=etc/ssh_random_seed `find etc -mindepth 1 -type f -o -type d` `find htdocs -mindepth 1 -type l`
[ 0 != $? ] && Err="$Err tar0"

# --- With component/main, include the default collection if exists,
#     or include the current main/data. But on the name for cloning (main)

cd $HOME

for i in `ls $HOME/component | grep -v 'component\|taskd'` ; do
  [ "" = "$Err" ] && $TAR -rf $TARFILE component/$i/*
  [ 0 != $? ] && Err="$Err tar component/$i"
done

# add taskd separately because all the files are linked via symlinks
[ "" = "$Err" ] && $TAR -rhf $TARFILE component/taskd/
[ 0 != $? ] && Err="$Err tar component/taskd"


[ "" = "$Err" ] && $GZIP $TARFILE
[ 0 != $? ] && Err="$Err gzip"

cd $CDIR

if [ "" = "$Err" ]; then
  Log_ info "clone file '$TARFILE' created"
else
  Log_ error "Error when creating '$TARFILE': '$Err'"
fi
