sync_phone_music (1916B)
1 #!/usr/bin/env bash 2 if ! command -v adb-sync &>/dev/null; then 3 echo "adb-sync not installed." >&2 4 exit 1 5 fi 6 if ! command -v adb &>/dev/null; then 7 echo "adb not installed." >&2 8 exit 1 9 fi 10 11 12 do_sync() { 13 adb-sync -d "$@" 14 # Used to use this, keeping for posterity: 15 # rsync -uhamP --iconv=utf-8-mac,utf-8 --delete --stats "$@" 16 # This guy saved me: https://askubuntu.com/a/540960 17 # "one should always use `--iconv=utf-8-mac,utf-8` when initialising the rsync 18 # from the mac, and always use `--iconv=utf-8,utf-8-mac` when initialising the 19 # rsync from the linux machine, no matter if I want to sync files from the mac 20 # or linux machine." 21 } 22 die() { 23 echo "$1" >&2 24 exit 1 25 } 26 27 PHONE_DIR="/storage/5D6D-E047/Music" 28 MUSIC_DIR="${MUSIC_DIR:-$HOME/Music}" 29 30 [ -d "$MUSIC_DIR" ] || die "Music dir not present, expected at $MUSIC_DIR" 31 [ -n "$(adb devices | grep device$)" ] || die "Phone not connected." 32 33 PARAMS="" 34 while [ $(($#)) -ne 0 ]; do 35 case "$1" in 36 -y) 37 skip_dry_run=1 38 shift 39 ;; 40 41 -h|--help) 42 echo "Pass -y to skip dry run." 43 exit 0 44 ;; 45 --) # end arg parsing 46 shift 47 break 48 ;; 49 -*) # unsupported flags 50 echo "Unsupported flag $1" >&2 51 exit 1 52 ;; 53 *) # preserve positional arguments 54 PARAMS="$PARAMS $1" 55 shift 56 ;; 57 esac 58 done 59 eval set -- "$PARAMS" 60 # Piping directly didn't work for some reason, less wouldn't page it 61 if [ -z "$skip_dry_run" ]; then 62 outfile=$(mktemp) 63 trap "rm $outfile" INT TERM EXIT 64 echo "Calculating differences..." 65 { do_sync "$MUSIC_DIR"/ "$PHONE_DIR" --dry-run; } &> "$outfile" 66 less -fR "$outfile" && rm "$outfile" 67 trap - INT TERM EXIT 68 69 read -rp "Execute? [Y/n]" -n 1 -s conf 70 echo 71 72 case "$conf" in 73 Y|y) do_sync "$MUSIC_DIR"/ "$PHONE_DIR";; 74 *) die "User cancelled.";; 75 esac 76 else 77 do_sync "$MUSIC_DIR"/ "$PHONE_DIR" 78 fi 79