Advertisement
Guest User

mktorrent wrapper

a guest
Sep 20th, 2019
460
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.17 KB | None | 1 0
  1. #!/bin/bash
  2.  
  3. # first save off the positional parameters
  4. OPTIONS=("$@")
  5.  
  6. # then find the last one, presumably the file or dir being dot torrented
  7. while [[ $# -gt 1 ]]; do
  8.     shift
  9. done
  10. target="$1"
  11.  
  12. # the max size for a given piece length in KB, or KiB if you prefer
  13. #
  14. PLMAXES=([15]=51200 [16]=102400 [17]=204800 [18]=409600 \
  15.         [19]=819200 [20]=1638400 [21]=3276800 [22]=6553600)
  16. pl="0"
  17.  
  18. # if the target exists, get the size of it and use that size to find the right
  19. # piece length by comparing to the max size for a given piece length. if it is
  20. # not smaller than any max size, use the piece length one larger than the
  21. # highest one with a max size.
  22. #
  23. # if the target doesn't seem to exist, call mktorrent with the original options
  24. # and let mktorrent give the error message as if the wrapper wasn't even here.
  25. #
  26. if [ -e "$target" ]; then
  27.     tsize=`du -sk "$target" | cut -f1`
  28.     for ((i = 15; i < 23 && pl == 0; i++)); do
  29.         if [[ $tsize -le ${PLMAXES[$i]} ]]; then
  30.             pl=$i
  31.         fi
  32.     done
  33.     if [[ $pl -eq 0 ]]; then
  34.         pl=$i
  35.     fi
  36.     mktorrent --piece-length=${pl} "${OPTIONS[@]}"
  37. else
  38.     mktorrent "${OPTIONS[@]}"
  39. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement