#!/bin/sh

# Update the GPC CVS repository
#
# Copyright (C) 2000-2004 Free Software Foundation, Inc.
#
# Author: Peter Gerwinski <peter@gerwinski.de>
#
# This file is part of GNU Pascal.
#
# GNU Pascal is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# GNU Pascal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Pascal; see the file COPYING. If not, write to the
# Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

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"
  cvsparentdir="$GPC_CVSDIR/gcc"
elif [ -e "$GPC_CVSDIR/p/gpc.c" ]; then
  cvsdir="$GPC_CVSDIR/p"
  cvsparentdir="$GPC_CVSDIR"
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' INT 0
cvsoutput="$TMP/gpc_cvsoutput"

cvs -n $GPC_CVSOPT update | grep -v '^. README$' \
                          | grep -v '^. test/README$' \
                          > "$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 "$cvsparentdir"
cvs $GPC_CVSOPT commit -R

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