#!/bin/sh
#
# Script to update the Pencil-Code tree from CVS
#
# Update all the necessaries but nothing else.
#  (eg. miss out the runs directory)
#
cmdname=`basename $0`
warn_sticky()
{
[ $1 ] || return
cat <<!
*************************************************
***  WARNING: Sticky Tags Found               ***
*************************************************
* Sticky tags/keywords/dates have been found    *
* on files in the following paths:              *
!
  while [ "$1" ]; do
    echo $1 | perl -pe 's@(.*?)CVS/Entries@*   $1@'
    shift
  done
cat <<!
*                    (-kb and -ko are ignored)  *
* Paths relative to: $PENCIL_HOME
*************************************************
***  Try using "$cmdname -A" to clear them.  ***
*************************************************
!
}


check_for_sticky()
{
  sticky=
  while [ "$1" ]; do
    if [ -d "$1" ]; then
      sticky="$sticky `find $1/ -path '*/CVS/Entries' | xargs egrep -lv 'D|//$|/-kb/$|/-ko/$'`"
    fi
    shift
  done

  warn_sticky $sticky
}

check_for_sticky_local()
{
  sticky=
  while [ "$1" ]; do
    if [ -d "$1" ]; then
      sticky="$sticky `find $1/ -maxdepth 2 -path '*/CVS/Entries' | xargs egrep -lv 'D|//$|/-kb/$|/-ko/$'`"
    fi
    shift
  done

  warn_sticky $sticky
}


add_update_path()
{
  while [ $1 ]; do
    [ -f $1 ] || [ -d $1 ] && UPDATE_PATHS="$UPDATE_PATHS $1"
    shift
  done
}

add_update_path_nonewdir()
{
  while [ $1 ]; do
    [ -f $1 ] || [ -d $1 ] && UPDATE_PATHS_NONEWDIR="$UPDATE_PATHS_NONEWDIR $1"
    shift
  done
}

if [ ! "$PENCIL_HOME" ]; then
  echo PENCIL_HOME variable not set! Exiting...
  exit 1
fi
if [ ! -d "$PENCIL_HOME/" ]; then
  echo PENCIL_HOME variable set but is not an existing directory! Exiting...
  exit 1
fi

while expr "$1" : '\-' >/dev/null; do
  case $1 in
    -a)
      include_rundirs=1
    ;;
    -A)
      EXTRA_ARGS=-A
      echo "Found STICKY CLEANER"
    ;;
    -A)
      EXTRA_ARGS=-A
      echo "Found STICKY CLEANER"
      shift
    ;;
    --all)
      include_rundirs=1
  esac
# catch other arguments
  EXTRA_ARGS=$1
  shift
done
cd ${PENCIL_HOME}

EXTRA_ARGS="$EXTRA_ARGS $@"
UPDATE_PATHS="src bin lib idl license"
UPDATE_PATHS_NONEWDIR=""

# Conditionally add some other directories to the full update
#   (if they exist!)
add_update_path samples doc

#  Add other, perhaps personal trees to the shallow update
[ $include_rundirs ] && add_update_path_nonewdir runs pencil-runs
[ $include_rundirs ] && add_update_path_nonewdir tony pencil-tony
[ $include_rundirs ] && add_update_path_nonewdir anders pencil-anders

# Shallow update other paths in the selected subdirectories
if [ "$UPDATE_PATHS_NONEWDIR" ]; then
  echo "Updating the subtrees of $PENCIL_HOME ($UPDATE_PATHS_NONEWDIR)"
  cvs -q up -P $EXTRA_ARGS $UPDATE_PATHS_NONEWDIR
  check_for_sticky $UPDATE_PATHS_NONEWDIR
fi

# Update files in the PENCIL_HOME root
echo "Updating the root of $PENCIL_HOME"
cvs -q up -l $EXTRA_ARGS .
check_for_sticky_local .

# Update files in the selected subdirectories
echo "Updating the subtrees of $PENCIL_HOME ($UPDATE_PATHS)"
cvs -q up -dP $EXTRA_ARGS $UPDATE_PATHS
check_for_sticky $UPDATE_PATHS