From f3697306960d5175829a155eb83990927ef7b1fe Mon Sep 17 00:00:00 2001 From: jonas Date: Sat, 14 Oct 2006 09:51:31 +0000 Subject: Move config loading to new file common-settings, and rename file functions to commmon-functions. git-svn-id: svn+ssh://xayide/home/jonas/private_svn/fleshybrid/homebase@34 8f53b18a-e215-0410-8885-9f593d34873e --- common-functions | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 common-functions (limited to 'common-functions') diff --git a/common-functions b/common-functions new file mode 100644 index 0000000..58536cf --- /dev/null +++ b/common-functions @@ -0,0 +1,151 @@ +#!/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 + # FIXME: This is broken - wordno unconditionally becomes firstword later on + 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 + # FIXME: This is broken - wordno unconditionally becomes firstword later on + 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 preserveandhashdisablelines() { + ## DESC: Backup older file and disable lines in 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 + # FIXME: This is broken - wordno unconditionally becomes firstword later on + wordno="$1"; shift + preserveolderfile "$file" "$ext" +# disablelines "$file" "$wordno" "#" $@ + # FIXME: Somehow avoid expansion, to make possibe a separate disablelines() + [ -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 >2 "Line \"$linemissing\" not found to disable in $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 <