summaryrefslogtreecommitdiff
path: root/functions
blob: 58536cf38dd386e7634b1e905788272773de2071 (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. # FIXME: This is broken - wordno unconditionally becomes firstword later on
  50. wordno="$1"; shift
  51. [ -e "$file" ] || touch "$file"
  52. for line; do
  53. [ -z "$line" ] && continue
  54. firstword=$(echo "$line" | awk "{print \$$wordno}")
  55. # FIXME: Why doesn't it work to print ARGVOUT in END block?
  56. linemissing=$(perl -i -pe "END { print \"$line\\n\" unless \$seen } s¡^[#;\s]*($firstword)\s.*¡$line\\n¡ and \$seen++" "$file")
  57. #"This stray quote is only to please buggy mc (cooledit) code hiliting
  58. [ -n "$linemissing" ] && echo $linemissing >> "$file"
  59. done
  60. # FIXME: Figure out what exits non-zero above
  61. return 0
  62. }
  63. function preserveandaddlines() {
  64. ## DESC: Backup older file and add lines to it
  65. # File to tweak
  66. file="$1"; shift
  67. # Extension of backup file
  68. ext="$1"; shift
  69. # Word number in line to search for and replace if found
  70. # FIXME: This is broken - wordno unconditionally becomes firstword later on
  71. wordno="$1"; shift
  72. preserveolderfile "$file" "$ext"
  73. # enableoraddlines "$file" "$wordno" $@
  74. # FIXME: Somehow avoid expansion, to avoid having to duplicate the whole enableoraddlines()
  75. [ -e "$file" ] || touch "$file"
  76. for line; do
  77. [ -z "$line" ] && continue
  78. firstword=$(echo "$line" | awk "{print \$$wordno}")
  79. # FIXME: Why doesn't it work to print ARGVOUT in END block?
  80. linemissing=$(perl -i -pe "END { print \"$line\\n\" unless \$seen } s¡^[#;\s]*($firstword)\s.*¡$line\\n¡ and \$seen++" "$file")
  81. #"This stray quote is only to please buggy mc (cooledit) code hiliting
  82. [ -n "$linemissing" ] && echo $linemissing >> "$file"
  83. done
  84. # FIXME: Figure out what exits non-zero above
  85. return 0
  86. }
  87. function preserveandhashdisablelines() {
  88. ## DESC: Backup older file and disable lines in it
  89. # File to tweak
  90. file="$1"; shift
  91. # Extension of backup file
  92. ext="$1"; shift
  93. # Word number in line to search for and replace if found
  94. # FIXME: This is broken - wordno unconditionally becomes firstword later on
  95. wordno="$1"; shift
  96. preserveolderfile "$file" "$ext"
  97. # disablelines "$file" "$wordno" "#" $@
  98. # FIXME: Somehow avoid expansion, to make possibe a separate disablelines()
  99. [ -e "$file" ] || touch "$file"
  100. for line; do
  101. [ -z "$line" ] && continue
  102. firstword=$(echo "$line" | awk "{print \$$wordno}")
  103. # FIXME: Why doesn't it work to print ARGVOUT in END block?
  104. linemissing=$(perl -i -pe "END { print \"$line\\n\" unless \$seen } s¡^(\s*$firstword)\s.*¡#$line\\n¡ and \$seen++" "$file")
  105. #"This stray quote is only to please buggy mc (cooledit) code hiliting
  106. [ -n "$linemissing" ] && echo >2 "Line \"$linemissing\" not found to disable in $file"
  107. done
  108. # FIXME: Figure out what exits non-zero above
  109. return 0
  110. }
  111. function perlrelines() {
  112. ## DESC: Apply perl regex to lines of a single file
  113. # File to tweak
  114. file=$1; shift
  115. backupfile="`savebackupfile "$file"`"
  116. for regexp; do
  117. perl -pi -e "$regexp" "$file"
  118. done
  119. cleanbackupfile "$file" "$backupfile"
  120. # FIXME: Figure out what exits non-zero above
  121. return 0
  122. }
  123. function mkgrubdevmap() {
  124. ## DESC: Spit out GRUB config used for /boot/grub/devices.map
  125. device="$1"; shift
  126. cat <<EOF
  127. (fd0) /dev/fd0
  128. (hd0) $device
  129. EOF
  130. }
  131. function addaddons() {
  132. context="$1"; shift
  133. addon_context="$1"; shift
  134. for addon in $@; do
  135. context_here="$(eval echo \$\{${addon_context}_$addon\})"
  136. if [ -n "$context_here" ]; then
  137. eval ${context}=\"\$${context} \$context_here\"
  138. else
  139. echo "Warning: Variable \"${addon_context}_${addon}\" empty or undefined!"
  140. fi
  141. done
  142. }