#!/bin/sh
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   copy IWS6 directories from one host to another
# author
#   idc@planetlarg.net 30 sep 02
# description
#   Transfer the server directories and files from this host to the named
#   host. This script uses tar and ssh.
#   Play safe. Don't risk overwriting or deleting anything that already exists.
#
#   Run this as root or you will not have permission to create webserver dirs.
#
#   If you are not using key pairs with no passphrases to log in, this
#   is a pain in the arse to run. You will type in your password a dozen times.
#
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------

# command line options
   usage_message="
   usage:
      $0 <OpCo> <remote host>
   example:
      $0 isp01 ics01
   description
      Transfer the server directories and files from this host to another host.
      Run this as root or you will not have permission to create webserver dirs.
      This script uses tar and ssh.
   "
   # wrong options provided? tell the punter how to use this utility
   if [ $# -ne 2 ]; then
      echo "This script takes 2 options."
      echo "$usage_message" 1>&2
      exit 1
   fi
#---------#---------#---------#---------#---------#---------#---------#---------

# setup
    # vars
    op_co=$1
    host=$2

    dns=template.$op_co.co.uk

# main

#---------#---------#---------#---------#---------#---------#---------#---------
# function:
#    transfer_tree
# description:
#    dir copy from one host to another.
#    It cleans up after itself.
#
#    Uncomment the checks if you want safety ie.
#    don't put a tar file in a totally new location, don't touch a dir
#    that already exists
#
transfer_tree() {

    tar_dir=/tmp
    tarball=$dir.tar
    # check: does the source directory exist?
    if [ ! -d $root/$dir ]
    then
        echo "FAIL: local directory $root/$dir is missing."
    else
#        # check: are we installing to a brand new dir?
#        ssh $host test -d $root
#        if [ $? -ne 0 ]
#        then
#            echo "CAUTION: remote directory $host:$root is missing."
#        else
#            # check: are we in danger of overwriting files?
#            ssh $host test -d $root/$dir
#            if [ $? -eq 0 ]
#            then
#                echo "CAUTION: remote directory $host:$root/$dir already exists."
#                echo "         This script will not risk overwriting files."
#            else
                echo "archiving $root/$dir  ..."
                tar -cf $tar_dir/$tarball $root/$dir
                echo "copying to $host  ..."
                scp $tar_dir/$tarball $host:$tar_dir
                echo "extracting $host:$tar_dir/$tarball ..."
                ssh $host tar -xpf $tar_dir/$tarball
                echo "removing tarfiles ..."
                rm $tar_dir/$tarball
                ssh $host rm $tar_dir/$tarball
#            fi
#        fi
    fi
}
#---------#---------#---------#---------#---------#---------#---------#---------
# The name of the directory to copy is given like this:
#   root-path/directory
# eg. for /export/home/larg/scripts,
# the root-path is /export/home/larg and directory is scripts.
#
# copy plaintext dir
    root=/opt/iplanet/iws6/isp01
    dir=https-$dns-80
    transfer_tree

# copy SSL dir
    root=/opt/iplanet/iws6/isp01
    dir=https-$dns-443
    transfer_tree

# copy SSL dir
    root=/opt/iplanet/iws6/isp01
    dir=https-$dns-444
    transfer_tree

# copy document tree
    root=//var/www/docroots/isp01
    dir=$dns
    transfer_tree

# copy ACLs
    root=/opt/iplanet/iws6/isp01
    dir=httpacl
    tar_dir=/tmp
    tarball=$dir.tar

	echo "archiving ACLs in $root/$dir  ..."
	tar -cf $tar_dir/$tarball \
		$root/$dir/generated.https-$dns-443.acl \
		$root/$dir/genwork.https-$dns-443.acl \
		$root/$dir/generated.https-$dns-444.acl \
		$root/$dir/genwork.https-$dns-444.acl \
		$root/$dir/generated.https-$dns-80.acl \
		$root/$dir/genwork.https-$dns-80.acl \
	    2>/dev/null
	echo "copying to $host  ..."
	scp $tar_dir/$tarball $host:$tar_dir
	echo "extracting $host:$tar_dir/$tarball ..."
	ssh $host tar -xpf $tar_dir/$tarball
	echo "removing tarfiles ..."
	rm $tar_dir/$tarball
	ssh $host rm $tar_dir/$tarball

#---------#---------#---------#---------#---------#---------#---------#---------
