summaryrefslogtreecommitdiff
path: root/functions
blob: a94325502ad69c4127eb2d74e048b780ab51de0e (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 perlrelines() {
  86. ## DESC: Apply perl regex to lines of a single file
  87. # File to tweak
  88. file=$1; shift
  89. backupfile="`savebackupfile "$file"`"
  90. for regexp; do
  91. perl -pi -e "$regexp" "$file"
  92. done
  93. cleanbackupfile "$file" "$backupfile"
  94. # FIXME: Figure out what exits non-zero above
  95. return 0
  96. }
  97. function mkgrubdevmap() {
  98. ## DESC: Spit out GRUB config used for /boot/grub/devices.map
  99. device="$1"; shift
  100. cat <<EOF
  101. (fd0) /dev/fd0
  102. (hd0) $device
  103. EOF
  104. }
  105. function addaddons() {
  106. context="$1"; shift
  107. addon_context="$1"; shift
  108. for addon in $@; do
  109. context_here="$(eval echo \$\{${addon_context}_$addon\})"
  110. if [ -n "$context_here" ]; then
  111. eval ${context}=\"\$${context} \$context_here\"
  112. else
  113. echo "Warning: Variable \"${addon_context}_${addon}\" empty or undefined!"
  114. fi
  115. done
  116. }