nix-configs

Personal NixOS and home-manager configuration files
Log | Files | Refs

phone-dump.nix (2181B)


      1 {pkgs, ...}: let
      2   phone-dump = pkgs.writeShellScriptBin "phone-dump" ''
      3     # variables
      4     phone_remote=phone
      5     destination="/tank/pictures/personal"
      6     if [ ! -d "$destination" ]; then
      7       echo "Destination $destination does not exist."
      8       exit 1
      9     fi
     10 
     11     # if ping phone
     12     if ping -c 1 "$phone_remote" &>/dev/null; then
     13       echo "Phone reachable, mounting remote"
     14       directory_temp="$(mktemp -d)"
     15       ${pkgs.rclone}/bin/rclone mount "$phone_remote": "$directory_temp" --daemon
     16       cd "$directory_temp" || exit
     17 
     18     declare -a directories=(
     19       "$directory_temp/DCIM/Camera"
     20       "$directory_temp/Pictures"
     21       "$directory_temp/Pictures/Whatsapp"
     22       "$directory_temp/Android/media/com.whatsapp/WhatsApp/Media"
     23       "$directory_temp/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Images"
     24       "$directory_temp/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Video"
     25       )
     26     for i in "''${directories[@]}"
     27     do
     28       if [ -d "$i" ]; then
     29         echo "$i"
     30         find "$i" -type f -name '.nomedia*' -delete -print
     31         find "$i" -type d -name '.thumbnails' -prune -exec find {} -mindepth 1 -delete \;
     32         ${pkgs.phockup}/bin/phockup "$i" "$destination/" -m
     33       fi
     34     done
     35 
     36       if [ -d "$directory_temp/Pictures/Screenshots" ]; then
     37         find "$directory_temp/Pictures/Screenshots" -type f -exec mv '{}' "$destination/screenshots/" -vi \;
     38       fi
     39 
     40       echo "Tidying up..."
     41       find "$destination" -type f -iname 'thumbs.db' -delete -print
     42       find "$destination" -type f -name '.nomedia*' -delete -print
     43       find "$destination" -type d -name '.thumbnails*' -delete -print
     44       find "$directory_temp" -maxdepth 2 -type d -not -path "*/\.*" -empty -delete -print 2>/dev/null
     45       echo "Unmounting storage..."
     46       sleep 2s
     47       umount "$directory_temp" || fusermount -uz "$directory_temp"
     48       echo "Deduplicating photos..."
     49       ${pkgs.jdupes}/bin/jdupes "$destination" -rdN
     50       find "/tmp/tmp.*" -maxdepth 1 -type d -not -path "*/\.*" -empty -delete -print 2>/dev/null
     51     else
     52       echo "Phone not reachable via ping, exiting" && exit 1
     53     fi
     54   '';
     55 in {
     56   environment.systemPackages = [phone-dump];
     57 }