diff options
Diffstat (limited to 'functions')
-rw-r--r-- | functions | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/functions b/functions new file mode 100644 index 0000000..7420f31 --- /dev/null +++ b/functions @@ -0,0 +1,125 @@ +#!/bin/sh + +# Comments on coding style: +# * shift mandatory function arguments: Provokes error if missing +# * quote variables: We want to support oddities like spaces in paths + +function preserveolderfile() { + ## DESC: Preserve copy of file, unless one exists already + # File to tweak + file="$1"; shift + # Extension used for new backup + newext="$1" + # Space-delimited list of possible extensions, default last + extensions="orig old" + for ext in $extensions; do + backupfile="$file.$ext" + [ "$ext" = "$newext" ] && newext_valid="yes" + if [ -f "$backupfile" ]; then + [ -n "$DEBUG" ] && echo "DEBUG: Backup file \"$backupfile\" not found - continuing." >&2 + continue + elif [ -e "$backupfile" ]; then + echo "ERROR: Backup file \"$backupfile\" is not a regular file." >&2 + return 1 + else + [ -n "$DEBUG" ] && echo "DEBUG: Backup file \"$backupfile\" found - exit silently." >&2 + return 0 + fi + done + if [ -n "$newext" ];then + if [ "$newext_valid" = "yes" ]; then + backupfile="$file.$newext" + else + echo "WARNING: Backup extension \"$newext\" is invalid - using default instead" >&2 + fi + fi + if [ -f "$file" ]; then + cp -p "file" "$backupfile" + elif [ -e "$file" ]; then + echo "ERROR: Backup of file \"$file\" failed - not a regular file." >&2 + return 1 + else + touch "$backupfile" + fi + return 0 +} + +function enableoraddlines() { + ## DESC: Add lines to file, or replace if similar exist already + # File to tweak + file="$1"; shift + # Word number in line to search for and replace if found + wordno="$1"; shift + [ -e "$file" ] || touch "$file" + for line; do + [ -z "$line" ] && continue + firstword=$(echo "$line" | awk "{print \$$wordno}") + # FIXME: Why doesn't it work to print ARGVOUT in END block? + linemissing=$(perl -i -pe "END { print \"$line\\n\" unless \$seen } s¡^[#;\s]*($firstword)\s.*¡$line\\n¡ and \$seen++" "$file") +#"This stray quote is only to please buggy mc (cooledit) code hiliting + [ -n "$linemissing" ] && echo $linemissing >> "$file" + done + # FIXME: Figure out what exits non-zero above + return 0 +} + +function preserveandaddlines() { + ## DESC: Backup older file and add lines to it + # File to tweak + file="$1"; shift + # Extension of backup file + ext="$1"; shift + # Word number in line to search for and replace if found + wordno="$1"; shift + preserveolderfile "$file" "$ext" +# enableoraddlines "$file" "$wordno" $@ + # FIXME: Somehow avoid expansion, to avoid having to duplicate the whole enableoraddlines() + [ -e "$file" ] || touch "$file" + for line; do + [ -z "$line" ] && continue + firstword=$(echo "$line" | awk "{print \$$wordno}") + # FIXME: Why doesn't it work to print ARGVOUT in END block? + linemissing=$(perl -i -pe "END { print \"$line\\n\" unless \$seen } s¡^[#;\s]*($firstword)\s.*¡$line\\n¡ and \$seen++" "$file") +#"This stray quote is only to please buggy mc (cooledit) code hiliting + [ -n "$linemissing" ] && echo $linemissing >> "$file" + done + # FIXME: Figure out what exits non-zero above + return 0 +} + +function perlrelines() { + ## DESC: Apply perl regex to lines of a single file + # File to tweak + file=$1; shift + backupfile="`savebackupfile "$file"`" + for regexp; do + perl -pi -e "$regexp" "$file" + done + cleanbackupfile "$file" "$backupfile" + + # FIXME: Figure out what exits non-zero above + return 0 +} + +function mkgrubdevmap() { + ## DESC: Spit out GRUB config used for /boot/grub/devices.map + device="$1"; shift + cat <<EOF +(fd0) /dev/fd0 +(hd0) $device +EOF +} + +function addaddons() { + set -x + context="$1"; shift + addon_context="$1"; shift + for addon in $@; do + context_here="$(eval echo \$\{${addon_context}_$addon\})" + if [ -n "$context_here" ]; then + eval ${context}=\"\$${context} \$context_here\" + else + echo "Warning: Variable \"${addon_context}_${addon}\" empty or undefined!" + fi +done +} |