1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tex/vc Tue Aug 16 17:27:34 2005 +0000
1.3 @@ -0,0 +1,140 @@
1.4 +#! /bin/bash
1.5 +#
1.6 +
1.7 +set -e # abort on errors
1.8 +shopt -s nullglob # file globs that don't match expand
1.9 + # to nothing
1.10 +
1.11 +#
1.12 +# Check if $EDITOR is set, otherwise set to "vi".
1.13 +#
1.14 +: ${EDITOR:=vi}
1.15 +
1.16 +#
1.17 +# Check for -f option ()
1.18 +#
1.19 +if [ "$1" = -f ]; then
1.20 + retry_lock=1
1.21 + shift
1.22 +fi
1.23 +
1.24 +#
1.25 +# Detect the name of the file to edit
1.26 +#
1.27 +unset FILE
1.28 +
1.29 +if [ -n "$1" ]; then
1.30 + if [ -f "$1" ]; then
1.31 + FILE=$1
1.32 + else
1.33 + if [ -d "$1" ]; then
1.34 + cd $1
1.35 + else
1.36 + FILE=$(package_name $1).changelog
1.37 + fi
1.38 + fi
1.39 +fi
1.40 +
1.41 +if [ -z "$FILE" ]; then
1.42 + for f in *.changelog; do
1.43 + if [ -n "$FILE" ]; then
1.44 + FILE=
1.45 + break
1.46 + fi
1.47 + FILE=$f
1.48 + done
1.49 +fi
1.50 +
1.51 +if [ -z "$FILE" ]; then
1.52 + for f in *.spec; do
1.53 + if [ -n "$FILE" ]; then
1.54 + FILE=
1.55 + break
1.56 + fi
1.57 + FILE=$f
1.58 + done
1.59 + if [ -n "$FILE" ]; then
1.60 + FILE=$(package_name $FILE).changelog
1.61 + fi
1.62 +fi
1.63 +
1.64 +if [ -z "$FILE" ]; then
1.65 + echo "usage: ${0##*/} [filename[.changelog]|path [file_with_comment]]"
1.66 + echo "If no <filename> is given, exactly one *.changelog or"
1.67 + echo "*.spec file has to be in the cwd or in <path>."
1.68 + echo
1.69 + exit 1
1.70 +fi
1.71 +
1.72 +user=`whoami`
1.73 +
1.74 +
1.75 +COMMENT_FILE=$2
1.76 +
1.77 +#
1.78 +# Create the lockfile and temporary file.
1.79 +#
1.80 +lockfile=.${FILE##*/}.lock
1.81 +if [ "${FILE/\//}" != "$FILE" ]; then
1.82 + lockfile=${FILE%/*}/.$lockfile
1.83 +fi
1.84 +
1.85 +if [ ! -w "$(dirname "$FILE")" ]; then
1.86 + echo "Write access to directory $(dirname "$FILE") required" >&2
1.87 + exit 1
1.88 +fi
1.89 +
1.90 +if [ -e "$FILE" -a ! -w "$FILE" ]; then
1.91 + echo "Write access to file $FILE required" >&2
1.92 + exit 1
1.93 +fi
1.94 +
1.95 +set -o noclobber
1.96 +while ! echo $$@$(hostname -f) 2> /dev/null > $lockfile; do
1.97 + if [ -z "$retry_lock" ]; then
1.98 + echo "$lockfile: Lock by process $(cat $lockfile)" >&2
1.99 + echo "Please remove stale lockfiles manually" >&2
1.100 + exit 1
1.101 + fi
1.102 + echo "$lockfile: Waiting for process $(cat $lockfile) to release lock" >&2
1.103 + sleep 1
1.104 +done
1.105 +set +o noclobber
1.106 +
1.107 +tmpfile=$(mktemp /tmp/${0##*/}.XXXXXX)
1.108 +trap "rm -f $lockfile $tmpfile" EXIT
1.109 +
1.110 +#
1.111 +# Prepare the working copy and start the editor
1.112 +#
1.113 +
1.114 +{ timestamp=$(LC_ALL=POSIX TZ=Europe/Berlin date)
1.115 + echo "-------------------------------------------------------------------"
1.116 + echo "$timestamp - $user"
1.117 + echo
1.118 + if [ -z "$COMMENT_FILE" ]; then
1.119 + echo "- "
1.120 + else
1.121 + cat $COMMENT_FILE
1.122 + fi
1.123 + echo
1.124 + if [ -f "$FILE" ]; then
1.125 + cat $FILE
1.126 + fi
1.127 +} >> $tmpfile \
1.128 +|| exit 1
1.129 +
1.130 +if [ -z "$COMMENT_FILE" ]; then
1.131 + lines=1
1.132 + CHKSUM_BEFORE=$(md5sum $tmpfile | awk '{print $1}')
1.133 +else
1.134 + lines=$(wc -l $COMMENT_FILE | awk '{print $1}')
1.135 + CHKSUM_BEFORE=has_changed
1.136 +fi
1.137 +
1.138 +$EDITOR +$((3+lines)) $tmpfile
1.139 +
1.140 +if [ "$CHKSUM_BEFORE" = "$(md5sum $tmpfile | awk '{print $1}')" ]; then
1.141 + exit 1
1.142 +fi
1.143 +cat $tmpfile > $FILE