Nicodemis79

bookmarks_install.sh

Dec 21st, 2021 (edited)
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.97 KB | None | 0 0
  1. #!/bin/bash
  2. #./bin/TileMolester/${version}/resources
  3. version[0]="v0.15a"
  4. version[1]="v0.16"
  5. version[2]="v0.17a"
  6. version[3]="v0.18"
  7. version[4]="v0.19"
  8. version[5]="v0.20"
  9. global_args="$*"
  10. THIS_DIR=""
  11. function printTMversions() {
  12. for v in "${version[@]}"
  13. do
  14.     echo "$v"
  15. done
  16. }
  17. function showVersion() {
  18. cat << VERSION_EOF
  19. Bookmarks Installation Script by Europia79
  20. version 0.20 compiled on December 23th, 2021
  21. supports TileMolester versions:
  22. VERSION_EOF
  23. printTMversions
  24. }
  25. function showHelp() {
  26. cat << HELP_EOF
  27. Installs bookmarks for your romhack for every version of TileMolester.
  28.  
  29. You can install bookmarks 4 ways:
  30.  
  31. (1) Drag N Drop either your romhack or bookmark onto this script.
  32. (2) CLI (Command Line Interface):
  33. Usage:
  34.   ./bookmarks_install.sh [options] [--] <parameters>
  35.  
  36. Options:
  37.   -b      the bookmark XML file to install
  38.   -f      the romhack file that needs bookmarks
  39.   -h      display this help
  40.   -l      list all bookmarks installed
  41.   -u      uninstall all bookmarks
  42.   -v      display the version
  43.  
  44. Examples:
  45.   ./bookmarks_install.sh -a NHL94-Example-RomHack.bin -b bookmarks.nhl94.xml
  46.  
  47. (3) Declare missing argument parameters when prompted.
  48.   Or simply input null parameter(s) to abort installation.
  49. (4) Manually copy & paste booksmarks.nhl94.xml (for example) into the folder:
  50.   ./bin/TileMolester/${version}/resources/
  51.   then rename the XML file with the same name as your romhack.
  52.  
  53. Example:
  54.   If the name of your romhack is "NHL94-Example-RomHack.bin" (or any other extension),
  55.   then run the following command to get bookmarks in TileMolester v0.15a:
  56.   cp ./bookmarks.nhl94.xml ./bin/TileMolester/v0.15a/resources/NHL94-Example-RomHack.xml
  57. HELP_EOF
  58. }
  59. # CLOSE
  60. function close() {
  61.     exit 0
  62. }
  63. # PAUSE
  64. function pause() {
  65.     read -rsn1 -p "Press any key to continue..."
  66. }
  67. # DIE
  68. function die() {
  69.     local reason="$1"
  70.     printf "Installation aborted: %s" "${reason}"; echo ""
  71.     pause
  72.     exit 1
  73. }
  74. # Null Check for function arguments: $* $1 $2 etc.
  75. function parseInput() {
  76.     local PARSED_INPUT=""
  77.     if [ -n "$*" ]
  78.     then
  79.         PARSED_INPUT="$*"
  80.     else
  81.         PARSED_INPUT="${global_args}"
  82.     fi
  83.     echo "${PARSED_INPUT}"
  84. }
  85. # $(parseFullpath _RETURN_VAR __INPUT_VAL)
  86. # _RETURN_VAR: $1 = the array used to store the return values
  87. # __INPUT_VAL: $2 = the input string
  88. function parseFullpath() {
  89.     local -n returnval=$1
  90.     local input_path=$(parseInput $2)
  91.     # Convert backslashes on \Windows\ to foward slashes: /Windows/
  92.     local FULLPATH="${input_path//\\//}"
  93.     # [0] Strip longest match of */ from start
  94.     file="${FULLPATH##*/}"
  95.     # [1] Substring from 0 thru pos of filename
  96.     dir="${FULLPATH:0:${#FULLPATH} - ${#file}}"
  97.     # [2] Strip shortest match of . plus at least one non-dot char from end
  98.     base="${file%.[^.]*}"
  99.     # [3] If we have an extension and no base, it's really the base
  100.     ext="${file:${#base} + 1}"
  101.     if [ -z "${base}" ] && [ -n "${ext}" ]
  102.     then
  103.         base=".$ext"
  104.         ext=""
  105.     fi
  106.     returnval[0]="${file}"
  107.     returnval[1]="${dir}"
  108.     returnval[2]="${base}"
  109.     returnval[3]="${ext}"
  110. }
  111. # gets the entire Filename including extention.
  112. function getFile() {
  113.     local -a path_array=()
  114.     local file_input=$(parseInput "$*")
  115.     parseFullpath path_array "${file_input}"
  116.     echo "${path_array[0]}"
  117. }
  118. # gets the Directory.
  119. function getDirectory() {
  120.     local -a path_array=()
  121.     local dir_input=$(parseInput "$*")
  122.     parseFullpath path_array "${dir_input}"
  123.     echo "${path_array[1]}"
  124. }
  125. # get only the base Filename without the extention.
  126. function getBase() {
  127.     local -a path_array=()
  128.     local base_input=$(parseInput "$*")
  129.     parseFullpath path_array "${base_input}"
  130.     echo "${path_array[2]}"
  131. }
  132. # gets the Extention without the period.
  133. function getExtention() {
  134.     local -a path_array=()
  135.     local ext_input=$(parseInput "$*")
  136.     parseFullpath path_array "${ext_input}"
  137.     echo "${path_array[3]}"
  138. }
  139. function parseBookmark() {
  140.     local input=$(parseInput "$*")
  141.     if [ "xml" = $(getExtention "${input}") ] && [ -f "${input}" ] && [ -z "${bookmark}" ]
  142.     then
  143.         bookmark="${input}"
  144.     fi
  145. }
  146. function parseRomhack() {
  147.     local input=$(parseInput "$*")
  148.     if [[ "xml" != $(getExtention "${input}") ]] && [ -f "${input}" ] && [ -z "${romhack}" ]
  149.     then
  150.         romhack="${input}"
  151.     fi
  152. }
  153. function promptRomhack() {
  154.     if [ -n "${romhack}" ] ; then return 0; fi
  155.     find . -type f -name '*.md' -exec echo {} \;
  156.     find . -type f -name '*.smd' -exec echo {} \;
  157.     find . -type f -name '*.bin' -exec echo {} \;
  158.     find . -type f -name '*.32x' -exec echo {} \;
  159.     echo "What romhack are you working on ? (Press [Enter] to input or exit)"
  160.     read -p "" romhack
  161.     if [ -z "${romhack}" ]
  162.     then
  163.         die "NULL input."
  164.     elif [ ! -f "${romhack}" ]
  165.     then
  166.         printf "Invalid file: %s" "${romhack}"; echo ""
  167.         die "Please create your romhack file before installing TM Bookmarks for it."
  168.     elif [ -f "${romhack}" ]
  169.     then
  170.         echo "Romhack successfuly found: "
  171.         printf "%s" "${romhack}"; echo ""
  172.     fi
  173. }
  174. function promptBookmark() {
  175.     if [ -n "${bookmark}" ] ; then return 0; fi
  176.     find . -maxdepth 1 -type f -name '*.xml' -exec echo {} \;
  177.     echo "What bookmark do you want to use ? (Press [Enter] to input or exit)"
  178.     read -p "" bookmark
  179.     if [ -z "${bookmark}" ]
  180.     then
  181.         die "NULL input."
  182.     elif [ ! -f "${bookmark}" ]
  183.     then
  184.         echo "BAD input:  Installation aborted..."
  185.         printf "Invalid file: %s" "${bookmark}"; echo ""
  186.         die "BAD input."
  187.     elif [ -f "${bookmark}" ]
  188.     then
  189.         echo "Bookmark successfully found: "
  190.         printf "%s" "${bookmark}"; echo ""
  191.     fi
  192. }
  193. # List all bookmarks installed to
  194. # ./bin/TileMolester/${version}/resources/*.xml
  195. function listBookmarks() {
  196.     local total=0
  197.     for v in "${version[@]}"
  198.     do
  199.         local count=0
  200.         local dir="./bin/TileMolester/${v}/resources/"
  201.         find "${dir}" -type f -name '*.xml' -exec echo {} \;
  202.         count=$(find ${dir} -type f -name '*.xml' | wc -l)
  203.         total=$(( total + count ))
  204.     done
  205.     printf "%s total bookmarks installed." "${total}"; echo ""
  206. }
  207. # Deletes all bookmarks installed to
  208. # ./bin/TileMolester/${version}/resources/*.xml
  209. function _DELETE_ALL_BOOKMARKS() {
  210.     for v in "${version[@]}"
  211.     do
  212.         local dir="./bin/TileMolester/${v}/resources/"
  213.         find "${dir}" -type f -name '*.xml' -exec rm -vf {} \;
  214.     done
  215. }
  216. function getFinalConfirmation() {
  217.     local result_C=""
  218.     read -p "FINAL WARNING: Are you absolutely sure that you want to DELETE them ALL ? (Y/n) " result_C
  219.     local C="${result_C}"
  220.     if [[ "$C" = "Y" || "$C" = "y" || "$C" = "YES" || "$C" = "Yes" || "$C" = "yes" ]]
  221.     then
  222.         _DELETE_ALL_BOOKMARKS
  223.     else
  224.         echo "Uninstall aborted..."
  225.         close
  226.     fi
  227. }
  228. function getConfirmation() {
  229.     local result_B=""
  230.     read -p "WARNING: Are you sure that you want to uninstall ALL bookmarks ? (Y/n) " result_B
  231.     local B="${result_B}"
  232.     if [[ "$B" = "Y" || "$B" = "y" || "$B" = "YES" || "$B" = "Yes" || "$B" = "yes" ]]
  233.     then
  234.         getFinalConfirmation
  235.     else
  236.         echo "Uninstall aborted..."
  237.         close
  238.     fi
  239. }
  240. # Removes all bookmarks installed to
  241. # ./bin/TileMolester/${version}/resources/*.xml
  242. function removeBookmarks() {
  243.     listBookmarks
  244.     local result_A=""
  245.     read -p "WARNING: You are about to DELETE all these files: (Y/n) ? " result_A
  246.     local A="${result_A}"
  247.     if [[ "$A" = "Y" || "$A" = "y" || "$A" = "YES" || "$A" = "Yes" || "$A" = "yes" ]]
  248.     then
  249.         getConfirmation
  250.     else
  251.         echo "Uninstall aborted..."
  252.         close
  253.     fi
  254. }
  255. # Installs bookmarks for your romhack into
  256. # ./bin/TileMolester/${version}/resources/
  257. function doInstallation() {
  258.     for v in "${version[@]}"
  259.     do
  260.         local dir="./bin/TileMolester/${v}/resources/"
  261.         local filename=$(getBase "${romhack}")
  262.         local new_xml="${dir}${filename}.xml"
  263.         echo "dir=${dir}"
  264.         echo "filename=${filename}"
  265.         echo "new_xml=${new_xml}"
  266.         cp --verbose -- "${bookmark}" "${new_xml}"
  267.     done
  268. }
  269. ##########################################
  270. #                  START                 #
  271. ##########################################
  272. romhack=""
  273. bookmark=""
  274. options=":b:f:hluv"
  275. while getopts "${options}" flag
  276. do
  277.     case "${flag}" in
  278.         b) bookmark=${OPTARG};;
  279.         f) romhack=${OPTARG};;
  280.         h) showHelp; close;;
  281.         l) listBookmarks; close;;
  282.         u) removeBookmarks; close;;
  283.         v) showVersion; close;;
  284.         *) showHelp; die "Unknown argument: ${flag}";;
  285.     esac
  286. done
  287. # Fixes an error on Windows:
  288. # where Drag N Drop changes the working directory to
  289. # C:\Windows\system32
  290. # This changes it back to the directory of this script.
  291. if [ -n "${global_args}" ] && [ -d $(getDirectory "${global_args}") ]
  292. then
  293.     THIS_DIR=$(getDirectory "${global_args}")
  294.     cd ${THIS_DIR}
  295. fi
  296. # NULL check $romhack file:
  297. if [ -z "${romhack}" ] && [ -n "$*" ] && [[ "xml" != $(getExtention "$*") ]]
  298. then
  299.     parseRomhack "${global_args}"
  300. fi
  301. if [ -z "${romhack}" ]
  302. then
  303.     promptRomhack
  304. fi
  305. # NULL check $bookmark file:
  306. if [ -z "${bookmark}" ] && [ -n "$*" ] && [[ "xml" = $(getExtention "$*") ]]
  307. then
  308.     parseBookmark "${global_args}"
  309. fi
  310. if [ -z "${bookmark}" ]
  311. then
  312.     promptBookmark
  313. fi
  314. # INSTALL $bookmark for $romhack
  315. if [ -f "${bookmark}" ] && [ -f "${romhack}" ]
  316. then
  317.     echo "Installation started..."
  318.     doInstallation
  319.     echo "Installation complete."
  320.     pause
  321.     close
  322. fi
  323. echo "END of script."
  324. die "Unknown error"
  325.  
Advertisement
Add Comment
Please, Sign In to add comment