libdeblob.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #!/bin/sh
  2. ########################################
  3. # name: libdeblob.sh
  4. # main: jadedctrl
  5. # lisc: isc
  6. # desc: important functions for the
  7. # libertybsd-scripts project, for
  8. # rebranding and deblobbing obsd
  9. # sources.
  10. ########################################
  11. # --------------------------------------
  12. # generic
  13. # STRING --> NIL
  14. # A more reliable & portable 'echo'.
  15. # GNU echo will print '\n' verbatim; but BSD might
  16. # print a newline. That's literally program-breaking...
  17. # This gets around that-- '\n' is always verbatim.
  18. function necho {
  19. echo "\$string = \$ENV{'string'}; print \"\$string\\\n\";" \
  20. | string="$1" perl
  21. }
  22. # NIL --> STRING
  23. # read from stdin until eof hit; return all input
  24. # good for writing functions that take piped info
  25. function reade {
  26. while IFS= read -r line; do
  27. necho "$line";
  28. done
  29. }
  30. # STRING NUMBER --> NIL
  31. # Run a command a given amount of times.
  32. function dotimes {
  33. local iteration="$1"
  34. local command="$(echo "$@" | awk '{$1=""; print}')"
  35. eval "$command"
  36. iteration="$(echo "$iteration - 1" | bc)"
  37. if test 0 -lt "$iteration"; then
  38. dotimes "$iteration" "$command"
  39. fi
  40. }
  41. # STRING STRING STRING --> BOOLEAN
  42. # Return 0 or 1 from a yes-no input prompt.
  43. function yn_prompt {
  44. prompt="$1"
  45. y="$2"
  46. n="$3"
  47. printf '%s ' "$prompt"
  48. read response
  49. case $response in
  50. "$y") return 0
  51. ;;
  52. "$n") return 1
  53. ;;
  54. *) yn_prompt "$prompt" "$y" "$n"
  55. ;;
  56. esac
  57. }
  58. # NUMBER --> STRING
  59. # Print the given amount of spaces
  60. function space {
  61. local spaces="$1"
  62. dotimes $spaces printf "\" \""
  63. }
  64. # --------------------------------------
  65. # core
  66. # |STRING PATH NUMBER --> NIL
  67. # Send string to a patch-file, and make a patch for the change.
  68. # Pass the (source-dir) file path, and 1 or 2.
  69. # "1" as second argument means "append" to file, "2" to "overwrite"
  70. # Pipe text to this function, and it'll go to the old patch-file,
  71. # or a new one will be created automatically.
  72. function ofile {
  73. local text="$(reade)"
  74. local file="$1"
  75. local overwrite="$2"
  76. local filetized="$(filetize "$file")"
  77. local source_path="${SRC_DIR}/${file}"
  78. local patch_path="${PATCH_DIR}/${filetized}"
  79. local add_path="${PATCH_DIR}/ADD_${filetized}"
  80. local patch_orig="${PATCH_DIR}/${filetized}.orig"
  81. local patch_temp="${PATCH_DIR}/${filetized}.temp"
  82. local patch_diff="${PATCH_DIR}/${filetized}.patch"
  83. local target="$patch_path"
  84. if test -e "$add_path"; then
  85. local target="$add_path"
  86. elif test ! -e "$patch_path"; then
  87. cp "$source_path" "$patch_path"
  88. cp "$source_path" "$patch_orig"
  89. fi
  90. case "$overwrite" in
  91. "1") necho "$text" >> "$target" ;;
  92. "2") necho "$text" > "$target" ;;
  93. esac
  94. # we can't have patches for ADD_'ed files
  95. if test ! -e "$add_path"; then
  96. diff "$patch_orig" "$patch_path" > "$patch_diff"
  97. fi
  98. }
  99. # PATH --> STRING
  100. # Get string from patch-file (or source-file) from it's
  101. # source-path. Decides automatically which to choose.
  102. function ifile {
  103. local file="$1"
  104. local filetized="$(filetize "$file")"
  105. local source_path="${SRC_DIR}/${file}"
  106. local patch_path="${PATCH_DIR}/${filetized}"
  107. local add_path="${PATCH_DIR}/ADD_${filetized}"
  108. if test -e "$add_path"; then
  109. cat "$add_path"
  110. elif test -e "$patch_path"; then
  111. cat "$patch_path"
  112. else
  113. cat "$source_path"
  114. fi
  115. }
  116. # --------------------------------------
  117. # STRING --> STRING
  118. # Turn file-path into a friendly filename
  119. function filetize {
  120. local file="$1"
  121. echo "$file" \
  122. | sed 's|/|\^|g'
  123. }
  124. # STRING --> STRING
  125. # Vice-versa, you can probably see. reverse `filetize`.
  126. function unfiletize {
  127. local filetized_path="$1"
  128. echo "$filetized_path" \
  129. | sed 's|\^|/|g'
  130. }
  131. # --------------------------------------
  132. # file manipulation
  133. # STRING STRING PATH --> NIL
  134. # Replace all instances of a string within a file.
  135. function rep {
  136. local replaced="$1"
  137. local replacement="$2"
  138. local file="$3"
  139. printf "."
  140. ifile "$file" \
  141. | sed 's^'"$1"'^'"$2"'^g' \
  142. | ofile "$file" 2
  143. }
  144. # STRING PATH --> NIL
  145. # Delete all instances of a string from a file.
  146. function strdel {
  147. local string="$1"
  148. local file="$2"
  149. printf "."
  150. rep "$1" "" $2
  151. }
  152. # STRING STRING PATH --> NIL
  153. # Add a line following the first old line with "identifier" in it.
  154. function lineadd {
  155. local identifier="$1"
  156. local newline="$2"
  157. local file="$3"
  158. local oldline="$(ifile "$file" | grep "$identifier" | head -1)"
  159. printf "."
  160. # ideally we could use `rep`, but that can't take newlines
  161. ifile "$file" \
  162. | sed 's^'"$oldline"'^'"${oldline}"'\
  163. '"${newline}"'^' \
  164. | ofile "$file" 2
  165. }
  166. # STRING PATH --> NIL
  167. # Remove all lines that contain a given "identifier".
  168. function linedel {
  169. local identifier="$1"
  170. local file="$2"
  171. printf "."
  172. ifile "$file" \
  173. | grep -v "$identifier" \
  174. | ofile "$file" 2
  175. }
  176. # --------------------------------------
  177. # file operations
  178. # PATH PATH --> NIL
  179. # Copy a given directory (from "files/" CWD or form "$SRC_DIR/")--
  180. # recursively.
  181. function dircp {
  182. local dir="$1"
  183. local dest="$2"
  184. echo "Copying directory $dir"
  185. if echo "$dir" | grep -q "^files"; then
  186. local abs_path="$dir"
  187. else
  188. local abs_path="$SRC_DIR/$dir"
  189. fi
  190. for file in $(ls $abs_path); do
  191. local abs_file="$abs_path/$file"
  192. if test -d "$abs_file"; then
  193. dircp "$dir/$file" "$dest/$file"
  194. else
  195. filecp "$dir/$file" "$dest/$file"
  196. fi
  197. done
  198. }
  199. # PATH --> NIL
  200. # Delete a given file.
  201. function dirdel {
  202. local file="$1"
  203. touch "$PATCH_DIR/RMD_$(filetize "$1")"
  204. }
  205. # PATH PATH --> NIL
  206. # Copy a given file (from "files/" CWD or form "$SRC_DIR/")
  207. function filecp {
  208. local file="$1"
  209. local dest="$2"
  210. local patch_dest="$PATCH_DIR/ADD_$(filetize "$dest")"
  211. if echo "$file" | grep -q "^files/"; then
  212. cp "$file" "$patch_dest"
  213. else
  214. cp "$SRC_DIR/$1" "$patch_dest"
  215. fi
  216. }
  217. # PATH --> NIL
  218. # Delete a given file.
  219. function filedel {
  220. local file="$1"
  221. touch "$PATCH_DIR/RM_$(filetize "$1")"
  222. }
  223. # --------------------------------------
  224. # PATH --> NIL
  225. # Apply a file deletion (filetized, in PATCH_DIR).
  226. function apply_rm {
  227. local file="$1"
  228. local unfiletized="$(unfiletize "$(echo "$file" | sed 's/RM_//')")"
  229. echo "Deleting $unfiletized (from $file)!"
  230. rm "$SRC_DIR/$unfiletized"
  231. }
  232. # PATH --> NIL
  233. # Apply a file deletion (filetized, in PATCH_DIR).
  234. function apply_rmd {
  235. local file="$1"
  236. local unfiletized="$(unfiletize "$(echo "$file" | sed 's/RMD_//')")"
  237. echo "Deleting -rf $unfiletized (from $file)!"
  238. rm -rf "$SRC_DIR/$unfiletized"
  239. }
  240. # PATH --> NIL
  241. # Apply a file addition (filetized, in PATCH_DIR).
  242. function apply_add {
  243. local file="$1"
  244. local unfiletized="$(unfiletize "$(echo "$file" | sed 's/ADD_//')")"
  245. echo "Copying $file to $unfiletized!"
  246. cp "$PATCH_DIR/$file" "$SRC_DIR/$unfiletized"
  247. }
  248. # PATH --> NIL
  249. # Apply a given patch (filetized, in PATCH_DIR).
  250. function apply_patch {
  251. local file="$1"
  252. local unfiletized="$(unfiletize "$(echo "$file" | sed 's/\.patch//')")"
  253. echo "Applying $file to $unfiletized!"
  254. patch "$SRC_DIR/$unfiletized" < "$PATCH_DIR/$file"
  255. }
  256. # --------------------------------------
  257. # NIL --> NIL
  258. # Apply all patches.
  259. function apply {
  260. for file in $(ls "$PATCH_DIR")
  261. do
  262. if echo "$file" | grep -q "^RMD_"; then
  263. apply_rmd "$file"
  264. elif echo "$file" | grep -q "^RM_"; then
  265. apply_rm "$file"
  266. elif echo "$file" | grep -q "^ADD_"; then
  267. apply_add "$file"
  268. elif echo "$file" | grep -q "\.patch$"; then
  269. apply_patch "$file"
  270. fi
  271. done
  272. }
  273. # --------------------------------------
  274. # ports tree
  275. # PATH --> STRING
  276. # Print a string that may show a port's license.
  277. function port_lisc_preview {
  278. local port="$1"
  279. grep -B1 "PERMIT_PACKAGE_CDROM" \
  280. "$SRC_DIR/$port/Makefile" 2>/dev/null
  281. grep -B1 "PERMIT_PACKAGE_CDROM" \
  282. "$SRC_DIR/$port/Makefile.inc" 2>/dev/null
  283. }
  284. # PATH --> NIL
  285. # Add a given port to the blacklist.
  286. function add_nonfree_port {
  287. local port="$1"
  288. echo "$port_name" \
  289. >> files/ports/blacklist
  290. }
  291. # PATH --> NIL
  292. # Add a given port to the whitelist.
  293. function add_libre_port {
  294. local port="$1"
  295. echo "$port_name" \
  296. >> files/ports/whitelist
  297. }
  298. # PATH --> STRING
  299. # Return how a port is flagged, if at all.
  300. function libre_status {
  301. local port="$1"
  302. if grep -q "^$port$" files/ports/whitelist; then
  303. echo "libre"
  304. elif grep -q "^$port$" files/ports/blacklist; then
  305. echo "nonfree"
  306. else
  307. echo "undetermined"
  308. fi
  309. }