Advertisement
g4r37h

MergeHelper with fixes

Aug 30th, 2019
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.71 KB | None | 0 0
  1. #!/bin/bash
  2. # 'MergeHelper v0.8' by mtrivs
  3. # Bash wrapper for binmerge to batch convert multi-track disc images to a single BIN/CUE pair
  4. # binmerge python script created by @putnam (https://github.com/putnam/binmerge)
  5. #
  6. # Requirements:
  7. #   - The root games directory should contain sub-folders for each disc
  8. #   - Each disc sub-folder should contain image files
  9. #   - Disc images should be in BIN/CUE format
  10. #   - Example:
  11. #   /my/game/root/     <-----THIS directory should be the 'GAMEROOT' (without the forward slash '/')
  12. #              |--> Some Game (USA)/
  13. #              |      |--> Some Game (USA) (Track 1).bin
  14. #              |      |--> Some Game (USA) (Track 2).bin
  15. #              |      |--> Some Game (USA).cue
  16. #              |--> Another Game (USA) (en, es, fr)/
  17. #              |      |--> Another Game (USA) (en, es, fr) (Track 1).bin
  18. #              |      |--> Another Game (USA) (en, es, fr) (Track 2).bin
  19. #              |      |--> Another Game (USA) (en, es, fr).cue
  20. #              |--> Already Merged Game (USA)/
  21. #             |--> Already Merged Game (USA).cue
  22. #             |--> Already Merged Game (USA).bin
  23. #             |--> orig/     <-----IF you want to remove the original source files after being merged, check out the 'REMOVEMODE' variable below
  24. #                |--> Already Merged Game (USA).cue
  25. #                |--> Already Merged Game (USA) (Track 1).bin
  26. #                |--> Already Merged Game (USA) (Track 2).bin
  27. ##############################################################################################
  28. #          Options
  29. ##############################################################################################
  30. # Set the 'GAMEROOT' directory to the root directory of your game folders.
  31. # NOTE: This variable should not end with a forward slash '/'
  32. GAMEROOT="/Volumes/Downloads/"
  33.  
  34. # Set the 'NAMEBY' variable to "cue" if you would like the game name determined by the CUE filename
  35. # By default, the name of the game folder containing the BIN/CUE files is used as the game name
  36. NAMEBY="cue"
  37.  
  38. # OPTIONAL: Set the 'PYDIR' variable below, to absolute location of your python executable.  May be required for certain configurations.
  39. # This must be python version 3!
  40. # If you do not set this variable, the script will call `python3` by name
  41. PYDIR="python3"
  42.  
  43. # By default the ORIGINAL milti-BIN files will be stored in a new "orig" folder and will NOT be deleted
  44. # If you uncomment line 168 below, the original BIN/CUE files will be deleted after a successful merge process
  45.  
  46. # BELOW HAS NOT BEEN IMPLEMENTED YET
  47. # The 'REMOVEMODE' variable is used to determine if the original BIN/CUE files should be deleted after a successful merge operation.
  48. # The variable can be optionally modified to one of the following values:
  49. #REMOVEMODE="0" #: The script will not remove any original BIN/CUE files. Source files will be moved to a new 'orig' folder, created
  50.                   #  in the same directory as the BIN/CUE files
  51. #REMOVEMODE="1" #: The script to remove original BIN/CUE files, if the binmerge operation completes successfully
  52. REMOVEMODE="1"  #: [default] Prompt once before the script is executed and ask if original BIN/CUE files should be removed after being merged
  53.  
  54.  
  55. ##############################################################################################
  56. #     DO NOT EDIT BELOW THIS LINE
  57. ##############################################################################################
  58. echo "~~~~~~~~~~~~~~~   MergeHelper v0.5  ~~~~~~~~~~~~~~~~~"
  59. echo "~~~~~~~~~~~~~~~    - by mtrivs -    ~~~~~~~~~~~~~~~~~"
  60. echo "~~~~~~~~~~~~~~  Powered by binmerge  ~~~~~~~~~~~~~~~~"
  61.  
  62. # Abort if Python version 3 is not installed
  63. "$PYDIR" --version &> /dev/null
  64. if [ $? -ne 0 ]; then
  65.         echo "ERROR: Python version 3 is required, but not found!"
  66.         echo "Try setting the PYDIR variable inside this script or installing it with your distribution's package manager"
  67.         echo "....Aborting!"
  68.         exit 1
  69. fi
  70.  
  71. # Check if BinMerge.py already exists
  72. if [[ -f ./BinMerge.py  ]]; then
  73.         if [[ -x ./BinMerge.py ]]; then
  74.                 echo "Dependency pre-check complelte.  Python3 installed and BinMerge.py downloaded!"
  75.         else
  76.                 echo "BinMerge.py is not executable.  Attempting to change permissions!"
  77.                 chmod +x ./BinMerge.py &> /dev/null
  78.                 if [[ -x ./BinMerge.py ]]; then
  79.                         echo "Successfully modified permissions for BinMerge.py!"
  80.                         echo "Dependency pre-check complelte.  Python3 installed and BinMerge.py downloaded!"
  81.                 else
  82.                         echo "ERROR: Unable to modify permissions for BinMerge.py!"
  83.                         echo "Correct this error and run the script again.....Aborting!"
  84.                         exit 1
  85.                 fi
  86.         fi
  87. else
  88.         echo "BinMerge.py does not exist!  Attempting to download it from GitHub (URL: https://raw.githubusercontent.com/putnam/binmerge/master/binmerge)"
  89.         wget -O ./BinMerge.py https://raw.githubusercontent.com/putnam/binmerge/master/binmerge &> /dev/null
  90.         if [[ -f ./BinMerge.py  ]]; then
  91.                 echo "Successfully downloaded BinMerge.py!"
  92.                 chmod +x ./BinMerge.py
  93.                 if [[ -x ./BinMerge.py ]]; then
  94.                         echo "Dependency pre-check complelte.  Python3 installed and BinMerge.py downloaded!"
  95.                 else
  96.                         echo "ERROR: Unable to modify permissions of BinMerge.py!"
  97.                         echo "Correct this error and run the script again.....Aborting!"
  98.                         exit 1
  99.                 fi
  100.         else
  101.                 echo "ERROR: Unable to download BinMerge.py from GitHub!"
  102.                 echo "Please try downloading the file manually, renaming it to "BinMerge.py", and place it"
  103.                 echo "in the same directory as this script....Aborting!"
  104.                 exit 1
  105.         fi
  106. fi
  107. # Loop through all sub-directories of the GAMEROOT
  108. for GAMEDIR in  "$GAMEROOT"/* ; do
  109.         if [ -d "$GAMEDIR" ]; then
  110.                 # Determine the number of BIN files inside Game directory
  111.                 BINCOUNT=(`find "$GAMEDIR" -maxdepth 1 -name "*.[bB][iI][nN]" | wc -l`)
  112.                 # Determine the number of CUE files inside Game directory
  113.         CUECOUNT=(`find "$GAMEDIR" -maxdepth 1 -name "*.[cC][uU][eE]" | wc -l`)
  114.  
  115.         # If a CUE file exists and the NAMEBY variable is set to `cue` above
  116.         if [ "$CUECOUNT" -eq 1 ] && [ "$NAMEBY" == "cue" ]; then
  117.                         # Determine the name of the game from the CUE file name
  118.             GAMENAME=`find "$GAMEDIR" -maxdepth 1 -type f -name "*.[cC][uU][eE]" -exec basename {} \;`
  119.                         GAMENAME=${GAMENAME%.*}
  120.         else
  121.                     # Determine the name of the game from the folder name
  122.             GAMENAME=$(basename "$GAMEDIR")
  123.         fi
  124.  
  125.                 # Display the game name being processed
  126.                 echo "Now processing: "$GAMENAME""
  127.  
  128.                 # If there are no BIN files in the folder, skip it
  129.                 if [ "$BINCOUNT" -eq 0 ]; then
  130.                         echo "    ERROR: unable to find any BIN files for "$GAMENAME".  Skipping folder!"
  131.                         continue
  132.             # If there are two or more BIN files, proceed with the merge process
  133.                 elif [ "$BINCOUNT" -gt 1 ]; then
  134.                         echo "    More than one BIN detected for "$GAMENAME", attempting BinMerge!"
  135.                         # Make sure there is only 1 CUE file inside the game directory
  136.                         if [ "$CUECOUNT" -eq 1 ]; then
  137.                                 CUENAME=`find "$GAMEDIR" -maxdepth 1 -type f -name "*.[cC][uU][eE]" -exec basename {} \;`
  138.                                 echo "    Found CUE file: "$CUENAME""
  139.                         elif [ "$CUECOUNT" -gt 1 ]; then
  140.                                 echo "    ERROR: Too many CUE files detected! Skipping merge for "$GAMENAME"..."
  141.                                 continue
  142.                         else
  143.                                 echo "    ERROR: No CUE file detected! Skipping merge for "$GAMENAME"..."
  144.                 continue
  145.             fi
  146.                         FAIL=0
  147.                         # Make a backup (orig) directory inside the game directory or FAIL=1
  148.                         eval mkdir -p "\"$GAMEDIR/orig\"" || FAIL=1
  149.                         if [ "$FAIL" -lt 1 ]; then
  150.                                 # No failure when creating bkup directory, now copy original BIN/CUE files to backup directory
  151.                                 echo "    Backing up original BIN/CUE files!"
  152.                                 eval mv "\"$GAMEDIR\""/*.{[cC][uU][eE],[bB][iI][nN]} "\"$GAMEDIR\""/orig/ || FAIL=1
  153.                         else
  154.                                 echo "    ERROR: Failed to create backup directory! Skipping file"
  155.                                 continue
  156.                         fi
  157.                         if [ "$FAIL" -lt 1 ]; then
  158.                                 # No failure when copying .bin/.cue to bkup.  Begin merging BIN/CUE files
  159.                                 echo "    Merging BIN files!"
  160.                                 NEWCUE=`find "$GAMEDIR"/orig -maxdepth 1 -name "*.[cC][uU][eE]"`
  161.                                 eval "$PYDIR" ./BinMerge.py "\"$NEWCUE\"" "\"$GAMENAME\"" -o "\"$GAMEDIR\"/" || FAIL=1
  162.                         else
  163.                                 echo "    ERROR: Failed to backup BIN/CUE files! Skipping file"
  164.                                 continue
  165.                         fi
  166.                         if [ "$FAIL" -lt 1 ]; then
  167.                                 # No failure in the conversion process. Uncomment below to remove backup directory after sucessful BIN merge
  168.                                 echo "    BinMerge completed successfully!"
  169.                                 if [ "$REMOVEMODE" == "1" ]; then
  170.                                         eval rm -r "\"$GAMEDIR\""/orig
  171.                                 fi
  172.                         else
  173.                                 # Merge process failed. Remove any partial BIN/CUE files and restore original BIN/CUE from backup
  174.                                 echo "    ERROR: Merge failed! Removing any partial BIN/CUE files and moving original files back!"
  175.                                 eval rm "\"$GAMEDIR\""/*.{[cC][uU][eE],[Bb][Ii][nN]} &> /dev/null
  176.                                 eval mv "\"$GAMEDIR\""/orig/*.{[cC][uU][eE],[Bb][Ii][nN]} "\"$GAMEDIR\"/" && eval rm -r "\"$GAMEDIR\""/orig
  177.                                 continue
  178.                         fi
  179.                 else
  180.                         # Game folder only has 1 BIN file, so there is nothing to merge.
  181.                         echo "    "$GAMENAME" doesn't need to be merged!"
  182.                 fi
  183.         fi
  184. done
  185. echo "** FINISHED processing all directories! **"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement