sync_phone_playlist (1027B)
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 die() { 12 echo "$1" >&2 13 exit 1 14 } 15 16 process_file() { 17 echo "Processing $1" 18 tr '\r' '\n' < "$1" | sed 's:^/Volumes.*Music/:../Music/:' > "$tempdir"/"${1##*/}" 19 } 20 clean_up() { 21 rm -r "$tempdir" 22 exit 0 23 } 24 25 PHONE_DIR="/storage/5D6D-E047/Playlists" 26 [ ! -z "$(adb devices | grep device$)" ] || die "Phone not connected." 27 28 [ $# -ge 1 ] || die "Need something to act on." 29 30 tempdir="$(mktemp -d)" 31 trap clean_up INT KILL TERM QUIT 32 for i in "$@"; do 33 if [ -d "$i" ]; then 34 while read -r f; do 35 process_file "$f" 36 done < <(find "$i" -name "*.m3u" -type f -maxdepth 1) 37 elif [ -f "$i" ]; then 38 if [[ "$i" == *.m3u ]]; then 39 process_file "$i" 40 else 41 echo "Could not process $i" 42 fi 43 fi 44 done 45 46 read -rp "Sync now?" -n 1 -s conf 47 case "$conf" in 48 Y|y) 49 adb-sync "$tempdir/" "$PHONE_DIR" 50 ;; 51 esac 52 53 clean_up