libdeblob.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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: defiletize $filetizedpath
  19. unfiletize() {
  20. echo $1 | sed 's|\^|/|g'
  21. }
  22. # Prints $1 number of spaces.
  23. # Usage: space $number
  24. space() {
  25. 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. if [ -e "$PATCH_DIR/$(filetize "$3")" ]
  36. then
  37. sed 's^'"$1"'^'"$2"'^g' $PATCH_DIR/$(filetize "$3") > $PATCH_DIR/$(filetize "$3").tmp
  38. mv $PATCH_DIR/$(filetize "$3").tmp $PATCH_DIR/$(filetize "$3")
  39. diff ${SRC_DIR}/$3 $PATCH_DIR/$(filetize "$3") > $PATCH_DIR/$(filetize "$3").patch
  40. else
  41. sed 's^'"$1"'^'"$2"'^g' ${SRC_DIR}/${3} > $PATCH_DIR/$(filetize "$3")
  42. diff ${SRC_DIR}/$3 $PATCH_DIR/$(filetize "$3") > $PATCH_DIR/$(filetize "$3").patch
  43. fi
  44. }
  45. # Delete a string in a file
  46. # Usage: strdel $string $file
  47. strdel() {
  48. rep "$1" "" $2
  49. }
  50. # Inserts a new line after another
  51. # Usage: lineadd $string $newline $file
  52. lineadd() {
  53. if [ -e "$PATCH_DIR/$(filetize "$3")" ]
  54. then
  55. sed 's^'"$1"'^'"$1"' \
  56. '"$2"'^' $PATCH_DIR/$(filetize "$3") > $PATCH_DIR/$(filetize "$3").tmp
  57. mv $PATCH_DIR/$(filetize "$3").tmp $PATCH_DIR/$(filetize "$3")
  58. diff ${SRC_DIR}/$3 $PATCH_DIR/$(filetize "$3") > $PATCH_DIR/$(filetize "$3").patch
  59. else
  60. sed 's^'"$1"'^'"$1"' \
  61. '"$2"'^' ${SRC_DIR}/${3} > $PATCH_DIR/$(filetize "$3")
  62. diff ${SRC_DIR}/$3 $PATCH_DIR/$(filetize "$3") > $PATCH_DIR/$(filetize "$3").patch
  63. fi
  64. }
  65. # Removes a line.
  66. # Usage linedel $string $file
  67. linedel() {
  68. if [ -e "$PATCH_DIR/$(filetize "$2")" ]
  69. then
  70. grep -v "$1" $PATCH_DIR/$(filetize "$2") > $PATCH_DIR/$(filetize "$2").tmp
  71. mv $PATCH_DIR/$(filetize "$2").tmp $PATCH_DIR/$(filetize "$2")
  72. diff ${SRC_DIR}/$2 $PATCH_DIR/$(filetize "$2") > $PATCH_DIR/$(filetize "$2").patch
  73. else
  74. echo otherwise
  75. grep -v "$1" "${SRC_DIR}/$2" > $PATCH_DIR/$(filetize "$2")
  76. diff ${SRC_DIR}/$2 $PATCH_DIR/$(filetize "$2") > $PATCH_DIR/$(filetize "$2").patch
  77. echo otherhell
  78. fi
  79. }
  80. # "Copies" a dir
  81. # Usage: dircp $file $dest
  82. dircp() {
  83. if echo $1 | grep "^files/"
  84. then
  85. echo "FILES"
  86. cp -r "$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  87. else
  88. echo "NO FILES"
  89. cp -r "${SRC_DIR}/$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  90. fi
  91. }
  92. # "Copies" a file
  93. # Usage: filedel $file $dest
  94. filecp() {
  95. if echo $1 | grep "^files/"
  96. then
  97. echo "FILES"
  98. cp "$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  99. else
  100. echo "FILES"
  101. cp "${SRC_DIR}/$1" "$PATCH_DIR/ADD_$(filetize "$2")"
  102. fi
  103. }
  104. # "Deletes" a file
  105. # Usage: filedel $file
  106. filedel() {
  107. echo $PATCH_DIR $1
  108. touch $PATCH_DIR/RM_$(filetize $1)
  109. }
  110. # Applies patches.
  111. apply() {
  112. for file in $PATCH_DIR/*
  113. do
  114. realname=$(echo $file | sed 's^.*/^^' | sed 's/ADD_//' | sed 's/RM_//')
  115. realname="$(unfiletize "$realname")"
  116. if echo "$file" | grep "RM_" > /dev/null
  117. then
  118. realname=$(echo "$realname" | sed 's/RM_//')
  119. echo "Deleting $realname..."
  120. if rm -rf ${SRC_DIR}/$realname
  121. then
  122. echo "$realname deleted" >> apply.log
  123. echo "$realname deleted"
  124. else
  125. echo "!!! $realname NOT deleted" >> apply.log
  126. echo "!!! $realname NOT deleted"
  127. fi
  128. elif echo "$file" | grep "ADD_" > /dev/null
  129. then
  130. realname=$(echo "$realname" | sed 's/ADD_//')
  131. echo "Copying $file to $realname..."
  132. if cp -r $file ${SRC_DIR}/$realname
  133. then
  134. echo "$realname copied from $file" >> apply.log
  135. else
  136. echo "!!! $realname NOT copied from $file" >> apply.log
  137. fi
  138. elif echo "$file" | grep ".patch$" > /dev/null
  139. then
  140. if patch "${SRC_DIR}/$(echo $realname | sed 's/\.patch//')" < $file
  141. then
  142. echo "${SRC_DIR}/$(echo $realname | sed 's/\.patch//') patched from $file" >> apply.log
  143. else
  144. echo "!!! ${SRC_DIR}/$(echo $realname | sed 's/\.patch//') NOT patched from $file" >> apply.log
  145. fi
  146. fi
  147. done
  148. }
  149. self_destruct_sequence() {
  150. echo "$1 will be deleted in three seconds."
  151. echo "CTRL-C now to avoid this fate!"
  152. echo "3"; sleep 1
  153. echo "2"; sleep 1
  154. echo "1"; sleep 1
  155. printf "0\nDestruction!"
  156. rm -rf "$1"
  157. }