Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- start_time=$(($(date +%s%N)/1000000))
- ################################################
- ################################################
- ################################################
- ###
- ### -------------------------------------
- ### delays playlist converter script
- ### LINUX to WINDOWS and visa verse
- ### -------------------------------------
- ###
- ### The script compares playlists in two folders
- ### with different filepathformats: Linux<->Windows,
- ### sync them depending on last modification date
- ### and applies changes to each line of the playlists
- ###
- ### This script needs write permissions in
- ### the playlist folders.
- ###
- ### Installation:
- ### ----------------
- ### 1. Set the vars to your needs!
- ### 2. Run script once in terminal!
- ### 3. After installation just add it to your crons
- ###
- ### -----------------------
- ### EXECUTE as root is important!
- ### -----------------------
- ###
- ### All errors and changes are logged to syslog
- ###
- ################################################
- ######### CONFIGURATION START ##################
- ################################################
- #
- # Permissions for the script
- #
- # User and group for the mpd folders
- USER="mpd"
- GRP="audio"
- # User and group for the windows folders
- USER_WIN="delay"
- GRP_WIN="delay"
- # New file permissions - no need for change
- PERMS="0755"
- #
- # Add all your playlist filetypes here
- FILETYPES="*.m3u"
- #
- # MPD Playlist folder
- MPDPATH=/var/lib/mpd/playlists
- #
- # WIN Playlist folder
- WINPATH=/media/raid/Musik/Playlist
- #
- # Backup file
- BACKUP="$MPDPATH/playlist-backup.tar.gz"
- #######################################
- #
- # Converter configuration
- # -------------------
- # Just add a new entry for any change the script should do.
- # We using a little sed function to do the changes
- # Get information about linux sed: http://sed.sourceforge.net/sed1line.txt or http://www.pement.org/sed/sed1line.txt
- # - Syntax: change $1 's/ search reg exp / replace with /'
- # See the examples below:
- # This example looks for all \ and converts them to / inside the playlist
- # change $1 's/\\/\//g'
- # Delte lines matching the pattern
- # change $1 '/pattern/d'
- #######################################
- convert_mpd() {
- ### Convert mpd playlists to win playlists
- # Add a leading ..\
- change $1 's/^/..\\/'
- # Convert all / to \
- change $1 's/\//\\/g'
- }
- convert_win() {
- ### Convert win playlists to mpd playlists
- # Remove leading subdirs
- change $1 's/^\\\\MEDIA-SERVER\\Musik\\//'
- # Remove leading ..\
- change $1 's/^..\\//'
- # Remove leading slash
- change $1 's/^\///'
- # Convert all \ to /
- change $1 's/\\/\//g'
- }
- ################################################
- ######### CONFIGURATION END ####################
- ################################################
- echolog() {
- PRE="## PL-SYNC"
- if [ "$1" = "=" ]; then
- LOG="$PRE [=] No update needed -> $2"
- elif [ "$1" = "+" ]; then
- #LOG="$PRE [+] $LINES Entries created & converted -> $2 in $3"
- LOG="$PRE [+] $2($LINES) converted -> $3"
- logit "$LOG"
- elif [ "$1" = "~" ]; then
- LOG="$PRE [~] Converted -> $2 in $3"
- logit "$LOG"
- elif [ "$1" = "-" ]; then
- LOG="$PRE [-] File deleted, index removed -> $2"
- logit "$LOG"
- elif [ "$1" = "!" ]; then
- LOG="$PRE [!] $2 "
- logit "$LOG"
- else
- LOG="$PRE $1"
- fi
- echo "$LOG"
- LOG=""
- }
- logit()
- {
- logger -p local0.notice -- PlaylistConverter: $*
- }
- echo '## ///==-- Playlist-Sync-Convert Script v2.3 --==\\\'
- # Check if script is running
- if [ -z $(pgrep -c "$BASH_ARGV") ] ; then
- echolog "!" "ERROR -> Script is already running!"
- exit 0
- fi
- # Is script executed as root?
- if [ ! $EUID = 0 ]; then
- echolog "!" "ERROR -> This script must be executed as root!"
- exit 0
- fi
- # Check if all vars are set
- if [ -z "$MPDPATH" ] || [ -z "$WINPATH" ] || [ -z "$USER" ] || [ -z "$PERMS" ] || [ -z "$GRP" ] || [ -z "$USER_WIN" ] || [ -z "$GRP_WIN" ] ; then
- echolog "!" "ERROR -> Check your variables!"
- exit 0
- fi
- # Check if all folders are reachable
- if [ ! -d "$MPDPATH" ] || [ ! -d "$WINPATH" ] ; then
- echolog "!" "ERROR -> Can't find folders"
- exit 0
- else
- # Check folder write permissions
- if [ ! -w "$MPDPATH" ] ; then
- echolog "!" "ERROR -> No write permissions: $MPDPATH"
- exit 0
- elif [ ! -w "$WINPATH" ] ; then
- echolog "!" "ERROR -> No write permissions: $WINPATH"
- exit 0
- fi
- fi
- INDEX="$MPDPATH/index.tmp"
- # Create index file if none there
- if [ ! -f $INDEX ] ; then
- # Ask for 1st install
- echolog "-----------------------------------------"
- while true; do
- ASK=$(echolog '[?] Do you wish to install this program? y/n ')
- read -p "$ASK" yn
- case $yn in
- [Yy]* )
- echolog "[!] Start installation..."
- start_time=$(($(date +%s%N)/1000000))
- break
- ;;
- [Nn]* )
- echolog "!" "Installation canceled by user!"
- exit
- ;;
- * ) echolog "[?] Please answer yes or no.";;
- esac
- done
- echolog "!" "No index found, creating one -> $INDEX"
- touch $INDEX
- chown "$USER:$GRP" $INDEX
- chmod 0777 $INDEX
- echolog "[!] This is the 1st run of this script. All playlists will be synced and indexed now!"
- echolog "[!] Remember: if you delete any playlists later, the other one will also be deleted during the next script run!"
- echolog "-----------------------------------------"
- fi
- PLAYLISTS="0"
- ENTRIES="0"
- LINES="0"
- TMP="$MPDPATH/playlist.tmp"
- # Create tmp file if none there
- if [ ! -f $TMP ] ; then
- #echolog "!" "TMP file created: $TMP"
- touch $TMP
- #chown "$USER:$GRP" $TMP
- fi
- # Whitespace handling
- IFS=$'\n'
- # Global sed script
- change() {
- # Simple stuff...
- sed $2 $1 > $TMP
- rm $1
- cp -f $TMP $1
- # clear tmp
- > $TMP
- }
- # Create pl index entry
- index() {
- # Check for old index entry
- COUNT=$(grep -cx "$1" "$INDEX")
- if [ "$COUNT" -gt "0" ] ; then
- ### Index found
- # Delete file
- rm $1
- # Delete index entry
- sed '/'"$1"'/d' $INDEX > $TMP
- rm $INDEX
- # Write new index
- cp -f $TMP $INDEX
- > $TMP
- echolog "-" "$1"
- else
- ### No index entry found
- #echolog "[!] No index entry found!"
- # Add index entry
- echo "$1" >> $INDEX
- # Copy to new loaction, convert and change perms...
- cp -f "$1" "$3"
- if [ "$3" = "$MPDPATH" ] ; then
- convert_win "$2"
- chown "$USER:$GRP" "$2"
- chmod "$PERMS" "$2"
- elif [ "$3" = "$WINPATH" ] ; then
- convert_mpd "$2"
- chown "$USER_WIN:$GRP_WIN" "$2"
- chmod "$PERMS" "$2"
- fi
- touch "$1"
- touch "$2"
- # Increase playlist count
- LINES=$(wc -l < $2)
- PLAYLISTS=$(($PLAYLISTS + 1))
- ENTRIES=$(($ENTRIES + $LINES))
- echolog "+" "$1" "$3"
- fi
- }
- ### Check WINDOWS folder
- echolog "[!] ---> Check WINDOWS folder..."
- cd "$WINPATH"
- for x in $(ls $FILETYPES); do
- MPD=$(printf "$MPDPATH/$x")
- ##### Check if we have a MPD version of the playlist
- if [ -f "$MPD" ] ; then
- # Do actions depending which file is newer or equal
- ### If WIN is newer do stuff
- if [ "$x" -nt "$MPD" ] ; then
- # copy to mpd
- cp -f "$x" "$MPDPATH"
- convert_win "$MPD"
- touch "$x"
- touch "$MPD"
- echolog "~" "$x" "$MPDPATH"
- ### MPD is newer do stuff
- elif [ "$MPD" -nt "$x" ] ; then
- # copy to WIN
- cp -f "$MPD" "$WINPATH"
- convert_mpd "$x"
- touch "$x"
- touch "$MPD"
- echolog "~" "$x" "$WINPATH"
- ### Files are the same -> exit run
- else
- echolog "=" "$x"
- fi
- else
- ##### No MPD Version, create one
- # Create mpd version and change permission
- index "$x" "$MPD" "$MPDPATH"
- fi
- done
- ### Check MPD folder
- echolog "[!] ---> Check MPD folder..."
- cd "$MPDPATH"
- for x in $(ls $FILETYPES); do
- WIN=$(printf "$WINPATH/$x")
- ##### Check if we have a WIN version of the playlist
- if [ -f "$WIN" ] ; then
- # Do actions depending which file is newer or equal
- ### If MPD is newer do stuff
- if [ "$x" -nt "$WIN" ] ; then
- # copy to win
- cp -f "$x" "$WINPATH"
- convert_mpd "$WIN"
- touch "$x"
- touch "$WIN"
- echolog "~" "$x" "$WINPATH"
- ### WIN is newer do stuff
- elif [ "$WIN" -nt "$x" ] ; then
- # copy to WIN
- cp -f "$WIN" "$MPDPATH"
- convert_win "$x"
- touch "$x"
- touch "$WIN"
- echolog "~" "$x" "$MPDPATH"
- ### Files are the same -> exit run
- else
- echolog "=" "$x"
- fi
- else
- ##### No WIN Version, create one
- # Create win version and change permission
- index "$x" "$WIN" "$WINPATH"
- fi
- done
- ### Delete TMP file after use
- if [ -f $TMP ] ; then
- rm $TMP
- fi
- # Calculate runtime
- finish_time=$(($(date +%s%N)/1000000))
- # Check if this was the first run
- if [ -n "$ASK" ] ; then
- echolog "-----------------------------------------"
- echolog "!" "Installation & 1st run completed in $((finish_time - start_time))ms !"
- echolog "!" "$PLAYLISTS playlists containing $ENTRIES entries synchronized."
- echolog "[!] Remember: if you delete any playlists later, the other one will also be deleted during the next script run!"
- echolog "-----------------------------------------"
- else
- echolog "[!] $PLAYLISTS playlists / $ENTRIES entries in $((finish_time - start_time))ms synchronized."
- fi
- ################### ALL JOBS DONE :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement