#!/bin/sh

if [ $# -eq 0 -o "$1" = "-h" -o "$1" = "--help" ] ; then
	echo "Usage: `basename $0` <fichiers à modifier>" 1>&2
	exit 1
fi

if test -z "$USER" ; then export USER=`whoami` ; fi

if test "$USER" = jmoreau ; then
  EDITOR='/usr/X11R6/bin/gvim -geom 88x40'
else
  if test -z "$EDITOR" ; then export EDITOR=emacs ; fi
fi

while [ $# -ge 1 ] ; do

  file="$1"

  cvs status "$file"|grep Status|grep -v 'Up-to-date' && cvs update "$file"

  editors=`cvs editors "$file"|cut -f-4`
  nb_editors=`echo $editors|grep -v ^$|wc -l`

  if [ $nb_editors -eq 0 ] ; then
    cvs edit "$file" && $EDITOR "$file"
  else
    ki=""
    if [ $nb_editors -eq 1 ] ; then ki=`cvs editors "$file"|cut -f2` ; fi
    if test "$ki" = `whoami` ; then
      echo "Vous êtes déjà éditeur de ce fichier."
      $EDITOR "$file"
    else
      echo -e "Il y a déjà" $nb_editors "éditeur(s) :\n$editors"
    fi
  fi

  shift

done