#!/bin/bash
#set -x

myname=$( basename $0)

GPG_RECIPIENT="B4465B57"
GPG_REC_OPTS=""
for i in $GPG_RECIPIENT; do 
  GPG_REC_OPTS="$GPG_REC_OPTS -r $i "
done

gpg < /dev/null > /dev/null 2>&1
gpg --list-keys $GPG_RECIPIENT > /dev/null 2>&1 || {

gpg --keyserver hkp://subkeys.pgp.net --recv-keys $GPG_RECIPIENT

}

# find buffer, if there.
unalias buffer > /dev/null 2>&1
BUFFER=$( type -p buffer )
if [ "$BUFFER" != "" -a -f "$BUFFER" ]; then
  BUFFER="buffer -s 500k"
else
  BUFFER=cat
fi

export DEBUG=normal

dirs=""
while [ "$1" != "" ]; do
  case "$1" in
    -d)
	DEBUG="yes"
	;;
    *)
	BASE_SRC="$1"
	shift
	BASE_DEST="$1"
  esac
  shift
done

if [ "$BASE_SRC" = "" -o "$BASE_DEST" = "" ]; then
	{
	echo "usage: $0 <source_basedir> <destination_basedir>"
	echo "Fatal."
	} 1>&2
	exit 3
fi

if [ ! -d "$BASE_SRC" ]; then
	{
	echo "$0: source_basedir $BASE_SRC does not exist. Fatal."
	} 1>&2
	exit 4
fi

if [ ! -d "$BASE_DEST" ]; then
	{
        echo "$0: destination_basedir $BASE_DEST does not exist. Fatal."
	} 1>&2
        exit 5
fi

pushd . 		> /dev/null 2>&1
cd "$BASE_DEST"
D=$( pwd -P )
popd 			> /dev/null 2>&1
pushd . 		> /dev/null 2>&1
cd "$BASE_SRC"
S=$( pwd -P )
popd			> /dev/null 2>&1
if [ "$S" = "$D" ]; then
	{
	echo "\$BASE_SRC and \$BASE_DEST are the same. Fatal."
	} 1>&2
	exit 6
fi

function crypt_file() {
    INFILE=$1
    OUTFILE=$2
#    $BUFFER < "$INFILE" | \
# 	gpg -z 9 -q --batch --options /dev/null --trust-model always \
#	 -e --hidden-recipient draht@suse.de \
# 	 | $BUFFER > "$OUTFILE"

gpg -z 9 -q --batch --options /dev/null --trust-model always -e --hidden-recipient draht@suse.de < "$INFILE" > "$OUTFILE"

}

ERRLOG="$BASE_SRC"/."$myname"-log
touch "$ERRLOG" > /dev/null 2>&1 || ERRLOG=/root/."$myname"-log
touch "$ERRLOG" > /dev/null 2>&1 || {
	echo "cannot open error log $ERRLOG. Fatal." 1>&2
	exit 6
}

FILELOG="$BASE_SRC"/."$myname"-files
touch "$FILELOG" > /dev/null 2>&1 || FILELOG=/root/."$myname"-files
touch "$FILELOG" > /dev/null 2>&1 || {
	echo "cannot open file log $FILELOG. Fatal." 1>&2
	exit 7
}

exec 2> "$ERRLOG"
exec 42> "$FILELOG"

# kindof tee...
#if [ -t 0 ]; then
#	tail -f "$ERRLOG" &
#fi

##############################################################################
while read -d " " f; do

  if [ -d "$f" ]; then
    find "$f" -maxdepth 0 -printf 'd %u:%g %m %p\n' 1>&42
    continue
  fi
  if [ -L "$f" ]; then
    target=$( readlink "$f" )
    find "$f" -maxdepth 0 -printf 'l %u:%g %l %p\n' 1>&42
    continue
  fi

  d=$( dirname "$f" )
  if [ ! -d "$BASE_DEST/$d" ]; then
    mkdir -p "$BASE_DEST/$d"
  fi

  if [ -f "$BASE_DEST/$f" -a "$BASE_DEST/$f" -nt "$f" ]; then
    continue
  fi

  find "$f" -maxdepth 0 -printf 'f %u:%g %m %s %p\n' 1>&42

  if [ -f "$BASE_DEST/$f" ]; then
	dinum=$( find "$BASE_DEST/$f" -maxdepth 0 -printf '%i' )
	sinum=$( find $f -maxdepth 0 -printf '%i' )
	if [ "$dinum" = "$sinum" ]; then
	    echo "source and destination file are the same. Not overwriting: $f" 1>&2
	    continue
	fi
  fi
  crypt_file "$f" "$BASE_DEST/$f"
  ret=$?
  if [ "$ret" != 0 ]; then
    echo "$f" >> "$BASE_SRC"/.out-$myname-errors
  fi

done < <( find "$BASE_SRC" -mount \( -type f -o -type d -o -type l \) -print0 )

exec 42>&-
exec 2>&-
exec 2>&1

cat "$ERRLOG" >> "$BASE_DEST"/"$BASE_DEST"
cat "$FILELOG" >> "$BASE_DEST"/"$FILELOG"
rm -f "$ERRLOG" "$FILELOG"

