mkv

imc.sh

mkv
Apr 9th, 2015
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.42 KB | None | 0 0
  1. #!/bin/bash
  2. # imc.sh - image copy
  3. # 2015 ed <irc.rizon.net>
  4. # something something mit license
  5. #
  6. set -e
  7. [ "x$2" == "x" ] &&
  8. {
  9.     echo
  10.     echo 'requires 2 arguments'
  11.     echo
  12.     echo '1) op mode:'
  13.     echo '   d, check for dupes'
  14.     echo '   n, check for continuity'
  15.     echo '   c, copy all images in folder'
  16.     echo '   r, copy all images recursively'
  17.     echo
  18.     echo '2) target directory'
  19.     echo
  20.     exit 1
  21. }
  22.  
  23. imagemask='jpg|jpeg|png|gif'
  24. rm imc.todo.list 2>/dev/null || true
  25. op="$1"
  26. to="$2"
  27. cpy=''
  28.  
  29. function getlatest()
  30. {
  31.     {
  32.         # list target directory with fallback '0.jpg'
  33.         echo '0.jpg'
  34.         find "$to" -maxdepth 1 2>/dev/null || true
  35.     } |
  36.  
  37.     # remove until final /
  38.     sed 's_.*/__' |
  39.    
  40.     # numerical sort
  41.     sort -n |
  42.  
  43.     # filter by number.whatever
  44.     grep -E '^[0-9]+\.' |
  45.  
  46.     # return the number of the last entry
  47.     tail -n 1 |
  48.     sed 's/\./\n/' |
  49.     head -n 1
  50. }
  51.  
  52. # copy single level
  53. [ "x$op" == "xc" ] &&
  54.     cpy='. -maxdepth 1'
  55.  
  56. # copy recursively
  57. [ "x$op" == "xr" ] &&
  58.     cpy='.'
  59.  
  60. # and then
  61. [ $cpy ] &&
  62. {
  63.     find $cpy -type f |
  64.     grep -E "\.($imagemask)$" > imc.todo.list
  65.  
  66.     echo -n 'Starting after '
  67.     fid=$(getlatest)
  68.     echo $fid
  69.    
  70.     # get number of files to copy
  71.     remains=$(
  72.         cat imc.todo.list |
  73.         wc -l
  74.     )
  75.    
  76.     # make sure target exists and has md5 list
  77.     mkdir -p "$to"
  78.     touch "$to/.md5"
  79.  
  80.     cat imc.todo.list |
  81.     while read file
  82.     do
  83.         echo -n "$remains  $file "
  84.         remains=$(( remains - 1 ))
  85.         hash=$(
  86.             md5sum -- "$file" |
  87.             head -c 32
  88.         )
  89.  
  90.         # check if exists
  91.         grep -qE "$hash" "$to/.md5" &&
  92.         {
  93.             echo -e "\033[1;33m no\033[0m"
  94.             continue
  95.         }
  96.        
  97.         fid=$((fid+1))
  98.         ext="$(
  99.            echo "$file" |
  100.            sed 's/\./\n/g' |
  101.            tail -n 1
  102.        )"
  103.  
  104.         # copy image + add md5sum
  105.         cp -- "$file" "$to/$fid.$ext"
  106.         echo "$hash  $fid.$ext" >> "$to/.md5"
  107.         echo -e "\033[1;32m Copied\033[0m"
  108.     done
  109.     rm imc.todo.list
  110.     echo 'done'
  111.     exit 0
  112. }
  113.  
  114. # check for dupes
  115. [ "x$op" == "xd" ] &&
  116. {
  117.     echo -e '\033[1;33mRunning dupe check\033[0m'
  118.    
  119.     md5sum "$to/"* |
  120.     sed 's_  .*/_  _' > "$to/.md5"
  121.  
  122.     cat "$to/.md5" |
  123.     sort |
  124.     uniq -d -w 32 |
  125.     while read hash
  126.     do
  127.         echo "Removing $hash"
  128.         fn="$(
  129.            echo "$hash" |
  130.            cut -c 35-
  131.        )"
  132.         rm "$to/$fn"
  133.     done
  134.     echo -e '\033[1;32mFinished checking for dupes.\033[0m'
  135.     echo 'Run a continuity check if changes were made'
  136.     exit 0
  137. }
  138.  
  139. # check continuity
  140. [ "x$op" == "xn" ] &&
  141. {
  142.     echo -e '\033[1;33mRunning continuity check\033[0m'
  143.     tn=0
  144.     find "$to" -maxdepth 1 |
  145.     grep -E "\.($imagemask)$" |
  146.     sort -n > imc.cont.list
  147.    
  148.     # iterate over all existing files
  149.     cat imc.cont.list |
  150.  
  151.     # remove everything until final slash
  152.     sed 's_.*/__' |
  153.  
  154.     # filter to number.whatever
  155.     grep -E '^[0-9]+\.' |
  156.  
  157.     # only keep number
  158.     sed 's/\..*//' |
  159.  
  160.     while read fn
  161.     do
  162.         tn=$(( tn + 1 ))
  163.         while [ $tn -lt $fn ]
  164.         do
  165.             # if tn and fn mismatch here,
  166.             # pick the first file that still exists
  167.             # off the end of the file list
  168.             # and swap it up to tn
  169.             tac imc.cont.list |
  170.             while read repl
  171.             do
  172.                 [ -e "$repl" ] &&
  173.                 {
  174.                     rnum=$(
  175.                         echo "$repl" |
  176.                         sed 's_.*/__;s/[^0-9].*//'
  177.                     )
  178.                     [ $rnum -le $tn ] &&
  179.                     {
  180.                         echo -e "\033[1;31mNO MORE SUBSTITUTE FILES WTF\033[0m"
  181.                         break
  182.                     }
  183.                     ext=$(
  184.                         echo "$repl" |
  185.                         sed 's/\./\n/g' |
  186.                         tail -n 1
  187.                     )
  188.                     echo "Moving $rnum.$ext to $tn.$ext"
  189.                     mv "$to/$rnum.$ext" "$to/$tn.$ext"
  190.                     break
  191.                 }
  192.             done
  193.             tn=$(( tn + 1 ))
  194.         done
  195.     done
  196.     rm imc.cont.list
  197.     echo -e '\033[1;32mFinished checking continuity.\033[0m'
  198.     echo 'Run a dupe check to refresh md5 cache if changes were made'
  199.     exit 0
  200. }
  201.  
  202. echo 'wat'
Advertisement
Add Comment
Please, Sign In to add comment