nix-configs

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

phone-dump.nix (2089B)


      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         ${pkgs.phockup}/bin/phockup "$i" "$destination/" -m
     32       fi
     33     done
     34 
     35       if [ -d "$directory_temp/Pictures/Screenshots" ]; then
     36         find "$directory_temp/Pictures/Screenshots" -type f -exec mv '{}' "$destination/screenshots/" -vi \;
     37       fi
     38 
     39       echo "Tidying up..."
     40       find "$destination" -type f -iname 'thumbs.db' -delete -print
     41       find "$destination" -type f -name '.nomedia*' -delete -print
     42       find "$destination" -type d -name '.thumbnails*' -delete -print
     43       find "$directory_temp" -maxdepth 2 -type d -not -path "*/\.*" -empty -delete -print 2>/dev/null
     44       echo "Unmounting storage..."
     45       sleep 2s
     46       umount "$directory_temp" || fusermount -uz "$directory_temp"
     47       echo "Deduplicating photos..."
     48       ${pkgs.jdupes}/bin/jdupes "$destination" -r
     49       find "/tmp/tmp.*" -maxdepth 1 -type d -not -path "*/\.*" -empty -delete -print 2>/dev/null
     50     else
     51       echo "Phone not reachable via ping, exiting" && exit 1
     52     fi
     53   '';
     54 in {
     55   environment.systemPackages = [phone-dump];
     56 }