libdeblob.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. grep -v "$1" "$SRC_DIR/$2" > "$PATCH_DIR/$file_ft"
  78. diff "$SRC_DIR/$2" "$PATCH_DIR/$file_ft" > "$PATCH_DIR/$file_ft.patch"
  79. fi
  80. }
  81. # "Copies" a dir
  82. # Usage: dircp $file $dest
  83. dircp() {
  84. if echo "$1" | grep -q "^files/"
  85. then
  86. cp -r "$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  87. else
  88. cp -r "$SRC_DIR/$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  89. fi
  90. }
  91. # "Copies" a file
  92. # Usage: filecp $file $dest
  93. filecp() {
  94. if echo "$1" | grep -q "^files/"
  95. then
  96. cp "$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  97. else
  98. cp "$SRC_DIR/$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  99. fi
  100. }
  101. # "Deletes" a file
  102. # Usage: filedel $file
  103. filedel() {
  104. echo "$PATCH_DIR $1"
  105. touch "$PATCH_DIR/RM_$(filetize "$1")"
  106. }
  107. # Applies patches.
  108. apply() {
  109. local file
  110. for file in "$PATCH_DIR"/*
  111. do
  112. local realname_prefixed="$(unfiletize "$(basename "$file")")"
  113. if echo "$realname_prefixed" | grep -q "^RM_"
  114. then
  115. realname="${realname_prefixed#RM_}"
  116. echo "Deleting $realname..."
  117. if rm -rf "$SRC_DIR/$realname"
  118. then
  119. echo "$realname deleted" >> apply.log
  120. echo "$realname deleted"
  121. else
  122. echo "!!! $realname NOT deleted" >> apply.log
  123. echo "!!! $realname NOT deleted"
  124. fi
  125. elif echo "$file" | grep -q "ADD_"
  126. then
  127. realname="${realname_prefixed#ADD_}"
  128. echo "Copying $file to $realname..."
  129. if cp -r "$file" "$SRC_DIR/$realname"
  130. then
  131. echo "$realname copied from $file" >> apply.log
  132. else
  133. echo "!!! $realname NOT copied from $file" >> apply.log
  134. fi
  135. elif echo "$file" | grep -q ".patch$"
  136. then
  137. realname="$realname_prefixed"
  138. if patch "$SRC_DIR/${realname%.patch}" < "$file"
  139. then
  140. echo "$SRC_DIR/${realname%.patch} patched from $file" >> apply.log
  141. else
  142. echo "!!! $SRC_DIR/${realname%.patch} NOT patched from $file" \
  143. >> apply.log
  144. fi
  145. fi
  146. done
  147. }
  148. self_destruct_sequence() {
  149. echo "$1 will be deleted in three seconds."
  150. echo "CTRL-C now to avoid this fate!"
  151. echo "3"; sleep 1
  152. echo "2"; sleep 1
  153. echo "1"; sleep 1
  154. printf "0\nDestruction!"
  155. rm -rf "$1"
  156. }