libdeblob.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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"'\n'"$2"'^' $PATCH_DIR/$(filetize "$3") > $PATCH_DIR/$(filetize "$3").tmp
  56. mv $PATCH_DIR/$(filetize "$3").tmp $PATCH_DIR/$(filetize "$3")
  57. diff ${SRC_DIR}/$3 $PATCH_DIR/$(filetize "$3") > $PATCH_DIR/$(filetize "$3").patch
  58. else
  59. sed 's^'"$1"'^'"$1"'\n'"$2"'^' ${SRC_DIR}/${3} > $PATCH_DIR/$(filetize "$3")
  60. diff ${SRC_DIR}/$3 $PATCH_DIR/$(filetize "$3") > $PATCH_DIR/$(filetize "$3").patch
  61. fi
  62. }
  63. # Removes a line.
  64. # Usage linedel $string $file
  65. linedel() {
  66. if [ -e "$PATCH_DIR/$(filetize "$2")" ]
  67. then
  68. grep -v "$1" $PATCH_DIR/$(filetize "$2") > $PATCH_DIR/$(filetize "$2").tmp
  69. mv $PATCH_DIR/$(filetize "$2").tmp $PATCH_DIR/$(filetize "$2")
  70. diff ${SRC_DIR}/$2 $PATCH_DIR/$(filetize "$2") > $PATCH_DIR/$(filetize "$2").patch
  71. else
  72. echo otherwise
  73. grep -v "$1" "${SRC_DIR}/$2" > $PATCH_DIR/$(filetize "$2")
  74. diff ${SRC_DIR}/$2 $PATCH_DIR/$(filetize "$2") > $PATCH_DIR/$(filetize "$2").patch
  75. echo otherhell
  76. fi
  77. }
  78. # "Copies" a file
  79. # Usage: filedel $file $dest
  80. filecp() {
  81. cp $1 $PATCH_DIR/ADD_$(filetize "$2")
  82. }
  83. # "Deletes" a file
  84. # Usage: filedel $file
  85. filedel() {
  86. echo $PATCH_DIR $1
  87. touch $PATCH_DIR/RM_$(filetize $1)
  88. }
  89. # Applies patches.
  90. apply() {
  91. for file in $PATCH_DIR/*
  92. do
  93. realname=$(echo $file | sed 's^.*/^^' | sed 's/ADD_//' | sed 's/RM_//')
  94. realname="$(unfiletize "$realname")"
  95. if echo "$file" | grep "RM_" > /dev/null
  96. then
  97. realname=$(echo "$realname" | sed 's/RM_//')
  98. echo "Deleting $realname in three seconds..."
  99. echo "3"; sleep 1
  100. echo "2"; sleep 1
  101. echo "1"; sleep 1
  102. if rm -rf ${SRC_DIR}/$realname
  103. then
  104. echo "$realname deleted" >> apply.log
  105. echo "$realname deleted"
  106. else
  107. echo "!!! $realname NOT deleted" >> apply.log
  108. echo "!!! $realname NOT deleted"
  109. fi
  110. elif echo "$file" | grep "ADD_" > /dev/null
  111. then
  112. realname=$(echo "$realname" | sed 's/ADD_//')
  113. echo "Copying $file to $realname..."
  114. if cp $file ${SRC_DIR}/$realname
  115. then
  116. echo "$realname copied from $file" >> apply.log
  117. else
  118. echo "!!! $realname NOT copied from $file" >> apply.log
  119. fi
  120. elif echo "$file" | grep ".patch$" > /dev/null
  121. then
  122. if patch "${SRC_DIR}/$(echo $realname | sed 's/\.patch//')" < $file
  123. then
  124. echo "${SRC_DIR}/$(echo $realname | sed 's/\.patch//') patched from $file" >> apply.log
  125. else
  126. echo "!!! ${SRC_DIR}/$(echo $realname | sed 's/\.patch//') NOT patched from $file" >> apply.log
  127. fi
  128. fi
  129. done
  130. }
  131. self_destruct_sequence() {
  132. echo "$1 will be deleted in five seconds."
  133. echo "CTRL-C now to avoid this fate!"
  134. echo "5"; sleep 1
  135. echo "4"; sleep 1
  136. echo "3"; sleep 1
  137. echo "2"; sleep 1
  138. echo "1"; sleep 1
  139. printf "0\nDestruction!"
  140. rm -rf "$1"
  141. }