tex/vc
author insilmaril
Fri, 11 Sep 2009 12:56:15 +0000
changeset 794 d922fb6ea482
parent 612 316e9237794c
child 804 14f2b1b15242
permissions -rwxr-xr-x
more fixes for collisions
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@612
    69
#
insilmaril@612
    70
# Add domain to username (if it's me ;-)
insilmaril@612
    71
#
insilmaril@147
    72
user=`whoami`
insilmaril@612
    73
if [ $user = "uwedr" ]; then
insilmaril@612
    74
	user="$user@suse.de"
insilmaril@612
    75
fi
insilmaril@147
    76
insilmaril@147
    77
COMMENT_FILE=$2
insilmaril@147
    78
insilmaril@147
    79
#
insilmaril@147
    80
# Create the lockfile and temporary file.
insilmaril@147
    81
#
insilmaril@147
    82
lockfile=.${FILE##*/}.lock
insilmaril@147
    83
if [ "${FILE/\//}" != "$FILE" ]; then
insilmaril@147
    84
    lockfile=${FILE%/*}/.$lockfile
insilmaril@147
    85
fi
insilmaril@147
    86
insilmaril@147
    87
if [ ! -w "$(dirname "$FILE")" ]; then
insilmaril@147
    88
    echo "Write access to directory $(dirname "$FILE") required" >&2
insilmaril@147
    89
    exit 1
insilmaril@147
    90
fi
insilmaril@147
    91
insilmaril@147
    92
if [ -e "$FILE" -a ! -w "$FILE" ]; then
insilmaril@147
    93
    echo "Write access to file $FILE required" >&2
insilmaril@147
    94
    exit 1
insilmaril@147
    95
fi
insilmaril@147
    96
insilmaril@147
    97
set -o noclobber
insilmaril@147
    98
while ! echo $$@$(hostname -f) 2> /dev/null > $lockfile; do
insilmaril@147
    99
    if [ -z "$retry_lock" ]; then
insilmaril@147
   100
	echo "$lockfile: Lock by process $(cat $lockfile)" >&2
insilmaril@147
   101
	echo "Please remove stale lockfiles manually" >&2
insilmaril@147
   102
	exit 1
insilmaril@147
   103
    fi
insilmaril@147
   104
    echo "$lockfile: Waiting for process $(cat $lockfile) to release lock" >&2
insilmaril@147
   105
    sleep 1
insilmaril@147
   106
done
insilmaril@147
   107
set +o noclobber
insilmaril@147
   108
insilmaril@147
   109
tmpfile=$(mktemp /tmp/${0##*/}.XXXXXX)
insilmaril@147
   110
trap "rm -f $lockfile $tmpfile" EXIT
insilmaril@147
   111
insilmaril@147
   112
#
insilmaril@147
   113
# Prepare the working copy and start the editor
insilmaril@147
   114
#
insilmaril@147
   115
insilmaril@147
   116
{   timestamp=$(LC_ALL=POSIX TZ=Europe/Berlin date)
insilmaril@147
   117
    echo "-------------------------------------------------------------------"
insilmaril@147
   118
    echo "$timestamp - $user"
insilmaril@147
   119
    echo
insilmaril@147
   120
    if [ -z "$COMMENT_FILE" ]; then
insilmaril@147
   121
	echo "- "
insilmaril@147
   122
    else
insilmaril@147
   123
	cat $COMMENT_FILE
insilmaril@147
   124
    fi
insilmaril@147
   125
    echo
insilmaril@147
   126
    if [ -f "$FILE" ]; then
insilmaril@147
   127
    	cat $FILE
insilmaril@147
   128
    fi
insilmaril@147
   129
} >> $tmpfile \
insilmaril@147
   130
|| exit 1
insilmaril@147
   131
insilmaril@147
   132
if [ -z "$COMMENT_FILE" ]; then
insilmaril@147
   133
    lines=1
insilmaril@147
   134
    CHKSUM_BEFORE=$(md5sum $tmpfile | awk '{print $1}')
insilmaril@147
   135
else
insilmaril@147
   136
    lines=$(wc -l $COMMENT_FILE | awk '{print $1}')
insilmaril@147
   137
    CHKSUM_BEFORE=has_changed
insilmaril@147
   138
fi
insilmaril@147
   139
insilmaril@147
   140
$EDITOR +$((3+lines)) $tmpfile
insilmaril@147
   141
insilmaril@147
   142
if [ "$CHKSUM_BEFORE" = "$(md5sum $tmpfile | awk '{print $1}')" ]; then
insilmaril@147
   143
    exit 1
insilmaril@147
   144
fi
insilmaril@147
   145
cat $tmpfile > $FILE