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