author | insilmaril |
Tue, 11 Apr 2006 14:34:14 +0000 | |
changeset 294 | 3492af261af2 |
parent 147 | 40de292411b6 |
child 612 | 316e9237794c |
permissions | -rwxr-xr-x |
insilmaril@147 | 1 |
#! /bin/bash |
insilmaril@147 | 2 |
# |
insilmaril@147 | 3 |
|
insilmaril@147 | 4 |
set -e # abort on errors |
insilmaril@147 | 5 |
shopt -s nullglob # file globs that don't match expand |
insilmaril@147 | 6 |
# to nothing |
insilmaril@147 | 7 |
|
insilmaril@147 | 8 |
# |
insilmaril@147 | 9 |
# Check if $EDITOR is set, otherwise set to "vi". |
insilmaril@147 | 10 |
# |
insilmaril@147 | 11 |
: ${EDITOR:=vi} |
insilmaril@147 | 12 |
|
insilmaril@147 | 13 |
# |
insilmaril@147 | 14 |
# Check for -f option () |
insilmaril@147 | 15 |
# |
insilmaril@147 | 16 |
if [ "$1" = -f ]; then |
insilmaril@147 | 17 |
retry_lock=1 |
insilmaril@147 | 18 |
shift |
insilmaril@147 | 19 |
fi |
insilmaril@147 | 20 |
|
insilmaril@147 | 21 |
# |
insilmaril@147 | 22 |
# Detect the name of the file to edit |
insilmaril@147 | 23 |
# |
insilmaril@147 | 24 |
unset FILE |
insilmaril@147 | 25 |
|
insilmaril@147 | 26 |
if [ -n "$1" ]; then |
insilmaril@147 | 27 |
if [ -f "$1" ]; then |
insilmaril@147 | 28 |
FILE=$1 |
insilmaril@147 | 29 |
else |
insilmaril@147 | 30 |
if [ -d "$1" ]; then |
insilmaril@147 | 31 |
cd $1 |
insilmaril@147 | 32 |
else |
insilmaril@147 | 33 |
FILE=$(package_name $1).changelog |
insilmaril@147 | 34 |
fi |
insilmaril@147 | 35 |
fi |
insilmaril@147 | 36 |
fi |
insilmaril@147 | 37 |
|
insilmaril@147 | 38 |
if [ -z "$FILE" ]; then |
insilmaril@147 | 39 |
for f in *.changelog; do |
insilmaril@147 | 40 |
if [ -n "$FILE" ]; then |
insilmaril@147 | 41 |
FILE= |
insilmaril@147 | 42 |
break |
insilmaril@147 | 43 |
fi |
insilmaril@147 | 44 |
FILE=$f |
insilmaril@147 | 45 |
done |
insilmaril@147 | 46 |
fi |
insilmaril@147 | 47 |
|
insilmaril@147 | 48 |
if [ -z "$FILE" ]; then |
insilmaril@147 | 49 |
for f in *.spec; do |
insilmaril@147 | 50 |
if [ -n "$FILE" ]; then |
insilmaril@147 | 51 |
FILE= |
insilmaril@147 | 52 |
break |
insilmaril@147 | 53 |
fi |
insilmaril@147 | 54 |
FILE=$f |
insilmaril@147 | 55 |
done |
insilmaril@147 | 56 |
if [ -n "$FILE" ]; then |
insilmaril@147 | 57 |
FILE=$(package_name $FILE).changelog |
insilmaril@147 | 58 |
fi |
insilmaril@147 | 59 |
fi |
insilmaril@147 | 60 |
|
insilmaril@147 | 61 |
if [ -z "$FILE" ]; then |
insilmaril@147 | 62 |
echo "usage: ${0##*/} [filename[.changelog]|path [file_with_comment]]" |
insilmaril@147 | 63 |
echo "If no <filename> is given, exactly one *.changelog or" |
insilmaril@147 | 64 |
echo "*.spec file has to be in the cwd or in <path>." |
insilmaril@147 | 65 |
echo |
insilmaril@147 | 66 |
exit 1 |
insilmaril@147 | 67 |
fi |
insilmaril@147 | 68 |
|
insilmaril@147 | 69 |
user=`whoami` |
insilmaril@147 | 70 |
|
insilmaril@147 | 71 |
|
insilmaril@147 | 72 |
COMMENT_FILE=$2 |
insilmaril@147 | 73 |
|
insilmaril@147 | 74 |
# |
insilmaril@147 | 75 |
# Create the lockfile and temporary file. |
insilmaril@147 | 76 |
# |
insilmaril@147 | 77 |
lockfile=.${FILE##*/}.lock |
insilmaril@147 | 78 |
if [ "${FILE/\//}" != "$FILE" ]; then |
insilmaril@147 | 79 |
lockfile=${FILE%/*}/.$lockfile |
insilmaril@147 | 80 |
fi |
insilmaril@147 | 81 |
|
insilmaril@147 | 82 |
if [ ! -w "$(dirname "$FILE")" ]; then |
insilmaril@147 | 83 |
echo "Write access to directory $(dirname "$FILE") required" >&2 |
insilmaril@147 | 84 |
exit 1 |
insilmaril@147 | 85 |
fi |
insilmaril@147 | 86 |
|
insilmaril@147 | 87 |
if [ -e "$FILE" -a ! -w "$FILE" ]; then |
insilmaril@147 | 88 |
echo "Write access to file $FILE required" >&2 |
insilmaril@147 | 89 |
exit 1 |
insilmaril@147 | 90 |
fi |
insilmaril@147 | 91 |
|
insilmaril@147 | 92 |
set -o noclobber |
insilmaril@147 | 93 |
while ! echo $$@$(hostname -f) 2> /dev/null > $lockfile; do |
insilmaril@147 | 94 |
if [ -z "$retry_lock" ]; then |
insilmaril@147 | 95 |
echo "$lockfile: Lock by process $(cat $lockfile)" >&2 |
insilmaril@147 | 96 |
echo "Please remove stale lockfiles manually" >&2 |
insilmaril@147 | 97 |
exit 1 |
insilmaril@147 | 98 |
fi |
insilmaril@147 | 99 |
echo "$lockfile: Waiting for process $(cat $lockfile) to release lock" >&2 |
insilmaril@147 | 100 |
sleep 1 |
insilmaril@147 | 101 |
done |
insilmaril@147 | 102 |
set +o noclobber |
insilmaril@147 | 103 |
|
insilmaril@147 | 104 |
tmpfile=$(mktemp /tmp/${0##*/}.XXXXXX) |
insilmaril@147 | 105 |
trap "rm -f $lockfile $tmpfile" EXIT |
insilmaril@147 | 106 |
|
insilmaril@147 | 107 |
# |
insilmaril@147 | 108 |
# Prepare the working copy and start the editor |
insilmaril@147 | 109 |
# |
insilmaril@147 | 110 |
|
insilmaril@147 | 111 |
{ timestamp=$(LC_ALL=POSIX TZ=Europe/Berlin date) |
insilmaril@147 | 112 |
echo "-------------------------------------------------------------------" |
insilmaril@147 | 113 |
echo "$timestamp - $user" |
insilmaril@147 | 114 |
echo |
insilmaril@147 | 115 |
if [ -z "$COMMENT_FILE" ]; then |
insilmaril@147 | 116 |
echo "- " |
insilmaril@147 | 117 |
else |
insilmaril@147 | 118 |
cat $COMMENT_FILE |
insilmaril@147 | 119 |
fi |
insilmaril@147 | 120 |
echo |
insilmaril@147 | 121 |
if [ -f "$FILE" ]; then |
insilmaril@147 | 122 |
cat $FILE |
insilmaril@147 | 123 |
fi |
insilmaril@147 | 124 |
} >> $tmpfile \ |
insilmaril@147 | 125 |
|| exit 1 |
insilmaril@147 | 126 |
|
insilmaril@147 | 127 |
if [ -z "$COMMENT_FILE" ]; then |
insilmaril@147 | 128 |
lines=1 |
insilmaril@147 | 129 |
CHKSUM_BEFORE=$(md5sum $tmpfile | awk '{print $1}') |
insilmaril@147 | 130 |
else |
insilmaril@147 | 131 |
lines=$(wc -l $COMMENT_FILE | awk '{print $1}') |
insilmaril@147 | 132 |
CHKSUM_BEFORE=has_changed |
insilmaril@147 | 133 |
fi |
insilmaril@147 | 134 |
|
insilmaril@147 | 135 |
$EDITOR +$((3+lines)) $tmpfile |
insilmaril@147 | 136 |
|
insilmaril@147 | 137 |
if [ "$CHKSUM_BEFORE" = "$(md5sum $tmpfile | awk '{print $1}')" ]; then |
insilmaril@147 | 138 |
exit 1 |
insilmaril@147 | 139 |
fi |
insilmaril@147 | 140 |
cat $tmpfile > $FILE |