summaryrefslogtreecommitdiff
path: root/functions
blob: 2a7cfa3e040be034896c25df73b9157cf943d968 (plain)
  1. #!/bin/sh
  2. # Comments on coding style:
  3. # * shift mandatory function arguments: Provokes error if missing
  4. # * quote variables: We want to support oddities like spaces in paths
  5. function preserveolderfile() {
  6. ## DESC: Preserve copy of file, unless one exists already
  7. # File to tweak
  8. file="$1"; shift
  9. # Extension used for new backup
  10. newext="$1"
  11. # Space-delimited list of possible extensions, default last
  12. extensions="orig old"
  13. for ext in $extensions; do
  14. backupfile="$file.$ext"
  15. [ "$ext" = "$newext" ] && newext_valid="yes"
  16. if [ -f "$backupfile" ]; then
  17. [ -n "$DEBUG" ] && echo "DEBUG: Backup file \"$backupfile\" not found - continuing." >&2
  18. continue
  19. elif [ -e "$backupfile" ]; then
  20. echo "ERROR: Backup file \"$backupfile\" is not a regular file." >&2
  21. return 1
  22. else
  23. [ -n "$DEBUG" ] && echo "DEBUG: Backup file \"$backupfile\" found - exit silently." >&2
  24. return 0
  25. fi
  26. done
  27. if [ -n "$newext" ];then
  28. if [ "$newext_valid" = "yes" ]; then
  29. backupfile="$file.$newext"
  30. else
  31. echo "WARNING: Backup extension \"$newext\" is invalid - using default instead" >&2
  32. fi
  33. fi
  34. if [ -f "$file" ]; then
  35. cp -p "file" "$backupfile"
  36. elif [ -e "$file" ]; then
  37. echo "ERROR: Backup of file \"$file\" failed - not a regular file." >&2
  38. return 1
  39. else
  40. touch "$backupfile"
  41. fi
  42. return 0
  43. }
  44. function enableoraddlines() {
  45. ## DESC: Add lines to file, or replace if similar exist already
  46. # File to tweak
  47. file="$1"; shift
  48. # Word number in line to search for and replace if found
  49. wordno="$1"; shift
  50. [ -e "$file" ] || touch "$file"
  51. for line; do
  52. [ -z "$line" ] && continue
  53. firstword=$(echo "$line" | awk "{print \$$wordno}")
  54. # FIXME: Why doesn't it work to print ARGVOUT in END block?
  55. linemissing=$(perl -i -pe "END { print \"$line\\n\" unless \$seen } s¡^[#;\s]*($firstword)\s.*¡$line\\n¡ and \$seen++" "$file")
  56. #"This stray quote is only to please buggy mc (cooledit) code hiliting
  57. [ -n "$linemissing" ] && echo $linemissing >> "$file"
  58. done
  59. # FIXME: Figure out what exits non-zero above
  60. return 0
  61. }
  62. function preserveandaddlines() {
  63. ## DESC: Backup older file and add lines to it
  64. # File to tweak
  65. file="$1"; shift
  66. # Extension of backup file
  67. ext="$1"; shift
  68. # Word number in line to search for and replace if found
  69. wordno="$1"; shift
  70. preserveolderfile "$file" "$ext"
  71. # enableoraddlines "$file" "$wordno" $@
  72. # FIXME: Somehow avoid expansion, to avoid having to duplicate the whole enableoraddlines()
  73. [ -e "$file" ] || touch "$file"
  74. for line; do
  75. [ -z "$line" ] && continue
  76. firstword=$(echo "$line" | awk "{print \$$wordno}")
  77. # FIXME: Why doesn't it work to print ARGVOUT in END block?
  78. linemissing=$(perl -i -pe "END { print \"$line\\n\" unless \$seen } s¡^[#;\s]*($firstword)\s.*¡$line\\n¡ and \$seen++" "$file")
  79. #"This stray quote is only to please buggy mc (cooledit) code hiliting
  80. [ -n "$linemissing" ] && echo $linemissing >> "$file"
  81. done
  82. # FIXME: Figure out what exits non-zero above
  83. return 0
  84. }
  85. function preserveandhashdisablelines() {
  86. ## DESC: Backup older file and disable lines in it
  87. # File to tweak
  88. file="$1"; shift
  89. # Extension of backup file
  90. ext="$1"; shift
  91. # Word number in line to search for and replace if found
  92. wordno="$1"; shift
  93. preserveolderfile "$file" "$ext"
  94. # disablelines "$file" "$wordno" "#" $@
  95. # FIXME: Somehow avoid expansion, to make possibe a separate disablelines()
  96. [ -e "$file" ] || touch "$file"
  97. for line; do
  98. [ -z "$line" ] && continue
  99. firstword=$(echo "$line" | awk "{print \$$wordno}")
  100. # FIXME: Why doesn't it work to print ARGVOUT in END block?
  101. linemissing=$(perl -i -pe "END { print \"$line\\n\" unless \$seen } s¡^(\s*$firstword)\s.*¡#$line\\n¡ and \$seen++" "$file")
  102. #"This stray quote is only to please buggy mc (cooledit) code hiliting
  103. [ -n "$linemissing" ] && echo >2 "Line \"$linemissing\" not found to disable in $file"
  104. done
  105. # FIXME: Figure out what exits non-zero above
  106. return 0
  107. }
  108. function perlrelines() {
  109. ## DESC: Apply perl regex to lines of a single file
  110. # File to tweak
  111. file=$1; shift
  112. backupfile="`savebackupfile "$file"`"
  113. for regexp; do
  114. perl -pi -e "$regexp" "$file"
  115. done
  116. cleanbackupfile "$file" "$backupfile"
  117. # FIXME: Figure out what exits non-zero above
  118. return 0
  119. }
  120. function mkgrubdevmap() {
  121. ## DESC: Spit out GRUB config used for /boot/grub/devices.map
  122. device="$1"; shift
  123. cat <<EOF
  124. (fd0) /dev/fd0
  125. (hd0) $device
  126. EOF
  127. }
  128. function addaddons() {
  129. context="$1"; shift
  130. addon_context="$1"; shift
  131. for addon in $@; do
  132. context_here="$(eval echo \$\{${addon_context}_$addon\})"
  133. if [ -n "$context_here" ]; then
  134. eval ${context}=\"\$${context} \$context_here\"
  135. else
  136. echo "Warning: Variable \"${addon_context}_${addon}\" empty or undefined!"
  137. fi
  138. done
  139. }