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