#!/bin/bash

# include the configuration file
source /root/etc/config || {
	echo "ERROR: Cannot source /root/etc/config." >&2
	exit 1
}

# get opts
aOpts=""
while [ "${1:0:1}" = "-" ]; do
	case "$1" in
		--master) MASTER_ACCOUNT="$2"; shift 2;;
		*) aOpts="$aOpts $1"; shift;;
	esac
done

# help?
if [ "$aOpts" = " -h" -o "$aOpts" = " --help" -o -z "$*" ]; then
	echo "hspull - copies files from master host"
	echo 
	echo "syntax: hspull [--master <master account>] [OPTIONS] [FILELIST]"
	echo "sample: hspull -P222 '*'"
	echo
	echo "Takes all single OPTIONS from OpenSSHs scp."
	echo "Copies each file from FILELIST from the master host."
	echo "Accesses master host by ${MASTER_ACCOUNT}."
	echo "Cannot really deal with directories yet."
	exit
fi

# iterate through all files (arguments)
for f in `ssh ${MASTER_ACCOUNT} ls -1 "$*"`; do
	aTarget=$aTempDir/`basename $f`
	scp $aOpts ${MASTER_ACCOUNT}:$f $f
	echo "$(date +'%Y-%m-%d %H:%M:%S') $f" >>/var/log/hostsharing/hspull.log
done

