libdeblob.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/ksh
  2. #########################
  3. # Name: libdeblob.sh
  4. # Main: jadedctrl
  5. # Lisc: ISC
  6. # Desc: Functions to be
  7. # used for deblobbing
  8. # and rebranding OBSD
  9. # sources for the LBSD
  10. # project.
  11. #########################
  12. # Turns a file and it's path into a friendly filename
  13. # Usage: filetize $filepath
  14. filetize() {
  15. echo "$1" | sed 's|/|\^|g'
  16. }
  17. # Vice-versa, clearly.
  18. # Usage: unfiletize $filetizedpath
  19. unfiletize() {
  20. echo "$1" | sed 's|\^|/|g'
  21. }
  22. # Prints $1 number of spaces.
  23. # Usage: space $number
  24. space() {
  25. typeset -i i=0
  26. while [ $i != $1 ]
  27. do
  28. printf " "
  29. i=i+1
  30. done
  31. }
  32. # Replace a string in a file
  33. # Usage: rep $replacee $replacer $file
  34. rep() {
  35. local file_ft="$(filetize "$3")"
  36. if [ -e "$PATCH_DIR/$file_ft" ]
  37. then
  38. sed 's^'"$1"'^'"$2"'^g' "$PATCH_DIR/$file_ft" > "$PATCH_DIR/$file_ft.tmp"
  39. mv "$PATCH_DIR/$file_ft.tmp" "$PATCH_DIR/$file_ft"
  40. diff "$SRC_DIR/$3" "$PATCH_DIR/$file_ft" > "$PATCH_DIR/$file_ft.patch"
  41. else
  42. sed 's^'"$1"'^'"$2"'^g' "$SRC_DIR/$3" > "$PATCH_DIR/$file_ft"
  43. diff "$SRC_DIR/$3" "$PATCH_DIR/$file_ft" > "$PATCH_DIR/$file_ft.patch"
  44. fi
  45. }
  46. # Delete a string in a file
  47. # Usage: strdel $string $file
  48. strdel() {
  49. rep "$1" "" $2
  50. }
  51. # Inserts a new line after another
  52. # Usage: lineadd $string $newline $file
  53. lineadd() {
  54. local file_ft="$(filetize "$3")"
  55. if [ -e "$PATCH_DIR/$file_ft" ]
  56. then
  57. sed 's^'"$1"'^'"$1"' \
  58. '"$2"'^' "$PATCH_DIR/$file_ft" > "$PATCH_DIR/$file_ft.tmp"
  59. mv "$PATCH_DIR/$file_ft.tmp" "$PATCH_DIR/$file_ft"
  60. diff "$SRC_DIR/$3" "$PATCH_DIR/$file_ft" > "$PATCH_DIR/$file_ft.patch"
  61. else
  62. sed 's^'"$1"'^'"$1"' \
  63. '"$2"'^' "$SRC_DIR/$3" > "$PATCH_DIR/$file_ft"
  64. diff "$SRC_DIR/$3" "$PATCH_DIR/$file_ft" > "$PATCH_DIR/$file_ft.patch"
  65. fi
  66. }
  67. # Removes a line.
  68. # Usage linedel $string $file
  69. linedel() {
  70. local file_ft="$(filetize "$2")"
  71. if [ -e "$PATCH_DIR/$file_ft" ]
  72. then
  73. grep -v "$1" "$PATCH_DIR/$file_ft" > "$PATCH_DIR/$file_ft.tmp"
  74. mv "$PATCH_DIR/$file_ft.tmp" "$PATCH_DIR/$file_ft"
  75. diff "$SRC_DIR/$2" "$PATCH_DIR/$file_ft" > "$PATCH_DIR/$file_ft.patch"
  76. else
  77. echo otherwise
  78. grep -v "$1" "$SRC_DIR/$2" > "$PATCH_DIR/$file_ft"
  79. diff "$SRC_DIR/$2" "$PATCH_DIR/$file_ft" > "$PATCH_DIR/$file_ft.patch"
  80. echo otherhell
  81. fi
  82. }
  83. # "Copies" a dir
  84. # Usage: dircp $file $dest
  85. dircp() {
  86. if echo "$1" | grep -q "^files/"
  87. then
  88. echo "FILES"
  89. cp -r "$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  90. else
  91. echo "NO FILES"
  92. cp -r "$SRC_DIR/$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  93. fi
  94. }
  95. # "Copies" a file
  96. # Usage: filecp $file $dest
  97. filecp() {
  98. if echo "$1" | grep -q "^files/"
  99. then
  100. echo "FILES"
  101. cp "$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  102. else
  103. echo "FILES"
  104. cp "$SRC_DIR/$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  105. fi
  106. }
  107. # "Deletes" a file
  108. # Usage: filedel $file
  109. filedel() {
  110. echo "$PATCH_DIR $1"
  111. touch "$PATCH_DIR/RM_$(filetize "$1")"
  112. }
  113. # Applies patches.
  114. apply() {
  115. local file
  116. for file in "$PATCH_DIR"/*
  117. do
  118. local realname_prefixed="$(unfiletize "$(basename "$file")")"
  119. if echo "$realname_prefixed" | grep -q "^RM_"
  120. then
  121. realname="${realname_prefixed#RM_}"
  122. echo "Deleting $realname..."
  123. if rm -rf "$SRC_DIR/$realname"
  124. then
  125. echo "$realname deleted" >> apply.log
  126. echo "$realname deleted"
  127. else
  128. echo "!!! $realname NOT deleted" >> apply.log
  129. echo "!!! $realname NOT deleted"
  130. fi
  131. elif echo "$file" | grep -q "ADD_"
  132. then
  133. realname="${realname_prefixed#ADD_}"
  134. echo "Copying $file to $realname..."
  135. if cp -r "$file" "$SRC_DIR/$realname"
  136. then
  137. echo "$realname copied from $file" >> apply.log
  138. else
  139. echo "!!! $realname NOT copied from $file" >> apply.log
  140. fi
  141. elif echo "$file" | grep -q ".patch$"
  142. then
  143. realname="$realname_prefixed"
  144. if patch "$SRC_DIR/${realname%.patch}" < "$file"
  145. then
  146. echo "$SRC_DIR/${realname%.patch} patched from $file" >> apply.log
  147. else
  148. echo "!!! $SRC_DIR/${realname%.patch} NOT patched from $file" \
  149. >> apply.log
  150. fi
  151. fi
  152. done
  153. }
  154. self_destruct_sequence() {
  155. echo "$1 will be deleted in three seconds."
  156. echo "CTRL-C now to avoid this fate!"
  157. echo "3"; sleep 1
  158. echo "2"; sleep 1
  159. echo "1"; sleep 1
  160. printf "0\nDestruction!"
  161. rm -rf "$1"
  162. }