From b6d85b04286d30498a11aaac36e75032b1e72801 Mon Sep 17 00:00:00 2001 From: jonas Date: Sat, 10 Jun 2006 18:18:37 +0000 Subject: Added all work so far... git-svn-id: svn+ssh://xayide/home/jonas/private_svn/fleshybrid/trunk@2 8f53b18a-e215-0410-8885-9f593d34873e --- functions | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 functions (limited to 'functions') 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 <