#!/bin/sh
# $Id: nice-commit.sh 396 2005-10-21 20:57:43Z taal $
TMPDIFF="/tmp/cvs-$USER-diff.$$"
ERRORS=0
for file in "$@"; do
  file "$file" | grep 'shell script text' && { sh -n "$file" || ERRORS=1; }
  file "$file" | grep 'perl script text'  && { perl -wc "$file" || ERRORS=1; }
  [ -x /usr/bin/tidy ] && file "$file" | \
    grep 'ML document text'  && { /usr/bin/tidy -e "$file" || ERRORS=1; }
  grep -q '^<?$' "$file" && { php -l "$file" || ERRORS=1; }
done

if [ $ERRORS -eq 0 ]; then
  echo "Syntax OK"
  sleep 2
else
  echo "Syntax errors, see above"
  exit 1
fi

#exit 177

if [ -d "`dirname "$1"`/CVS" ]; then
  MINPLUS=`cvs diff "$@" | grep "^[-+]" | grep -v "^---\|^+++" | cut -b 1 | sort | uniq -c | xargs echo`
  echo "# $MINPLUS" > "$TMPDIFF"
  cvs diff -u "$@" >> "$TMPDIFF"
  echo '# vim:ft=diff:syntax=diff' >> "$TMPDIFF"
  CVSEDITOR="vim -o $TMPDIFF" cvs commit "$@"
fi
if [ -d "`dirname "$1"`/.svn" ]; then
  MINPLUS=`svn diff "$@" | grep "^[-+]" | grep -v "^---\|^+++" | cut -b 1 | sort | uniq -c | xargs echo`
  echo "# $MINPLUS" > "$TMPDIFF"
  svn diff "$@" >> "$TMPDIFF"
  echo '# vim:ft=diff:syntax=diff' >> "$TMPDIFF"
  EDITOR="vim -o $TMPDIFF" svn commit "$@"
fi
rm -f $TMPDIFF
