Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.19 KB | None | 0 0
  1. #!/bin/bash
  2. #############################
  3. #
  4. # pk3filter script by joki ( #racenet @ quakenet )
  5. # version 0.5
  6. #
  7. # use at your own risk!
  8. #
  9. # usage: pk3filter [files]
  10. #
  11. # directories can be setup inside the script
  12. #
  13. # required tools: bash, zipinfo, unzip, zip, sort (should be included in most linux distributions)
  14. #
  15. # bash version info: GNU bash, Version 4.1.5(1)-release (x86_64-pc-linux-gnu)
  16. #
  17. #
  18. #
  19. # use this script with a bunch of pk3 files. It will detect duplicate files inside the packages and put them into another package inside the package.
  20. # packages will be sorted in reversed alphabetical order.
  21. #
  22. # packages without duplicates will be copied to packageroot
  23. # packages with duplicates will be repacked and put to packageroot/incomplete
  24. #
  25. #############################
  26.  
  27. # you may edit the following variables to your needs
  28.  
  29. # set directories. they can also be relative
  30. # tmpdir: any directory you want the temporary files to be stored
  31. tmpdir="/media/tmpfs" # a tmpfs (ramdisk) might improve performance
  32.  
  33. # contentroot: the contents of the packages will be stored here. delete this directory, if you want to start a fresh run.
  34. # this can also be on a tmpfs, because there will be only empty files inside
  35. contentroot="/media/tmpfs/content"
  36.  
  37. # packageroot: the new ( repacked or copied ) packages will be stored here. repacked packages are in the "incomplete" directory.
  38. packageroot="/tmp/pk3filter/packages"
  39.  
  40. # prefix and suffix for repacked packages. leave empty to use the original filename.
  41. prefix=""
  42.  
  43. # suffix will be added BEFORE file extension
  44. suffix="_repack"
  45.  
  46.  
  47. # do not edit below ( you may, if you know what you are doing ;)
  48.  
  49. oldwd="$PWD"
  50. newline="
  51. ";
  52.  
  53. failed=0
  54. empty=0
  55. complete=0
  56. incomplete=0
  57.  
  58.  
  59. mkdir -p "$contentroot"
  60. mkdir -p "$packageroot/incomplete"
  61.  
  62.  
  63. function process_package
  64. {
  65.     local pk3file="$1"
  66.     local pk3name=`basename "${pk3file}"`
  67.  
  68.     filename="${pk3name%.*}"
  69.     fileext="${pk3name##*.}"
  70.     if [ "${fileext}" = "${pk3name}" ] ; then
  71.         fileext=""
  72.     else
  73.         fileext=".${fileext}"
  74.     fi
  75.  
  76.     echo -n "[${filename}] "
  77.  
  78.     unzipstr=`zipinfo -1 "$pk3file" 2>/dev/null`
  79.  
  80.     if [ "${unzipstr}" = "" ] ; then
  81.                 echo "zipinfo failed. Aborting!"
  82.         let failed++
  83.         return
  84.     fi
  85.  
  86.     # files: array of files inside the package (including path)
  87.     local files=()
  88.     local f=0 # f: pointer for files array
  89.  
  90.     # get files and directories and store them in arrays
  91.     # IFS is still newline (required for this for-loop to work)
  92.     for str in $unzipstr ; do
  93.         # zip directory entries are strange, dont use them
  94.         if [ "${str:(-1)}" = "/" ] ; then
  95.             continue;
  96.         fi
  97.         files[$f]="$str"
  98.         let f++
  99.  
  100.         # strip filename
  101.         local directory="$contentroot/$str"
  102.         directory="${directory%/*}"
  103.  
  104.         # convert directory to lowercase
  105.         directory="${directory,,}"
  106.  
  107.         # not really necessary, but nicer to skip root and multiple directories
  108.         case "$directory" in
  109.             "$oldDirectory" ) continue;;
  110.             "$contentroot" ) continue;;
  111.         esac
  112.         oldDirectory="$directory"
  113.  
  114.         if [ ! -d "${directory}" ] ; then
  115.             mkdir -p "${directory}"
  116.         fi
  117.     done
  118.  
  119.     # conversion to lowercase
  120.     # str="${str,,}"
  121.  
  122.     # all filesystem operations are to be lowercase.
  123.  
  124.     # valid and invalid: arrays of filenames
  125.     local iv=0
  126.     local invalid=()
  127.     # valid is not needed
  128.     #local v=0
  129.     #local valid=()
  130.  
  131.     # filter valid / invalid files, add valid files to filesystem
  132.     for file in ${files[*]}
  133.     do
  134.         # convert to lowercase for filesystem
  135.         fsFile="$contentroot/${file,,}"
  136.         if [ -f "${fsFile}" ] ; then
  137.             #echo "file already exists: ${file}"
  138.  
  139.             invalid[$iv]="${file}"
  140.             let iv++
  141.         else
  142.             #echo "touching file: ${fsFile}"
  143.             echo "" > "${fsFile}" # this is a little faster than `touch $fsFile`
  144.  
  145.             #valid[$v]="${file}"
  146.             #let v++
  147.         fi
  148.     done
  149.  
  150.     # decide what to do with this package
  151.     if [ ${#files[*]} -eq ${#invalid[*]} ] ; then # all files are duplicate, package will still be created
  152.         echo "duplicate files: all"
  153.         let empty++
  154.         # run repacking in background
  155.         repack_package "${pk3file}" "${filename}" "${fileext}" "${invalid[@]}" &
  156.         return
  157.     elif [ ${#invalid[*]} -gt 0 ] ; then # some files are duplicate
  158.         echo "duplicate files: ${#invalid[*]}"
  159.         let incomplete++
  160.         # run repacking in background
  161.         repack_package "${pk3file}" "${filename}" "${fileext}" "${invalid[@]}" &
  162.         return
  163.     else # all files are unique
  164.         echo "all files are unique!"
  165.         let complete++
  166.         keep_package "${pk3file}" &
  167.         return
  168.     fi
  169. }
  170.  
  171. #####
  172. # repack_package
  173. # $1 = pk3file
  174. # $2 = file name without extension
  175. # $3 = file extension
  176. # $4, $5, ... = content files to be renamed in package
  177. #####
  178. function repack_package
  179. {
  180.     local pkgfile="$1"
  181.     shift
  182.     local pkgname="$1"
  183.     shift
  184.     local pkgext="$1"
  185.     shift
  186.     local newpkg="${prefix}${pkgname}${suffix}${pkgext}"
  187.     local insidepkg="${pkgname}_missingfiles${pkgext}"
  188.  
  189.         # setup temporary directory
  190.         pkgtemp=`mktemp -d "${tmpdir}/pkg_XXXXX"`
  191.     cp "${pkgfile}" "${pkgtemp}/${newpkg}"
  192.     cd "${pkgtemp}"
  193.  
  194.     # extract duplicates from zipfile
  195.     unzip -qq "${newpkg}" $@
  196.  
  197.     # create inside package with duplicate files
  198.     zip -qq "${insidepkg}" $@
  199.  
  200.     # delete old duplicates from zipfile
  201.     zip -qq -d "${newpkg}" $@
  202.  
  203.     # create dupelist
  204.     local dupelist="${pkgname}_missingfiles.txt"
  205.     # IFS=newline tricking again
  206.     echo "$*" > "${dupelist}"
  207.  
  208.     # add dupelist and inside package to package
  209.     zip -qq "${newpkg}" "${dupelist}" "${insidepkg}"
  210.  
  211.     # save repacked package and clean up
  212.     mv "${newpkg}" "$packageroot/incomplete"
  213.     cd "$oldwd"
  214.     rm -rf "${pkgtemp}"
  215. }
  216.  
  217. #####
  218. # keep_package
  219. # $1 = pk3file
  220. #####
  221. function keep_package
  222. {
  223.     # copy package to packageroot
  224.     cp "${1}" "${packageroot}"
  225. }
  226.  
  227.  
  228. oldtime=`date +%s`
  229.  
  230. filecount=$#
  231.  
  232. oldifs="$IFS"
  233. IFS="${newline}"
  234.  
  235. filelist=`echo "${*}" | sort -r`
  236.  
  237. # check if file exists and process it
  238. for pkg in ${filelist} ; do
  239.     if [ -f "${pkg}" ] ; then
  240.         process_package "${pkg}"
  241.     else
  242.         let filecount--
  243.     fi
  244. done
  245.  
  246. IFS="${oldifs}"
  247.  
  248. wait
  249. newtime=`date +%s`
  250.  
  251. #rm -rf "$packageroot" "$contentroot"
  252.  
  253.  
  254. echo ""
  255. echo "Took $[ $newtime - $oldtime ] seconds for $filecount files"
  256. echo "Failed to unzip: $failed"
  257. echo "Empty packages left out: $empty"
  258. echo "Repacked packages: $incomplete"
  259. echo "Original packages: $complete"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement