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