#! /bin/sh
#
# log.sh -- simple shell log script for CVS's loginfo
# Copyright (c) 2003  Michael Weber <michaelw@foldr.org>

# Version:	20041014.1
# Author:	michaelw (with kind additions from an anonymous it.uu.se admin)
# Tested:	POSIX shell
 
# This program 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 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Usage example:
# $CVSROOT/CVSROOT/loginfo:
# some-module $CVSROOT/CVSROOT/log.sh -m user1@example.com -m user2@example.com -- %s
#
# HINT: DO NOT FORGET "%s" AT THE END OF THE COMMAND LINE!

# Parameters:
#   -m USER@MAIL.EXAMPLE.COM    specifies which users get log mails
#
#   --                          after  '--', nothing is treated
#	                        as an option any more.

MAIL="mail"
RCS="cvs -nQq status -v"
WHOAMI="whoami"

while [ $# -gt 0 ]
do
  case "$1" in
    -m) shift; users="${users} $1"; shift;;
    -u) shift; login="$1"; shift;;
    -s) shift; RCS="";;
    --) shift; break;;
    -*) echo "E: log.sh: unknown option '$1'" 1>&2; exit 1;;
    *)  break;;
  esac
done

if [ $# -eq 0 ]
then
  echo "E: log.sh: no file specified" 1>&2
  exit 1
fi

# Bugfix, if all files are inserted as a single argument
if [ $# -eq 1 ]
then
  set $1
fi

modulepath="$1"; shift;

if [ -z "${login}" ]
then
  login="${CVS_USER:-${USER}}"
fi
if [ -z "${login}" ]
then
  login=$(${WHOAMI})
fi
if [ -z "${login}" ]
then
  login="nobody"
fi

(
cat <<EOF
Date:   `date`
Author: ${login}

EOF

# print log message
cat

if [ -z "${RCS}" ]
then
  echo "Modulepath: ${modulepath}"
  echo "Files: $*"
else
  for file
  do
    if [ "${file}" = "-" ]
    then
      echo "[input file was '-']"
      continue
    fi
    echo "${RCS} -- ${file}"
    ${RCS} -- "${file}"
  done
fi
) | ${MAIL} -s "CVS update: ${CVSROOT}/${modulepath}" ${users}
