insilmaril@147: #! /bin/bash
insilmaril@147: #
insilmaril@147: 
insilmaril@147: set -e  # abort on errors
insilmaril@147: shopt -s nullglob  # file globs that don't match expand
insilmaril@147: 		   # to nothing
insilmaril@147: 
insilmaril@147: #
insilmaril@147: # Check if $EDITOR is set, otherwise set to "vi".
insilmaril@147: #
insilmaril@147: : ${EDITOR:=vi}
insilmaril@147: 
insilmaril@147: #
insilmaril@147: # Check for -f option ()
insilmaril@147: #
insilmaril@147: if [ "$1" = -f ]; then
insilmaril@147:     retry_lock=1
insilmaril@147:     shift
insilmaril@147: fi
insilmaril@147: 
insilmaril@147: #
insilmaril@147: # Detect the name of the file to edit
insilmaril@147: #
insilmaril@147: unset FILE
insilmaril@147: 
insilmaril@147: if [ -n "$1" ]; then
insilmaril@147:     if [ -f "$1" ]; then
insilmaril@147: 	FILE=$1
insilmaril@147:     else
insilmaril@147: 	if [ -d "$1" ]; then
insilmaril@147: 	    cd $1
insilmaril@147: 	else
insilmaril@147: 	    FILE=$(package_name $1).changelog
insilmaril@147: 	fi
insilmaril@147:     fi
insilmaril@147: fi
insilmaril@147: 
insilmaril@147: if [ -z "$FILE" ]; then
insilmaril@147:     for f in *.changelog; do
insilmaril@147: 	if [ -n "$FILE" ]; then
insilmaril@147: 	    FILE=
insilmaril@147: 	    break
insilmaril@147: 	fi
insilmaril@147: 	FILE=$f
insilmaril@147:     done
insilmaril@147: fi
insilmaril@147: 
insilmaril@147: if [ -z "$FILE" ]; then
insilmaril@147:     for f in *.spec; do
insilmaril@147: 	if [ -n "$FILE" ]; then
insilmaril@147: 	    FILE=
insilmaril@147: 	    break
insilmaril@147: 	fi
insilmaril@147: 	FILE=$f
insilmaril@147:     done
insilmaril@147:     if [ -n "$FILE" ]; then
insilmaril@147: 	FILE=$(package_name $FILE).changelog
insilmaril@147:     fi
insilmaril@147: fi
insilmaril@147: 
insilmaril@147: if [ -z "$FILE" ]; then
insilmaril@147:     echo "usage: ${0##*/} [filename[.changelog]|path [file_with_comment]]"
insilmaril@147:     echo "If no <filename> is given, exactly one *.changelog or"
insilmaril@147:     echo "*.spec file has to be in the cwd or in <path>."
insilmaril@147:     echo
insilmaril@147:     exit 1
insilmaril@147: fi
insilmaril@147: 
insilmaril@147: user=`whoami`
insilmaril@147: 
insilmaril@147: 
insilmaril@147: COMMENT_FILE=$2
insilmaril@147: 
insilmaril@147: #
insilmaril@147: # Create the lockfile and temporary file.
insilmaril@147: #
insilmaril@147: lockfile=.${FILE##*/}.lock
insilmaril@147: if [ "${FILE/\//}" != "$FILE" ]; then
insilmaril@147:     lockfile=${FILE%/*}/.$lockfile
insilmaril@147: fi
insilmaril@147: 
insilmaril@147: if [ ! -w "$(dirname "$FILE")" ]; then
insilmaril@147:     echo "Write access to directory $(dirname "$FILE") required" >&2
insilmaril@147:     exit 1
insilmaril@147: fi
insilmaril@147: 
insilmaril@147: if [ -e "$FILE" -a ! -w "$FILE" ]; then
insilmaril@147:     echo "Write access to file $FILE required" >&2
insilmaril@147:     exit 1
insilmaril@147: fi
insilmaril@147: 
insilmaril@147: set -o noclobber
insilmaril@147: while ! echo $$@$(hostname -f) 2> /dev/null > $lockfile; do
insilmaril@147:     if [ -z "$retry_lock" ]; then
insilmaril@147: 	echo "$lockfile: Lock by process $(cat $lockfile)" >&2
insilmaril@147: 	echo "Please remove stale lockfiles manually" >&2
insilmaril@147: 	exit 1
insilmaril@147:     fi
insilmaril@147:     echo "$lockfile: Waiting for process $(cat $lockfile) to release lock" >&2
insilmaril@147:     sleep 1
insilmaril@147: done
insilmaril@147: set +o noclobber
insilmaril@147: 
insilmaril@147: tmpfile=$(mktemp /tmp/${0##*/}.XXXXXX)
insilmaril@147: trap "rm -f $lockfile $tmpfile" EXIT
insilmaril@147: 
insilmaril@147: #
insilmaril@147: # Prepare the working copy and start the editor
insilmaril@147: #
insilmaril@147: 
insilmaril@147: {   timestamp=$(LC_ALL=POSIX TZ=Europe/Berlin date)
insilmaril@147:     echo "-------------------------------------------------------------------"
insilmaril@147:     echo "$timestamp - $user"
insilmaril@147:     echo
insilmaril@147:     if [ -z "$COMMENT_FILE" ]; then
insilmaril@147: 	echo "- "
insilmaril@147:     else
insilmaril@147: 	cat $COMMENT_FILE
insilmaril@147:     fi
insilmaril@147:     echo
insilmaril@147:     if [ -f "$FILE" ]; then
insilmaril@147:     	cat $FILE
insilmaril@147:     fi
insilmaril@147: } >> $tmpfile \
insilmaril@147: || exit 1
insilmaril@147: 
insilmaril@147: if [ -z "$COMMENT_FILE" ]; then
insilmaril@147:     lines=1
insilmaril@147:     CHKSUM_BEFORE=$(md5sum $tmpfile | awk '{print $1}')
insilmaril@147: else
insilmaril@147:     lines=$(wc -l $COMMENT_FILE | awk '{print $1}')
insilmaril@147:     CHKSUM_BEFORE=has_changed
insilmaril@147: fi
insilmaril@147: 
insilmaril@147: $EDITOR +$((3+lines)) $tmpfile
insilmaril@147: 
insilmaril@147: if [ "$CHKSUM_BEFORE" = "$(md5sum $tmpfile | awk '{print $1}')" ]; then
insilmaril@147:     exit 1
insilmaril@147: fi
insilmaril@147: cat $tmpfile > $FILE