#!/bin/bash # # Ross J. Michaels (1995) # rjm2@lehigh.edu # # and # # Judith Elaine # blondie@cybertia.org.tenn.com # # (Only the major functional lines were entered by Bill # Stearns (wstearns@pobox.com). The full text can be found # in the November 1995 Linux Gazette at: # http://www.ssc.com/lg/linux/gazette.nov.html # # Since this was typed in, improvements have been made by Bill # Stearns. #Version 0.5 #FIXME: check for unset HOME, fix with /root, /home/nnnn, /tmp CONFIG_DIR="$HOME/config" DIR_DIST="$HOME/config/dist" DIR_LINKS="$HOME/config/links" DIR_BACKUPS="$HOME/config/backup" EDITED_FILES="$HOME/config/edited_files" for OneDir in $CONFIG_DIR $DIR_DIST $DIR_BACKUPS $DIR_LINKS ; do if [ ! -d $OneDir ]; then mkdir $OneDir chmod 711 $OneDir fi done if [ ! -f $EDITED_FILES ]; then touch $EDITED_FILES chmod 600 $EDITED_FILES fi if [ -z "$RealEditor" ]; then for TryEditor in `type -path mcedit || :` `type -path pico || :` `type -path vi || :` `type -path jove || :` `type -path nedit || :` `type -path emacs || :` ; do if [ -z "$RealEditor" ]; then case "$TryEditor" in *pico) RealEditor="$TryEditor -w" ;; *mcedit) RealEditor="$TryEditor -c" ;; *) RealEditor="$TryEditor" ;; esac echo Editor default of $RealEditor taken. If you prefer another editor, please add echo export RealEditor=myeditorname echo to ~/.bash_profile fi done unset TryEditor || : if [ -z "$RealEditor" ]; then echo No editor was specified by the RealEditor= variable and this echo script was unable to find mcedit, pico, vi, jove, nedit or echo emacs on your system. Please put echo export RealEditor=the_name_of_an_editor echo in ~/.bash_profile or your environment and re-start. Exiting. exit 4 fi #no suitable editor found fi if [ -z "$1" ]; then echo "hack: filename is missing" ; exit 2 ; fi if [ -d "$1" ]; then echo "hack: filename is a directory" ; exit 3 ; fi BaseName="`basename $1`" FilePath="`( cd $(dirname $1) ; pwd )`" echo "Working with \"$FilePath\"/\"$BaseName\"" if [ -z "$FilePath" ] || [ -z "$BaseName" ]; then echo Missing filepath or filename, exiting. exit 1 fi if [ ! -f "$1" ]; then #FIXME - handle wildcards, perhaps? echo "hack: No file by that name. Shall I create one for you? (y/N)" read CreateOne case "$CreateOne" in y*|Y*) touch "$1" ;; *) echo "Exiting." exit 3 ;; esac fi #Debugging sanity check if [ ! "$1" -ef "$FilePath/$BaseName" ]; then echo "Fatal: $1 and "$FilePath/$BaseName are not identical files, exiting. exit 1 fi #Don't use $1 from here on. if [ -f "$DIR_DIST/$BaseName.dist" ]; then echo "hack: $DIR_DIST/$BaseName.dist already exists" else cp -p "$FilePath/$BaseName" "$DIR_DIST/$BaseName.dist" echo "hack: creating file $DIR_DIST/$BaseName.dist" fi #FIXME - relative paths? if [ -L "$DIR_LINKS/$BaseName" ]; then if [ ! -e "$DIR_LINKS/$BaseName" ]; then echo "$DIR_LINKS/$BaseName is not pointing at any file, shall I replace it? (y/N)" read ReplaceIt case "$ReplaceIt" in y*|Y*) rm -rf $DIR_LINKS/$BaseName ln -s "$FilePath/$BaseName" "$DIR_LINKS/$BaseName" echo "hack: linking $FilePath/$BaseName" ;; *) echo Not replacing. esac fi else ln -s "$FilePath/$BaseName" "$DIR_LINKS/$BaseName" echo "hack: linking $FilePath/$BaseName" fi echo "$FilePath/$BaseName" >> $EDITED_FILES if [ `ls -al "$FilePath/$BaseName" | awk '{print $2}'` -gt 1 ]; then echo "$FilePath/$BaseName is hardlinked, copying to break link." if type -path mktemp >/dev/null 2>/dev/null ; then notempfile () { #Handle the case where the temp file can't be created for some reason. echo Couldn\'t create $1 temp file, exiting. exit 1 } TEMPFILE=`mktemp -q /tmp/hack.XXXXXX` || notempfile hack else echo No mktemp on this system, using insecure method for temp file. TEMPFILE=/tmp/hack.$$.$RANDOM rm -f $TEMPFILE fi cp -p "$FilePath/$BaseName" $TEMPFILE rm -f "$FilePath/$BaseName" mv $TEMPFILE "$FilePath/$BaseName" else echo "$FilePath/$BaseName" has only a single link, good. fi cp --backup=numbered "$FilePath/$BaseName" "$DIR_BACKUPS/$BaseName.`date +%y%m%d`" echo "hack: creating dated backup $BaseName.`date +%y%m%d`" rm -f $EDITED_FILES.tmp cp $EDITED_FILES $EDITED_FILES.tmp cat $EDITED_FILES.tmp | sort | uniq > $EDITED_FILES rm -f $EDITED_FILES.tmp if [ "$TERM" = "xterm" -o "$TERM" = "xterm-color" ]; then echo -ne "\033]0;hack ${BaseName}\007" fi #Many thanks to Johannes Ullrich for the rcs tutorial. if [ -f "$FilePath/$BaseName,v" ] || [ -f "$FilePath/RCS/$BaseName,v" ]; then #FIXME - testme if [ -w "$FilePath/$BaseName" ]; then echo "Warning: RCS-enabled $FilePath/$BaseName is writeable. Continue? (y/N)" read Continue case "$Continue" in y*|Y*) : ;; *) echo Exiting exit 5 ;; esac fi echo Checking out and locking "$FilePath/$BaseName" file in rcs. co -l "$FilePath/$BaseName" fi #FIXME - case for *hack* ;-) $RealEditor "$FilePath/$BaseName" if [ -f "$FilePath/$BaseName,v" ] || [ -f "$FilePath/RCS/$BaseName,v" ]; then echo Checking in and unlocking "$FilePath/$BaseName" file in rcs. ci -u "$FilePath/$BaseName" fi if [ "$TERM" = "xterm" -o "$TERM" = "xterm-color" ]; then if [ -n "$XTITLE" ]; then echo -ne "\033]0;${XTITLE}\007" ; else echo -ne "\033]0;${USER}@${SHOSTNAME} ${PWD}\007" | sed -e "s@${HOME}@\~@" fi fi exit #FIXME $1 #Test is too crude, this sees itself. ;-( #if ps ax | grep "[h]ack $1" >/dev/null 2>/dev/null ; then # echo "It appears someone is already editing $1:" # ps ax | grep "[h]ack $1" # echo "Continue? (y/N)" # read GoAnyways # case "$GoAnyways" in # y*|Y*) # : # ;; # *) # echo Exiting. # exit 1 # ;; # esac ##else ## echo "It does not appear anyone is editing $1, good." #fi