#!/bin/sh

# Update the GPC CVS repository
#
# Author: Peter Gerwinski <peter@gerwinski.de>

if [ x"$GPC_SRCDIR" = x ]; then
  echo "GPC_SRCDIR must be set" >&2
  exit 1
fi

if [ -e "$GPC_SRCDIR/gcc/p/gpc.c" ]; then
  srcdir="$GPC_SRCDIR/gcc/p"
elif [ -e "$GPC_SRCDIR/p/gpc.c" ]; then
  srcdir="$GPC_SRCDIR/p"
else
  echo "Invalid GPC_SRCDIR" >&2
  exit 1
fi

if [ x"$GPC_CVSDIR" = x ]; then
  echo "GPC_CVSDIR must be set" >&2
  exit 1
fi

if [ -e "$GPC_CVSDIR/gcc/p/gpc.c" ]; then
  cvsdir="$GPC_CVSDIR/gcc/p"
elif [ -e "$GPC_CVSDIR/p/gpc.c" ]; then
  cvsdir="$GPC_CVSDIR/p"
else
  echo "Invalid GPC_CVSDIR" >&2
  exit 1
fi

cd "$cvsdir"

if [ x"$TMPDIR" = x ]; then
  TMPDIR="/tmp"
fi
TMP="$TMPDIR/`basename "$0"`.$$"
rm -rf "$TMP" || exit 1
mkdir "$TMP" || exit 1
trap 'cd /; rm -rf "$TMP"; exit 1' SIGINT 0
cvsoutput="$TMP/gpc_cvsoutput"

cvs -n $GPC_CVSOPT update | grep -v test/internal > "$cvsoutput" 2>/dev/null || exit 1

newfiles="`cat "$cvsoutput" | grep "^?" | cut -b 3-`"
rmfiles="`cat "$cvsoutput" | grep "^U" | cut -b 3-`"

echo
echo "Files to be added to or removed from the CVS repository:"

for x in $newfiles; do
  echo "adding $x"
done

for x in $rmfiles; do
  if [ ! -f "$x" ]; then
    echo "removing $x"
  fi
done

echo "Press ENTER to continue; ^C to abort"
read junk

for x in $newfiles; do
  cvs $GPC_CVSOPT add "$x"
done

for x in $rmfiles; do
  if [ ! -f "$x" ]; then
    cvs $GPC_CVSOPT remove "$x"
  fi
done

cd ..
cvs $GPC_CVSOPT commit -R

# Clean up
cd /
rm -rf "$TMP" || exit 1
trap "" 0
