#!/bin/sh ## creates playlists on sansa ## takes one optinal argument : sansa mount dir ## ./sansa.sh "/mnt/Sansa 360" ### created by : Sujee Maniyam ### www : http://sujee.net ### contact : http://sujee.net/contact.php # configure this to point to your music collection # every thing underneath will be copied under sansa music folder MY_MUSIC_DIR=$HOME/sansa SANSA=/media/sansa #SANSA="/media/Sansa e260" if [ -n "$1" ]; then SANSA="$1" fi ## --- end config # sanity check; look for MUSIC, PLAYLIST folders if [ ! -d "$SANSA/music" -o ! -d "$SANSA/PLAYLISTS" ] ; then echo "**** can't find '$SANSA/music' or '$SANSA/PLAYLISTS'" echo "'$SANSA' doesn't seem to be a sansa mount, please provide a correct one" exit 1 fi #---------- functions ------------ #comment out MYDEBUG, or make it empty to stop debugging #MYDEBUG="yes" #debug true #MYDEBUG="" #no debug function debug_echo () { if [ -n "$MYDEBUG" ] ; then echo "$@" fi } # creates s playlist based on a dir # $1 - dir # file format: # If you have following directory structure # SANSA / Music / artist / song1.mp3 # SANSA / Music / artist / song2.mp3 # # THe playlist is : artist.PLA, it will be in SANSA/PLAYLISTS/ directory # Format is : # PLP PLAYLIST # VERSION 1.20 # # HARP, MUSIC\artist\song1.mp3 # # File format # - DOS style line endings # - DOS style backslashes # - encoded in UTF16-LE function create_playlist_for_dir () { dir=$1 a=${dir##*music\/} b=`echo "$a" | tr '\/' '_'` #debug_echo "****B : $b" name=`basename "$dir"` #playlist_file="$SANSA/PLAYLISTS/$name.PLA" playlist_file="$SANSA/PLAYLISTS/$b.PLA" #debug_echo "PLAYLIST=$name, file=$playlist_file" echo "PLP PLAYLIST" > "$playlist_file" echo "VERSION 1.20" >> "$playlist_file" echo "" >> "$playlist_file" for f in `find "$dir" -type f \( -name "*.[mM][pP]3" -o -name "*.[wW][mM][aA]" \)` do #debug_echo " $f" ext=${f##*music\/} ext2="MUSIC/$ext" # convert unix style forward slashes(/) to dos style backward slashes(\) ext3=`echo $ext2 | sed -e 's/\//\\\\/g'` #debug_echo " $ext3" echo "HARP, $ext3" >> "$playlist_file" #echo >> "$playlist_file" done # sansa likes dos style line endings unix2dos "$playlist_file" # sansa uses files in UTF16-LE encoding iconv -f ASCII -t UTF16LE -o "$playlist_file.TMP" "$playlist_file" mv "$playlist_file.TMP" "$playlist_file" # following is for debug only debug_echo "" debug_echo "----------------------------" debug_echo "PLAYLIST=$name, file=$playlist_file" content=`cat "$playlist_file"` debug_echo "........." debug_echo "$content" debug_echo "........." } #---------- end functions ------------ ## ------------ MAIN ---------------- # first copy the files # timestamps to be accurate within a second. This is needed as Unix file # system has more accurate timestamps the VFAT file sytem of sansa rsync -vtrL --modify-window=1 --delete "$MY_MUSIC_DIR/" "$SANSA/music" #rsync -vtr --size-only --modify-window=1 --delete ~/podcasts/ /media/disk2/music/podcasts/ # delete existing playlists # using a subshell, so the CD won't affect following statements #echo "HERE `pwd`" (cd "$SANSA"/PLAYLISTS/; rm -f *.PLA) #echo "HERE `pwd`" # set IFS to NEWLINE (from SPACE), as file names might have spaces in them OIFS="$IFS" IFS=$'\n' # create a playlist for each directory found under music for d in `find "$SANSA"/music/* -type d` do if [ -d "$d" ] ; then create_playlist_for_dir "$d" fi done IFS="$OIFS"