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