#!/usr/bin/env bash shopt -s globstar # check for config files if [[ ! -f $HOME/.config/clerk/config ]] && [[ ! -f /etc/clerk ]]; then echo "Error: could not find configuration file \"$HOME/.config/clerk/config\"" echo "You can use the provided example configuration file (config.clerk), copy it to the above location and edit it to your needs." exit fi # read global config, if present if [[ -f /etc/clerk.conf ]]; then source /etc/clerk.conf fi # read local config file if [[ -f $HOME/.config/clerk/config ]]; then source $HOME/.config/clerk/config fi # check for clerk_helper config. Create if needed. if [[ -f $HOME/.config/clerk/helper_config ]]; then : else echo "[global]" > $HOME/.config/clerk/helper_config echo "separator = " $separator "" >> $HOME/.config/clerk/helper_config echo "music_path = "$music_path"" >> $HOME/.config/clerk/helper_config echo " " >> $HOME/.config/clerk/helper_config echo "[updater]" >> $HOME/.config/clerk/helper_config echo "change_db = xxx" >> $HOME/.config/clerk/helper_config fi echo "$backend" # check for scrobbler if [[ $scrobbler == mpdas ]]; then export scrobbler="mpdas -d" export scrobbler_kill="mpdas" elif [[ $scrobbler == mpdscribble ]]; then export scrobbler_kill="mpdscribble" fi # check if clerk_helper process is running. if it isn't update cache files, if # needed if [[ $(ps x| grep clerk_helper | head -1 | grep -v 'grep') ]]; then : else clerk_helper update & fi export separator="$separator" # load cache files into variables to speed up access loadCacheAlbums () { if [[ -f $HOME/.config/clerk/albums.cache ]]; then album_temp=$(cat $HOME/.config/clerk/albums.cache) fi } loadCacheLatest () { if [[ -f $HOME/.config/clerk/latest.cache ]]; then last_temp=$(cat $HOME/.config/clerk/latest.cache) fi } loadCacheTracks () { if [[ -f $HOME/.config/clerk/tracks.cache ]]; then tracks_temp=$(cat $HOME/.config/clerk/tracks.cache) fi } # Use GNU coreutils on OSX sed=$([[ "$OSTYPE" == "darwin"* ]] && echo 'gsed' || echo 'sed') shuf=$([[ "$OSTYPE" == "darwin"* ]] && echo 'gshuf' || echo 'shuf') tac=$([[ "$OSTYPE" == "darwin"* ]] && echo 'gtac' || echo 'tac') updateCache() { rm -f $HOME/.config/clerk/*.cache clerk_helper createcache } # main Menu dplayPrompt () { if [[ -z $(mpc status | grep "/") ]]; then song="No Song is playing" else song=$(mpc current) fi menu=("Q Exit Clerk" "---" "1 Play random Album" "2 Play random Songs" "---" "3 Current Artist" "4 Current Queue" "5 Browse Library" "6 Manage Playlists" "---" "7 Options" "8 Ratings" "9 Lookup") menu=$(printf "%s\n" "${menu[@]}" | rofi -dmenu -lines 17 -mesg "${toggle}: Toggle Playback, ${prev}: Prev, ${next}: Next, ${stop}: Stop" -auto-select -p "Now Playing: ${song} > ") val=$? if [[ $val -eq 10 ]]; then mpc toggle dplayPrompt elif [[ $val -eq 11 ]]; then mpc prev dplayPrompt elif [[ $val -eq 12 ]]; then mpc next dplayPrompt elif [[ $val -eq 13 ]]; then mpc stop dplayPrompt fi if [[ "$menu" == "Q Exit Clerk" ]]; then exit elif [[ "$menu" == "1 Play random Album" ]]; then playRandomAlbum elif [[ "$menu" == "2 Play random Songs" ]]; then playRandomTracks elif [[ "$menu" == "3 Current Artist" ]]; then currentMenu elif [[ "$menu" == "4 Current Queue" ]]; then dplayQueue elif [[ "$menu" == "5 Browse Library" ]]; then browseLibPrompt elif [[ "$menu" == "6 Manage Playlists" ]]; then managePlaylists elif [[ "$menu" == "7 Options" ]]; then dplayOptionsPrompt elif [[ "$menu" == "8 Ratings" ]]; then ratingPrompt elif [[ "$menu" == "9 Lookup" ]]; then infoPrompt elif [[ -z "$menu" ]]; then exit fi } # start/stop scrobbler. locally or remote lastFM () { # Some Variables to clean up the code if [[ $ssh_lastfm == 1 ]]; then mpds_check="$(ssh $ssh_host -q -t "pgrep $scrobbler_kill")" if [ -n "$mpds_check" ]; then ssh $ssh_host -q -t "killall $scrobbler_kill" && notify-send "MPD" "LastFM Scrobbling Disabled" else ssh $ssh_host -q "$scrobbler" && notify-send "MPD" "LastFM Scrobbling Enabled" fi else if pgrep $scrobbler_kill then killall $scrobbler_kill && notify-send "MPD" "LastFM Scrobbling Disabled" else $scrobbler && notify-send "MPD" "LastFM Scrobbling Enabled" fi fi } lastFMCheck () { # Some Variables to clean up the code if ((ssh_lastfm)); then mpds_check="$(ssh $ssh_host -q -t "pgrep $scrobbler_kill")" if [ -n "$mpds_check" ]; then echo "lastfm: off" else echo "lastfm: on" fi else if pgrep $scrobbler_kill then echo "lastfm: on" else echo "lastfm: off" fi fi } # function to browse local filesystem. # Only works, if music_dir is accessible locally browseFilesystem () { usefile() { realpath="$(realpath "$selection")" MPD_HOST="$mpd_socket" mpc add ""file://"$realpath""" } comboadd() { for i in **/*.{mp3,ogg,flac,wma,mp4,aac,mpc,m4a,wv}; do realpath="$(realpath "$i")" MPD_HOST="$mpd_socket" mpc add ""file://"$realpath""" done } chose() { if [[ -d "$selection" ]]; then cd "$selection" dirs="$(find . -maxdepth 1 \( ! -regex '.*/\..*' \) -type d | cut -c 3- | sort -u)" files="$(find -maxdepth 1 -type f -regex ".*/.*\.\(flac\|mp3\|ogg\|m4a\|wav\|wv\|mpc\|wma\|aac\)" | cut -c 3- | sort -u)" selection="$(echo -e "0 Return to Library Menu\n---\n1 Add all music files\n---\n..$(echo "$dirs")\n$(echo "$files")" | dmenu_t -p "$(realpath .) > ")" chose elif [[ "$selection" == "1 Add all music files" ]]; then comboadd elif [[ "$selection" == "0 Return to Library Menu" ]]; then browseLibPrompt elif [[ "$selection" == "" ]]; then exit else usefile fi } cd ~ dirs="$(find . -maxdepth 1 \( ! -regex '.*/\..*' \) -type d | cut -c 3- | sort -u)" files="$(find -maxdepth 1 -type f -regex ".*/.*\.\(flac\|mp3\|ogg\|m4a\|wav\|wv\|mpc\|wma\|aac\)" | cut -c 3- | sort -u)" selection="$(echo -e "0 Return to Library Menu\n---\n1 Add all music files\n---\n..$(echo "$dirs")\n$(echo "$files")" | dmenu_t -p "$(realpath .) > ")" chose } # Show Albums and Tracks by currently playing artist currentMenu () { albums=$(clerk_helper getartistalbums "$(mpc current -f '%artist%')") titles=$(clerk_helper getartisttracks "$(mpc current -f '%artist%')") if [[ -z $(mpc current) ]]; then menu=$(echo -e "0 Return to Main Menu" | rofi -dmenu -p "No Music is playing") else current=$(mpc current -f '%artist%') menu=("0 Return to Main Menu" "---" "$albums" "---" "$titles") fi menu_temp=$(printf "%s\n" "${menu[@]}" | rofi -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -dmenu -format "f¬s" -filter "$filter" -select "$entry" -mesg "${add}: Add, ${insert}: Insert, ${replace}: Replace" -p "Other Music by ${current} > ") # for some reason directly defining $val wasnt working. Using a # temporary variable instead. tempval=$? val=$tempval menu="${menu_temp#*¬}" albumartist=$(mpc current -f '%albumartist%') artist=$(mpc current -f '%artist%') unset filter export filter="${menu_temp%¬*}" # checking for exit codes. if [[ $menu == "(Album)"* ]]; then if [[ $val -eq 11 ]]; then mpc find album "$(echo "$menu" | awk -F "$separator" '{ print $2 }')" artist "${artist}" | mpc insert entry="$menu" elif [[ $val -eq 0 || $val -eq 12 ]]; then mpc clear mpc find album "$(echo ${menu} | awk -F "$separator" '{ print $2 }')" artist "${artist}" | mpc add mpc play entry="$menu" elif [[ $val -eq 10 ]]; then mpc find album "$(echo ${menu} | awk -F "$separator" '{ print $2 }')" artist "${artist}" | mpc add entry="$menu" elif [[ $val -eq 1 ]]; then exit fi currentMenu elif [[ $menu == "(Song)"* ]]; then if [[ $val -eq 11 ]]; then mpc find track "$(echo ${menu} | awk -F ') ' '{ print $2 }' | awk -F "$separator" '{ print $1 }')" title "$(echo "$menu" | awk -F "$separator" '{ print $2 }')" artist "${artist}" | mpc insert title="$(echo "$menu" | awk -F "$separator" '{ print $2 }')" entry="$menu" elif [[ $val -eq 12 ]]; then mpc clear mpc find track "$(echo ${menu} | awk -F ') ' '{ print $2 }' | awk -F "$separator" '{ print $1 }')" title "$(echo "$menu" | awk -F "$separator" '{ print $2 }')" artist "${artist}" | mpc add mpc play entry="$menu" elif [[ $val -eq 0 || $val -eq 10 ]]; then mpc find track "$(echo ${menu} | awk -F ') ' '{ print $2 }' | awk -F "$separator" '{ print $1 }')" title "$(echo "$menu" | awk -F "$separator" '{ print $2 }')" artist "${artist}" | mpc add entry="$menu" elif [[ $val -eq 1 ]]; then exit fi currentMenu elif [[ -z "$menu" ]]; then exit elif [[ "$menu" == "${add}: Add, ${insert}: Insert, ${replace}: Replace" ]]; then currentMenu elif [[ $menu == "0 Return to Main Menu" ]]; then dplayPrompt fi } # rating menu ratingPrompt () { menu=("Q Return to Main Menu" "---" "1 Rate current Album" "2 Load Rated Albums" "3 Load Random Rated Album" "---" "4 Rate current Track" "5 Load Rated Tracks" "6 Load Random Rated Tracks" "---" "7 Love current Song on LastFM" "---" "0 Backup/Restore") prompt() { printf "%s\n" "$@" | dmenu_t -p "Ratings > " } case "$(prompt "${menu[@]}")" in 1*) rateAlbum ;; 2*) loadRatedAlbums ;; 3*) loadRandomRating ;; 4*) rateTrack ;; 5*) loadRatedTracks ;; 6*) loadRandomRatedTracks ;; 7*) loveLast ;; 0*) backupPrompt ;; Q*) dplayPrompt ;; *) exit esac } # create rating json files from mpd sticker database and vice versa backupPrompt () { menu=("0 Return to Ratings Menu" "---" "1 Backup Album Ratings to File" "2 Backup Track Ratings to File" "---" "3 Restore Ratings from File") prompt() { printf "%s\n" "$@" | dmenu_t -p "Backup/Restore > " } case "$(prompt "${menu[@]}")" in 1*) clerk_helper importalbumratings & ;; 2*) clerk_helper importtrackratings & ;; 3*) clerk_helper sendstickers & ;; 0*) ratingPrompt ;; *) exit esac } # if mpdas is used use mpc to love track on last.fm. otherwise try # lastfm-mpd-cli loveLast () { if [[ "$scrobbler" == "mpdscribble" ]]; then lastfm-mpd-cli love > /dev/null && notify-send "MPD" "Loved $(mpc current -f '%title%') on LastFM" && exit elif [[ "$scrobbler" == "mpdas" ]]; then mpc sendmessage mpdas love fi } infoPrompt () { menu=("0 Return to Main Menu" "---" "1 Artist Info" "2 Album Info" "3 Current Track Lyrics" "4 Current Track Tags") prompt() { printf "%s\n" "$@" | dmenu_t -p "MPD Menu > " } case "$(prompt "${menu[@]}")" in # 1*) surfraw yubnub allmusic $(mpc current -f %artist%) ;; 1*) artistinfo ;; 2*) surfraw yubnub allmusic $(mpc current -f %album%) && exit;; # 3*) surfraw yubnub google $(mpc current -f %title%) $(mpc current -f %artist%) lyrics ;; 3*) lyrics ;; 4*) currentTag ;; 0*) dplayPrompt ;; *) exit esac } lyrics () { rm -f $HOME/.config/clerk/current.txt glyrc lyrics -a "$(mpc current --format '%artist%')" -t "$(mpc current --format '%title%')" -w "$HOME/.config/clerk/current.txt" fold "$HOME/.config/clerk/current.txt" -w 50 -s | dmenu_t -p "$(mpc current --format '%artist% - %title%') Lyrics >" } artistinfo () { rm -f $HOME/.config/clerk/artist.txt glyrc artistbio -a "$(mpc current --format '%artist%')" -w "$HOME/.config/clerk/artist.txt" fold "$HOME/.config/clerk/artist.txt" -s -w 50 | dmenu_t -p "$(mpc current --format '%artist% - %title%') Lyrics >" } currentTag () { declare -i seen=0 while read line do seen=1 if [[ "$line" == "0 Return to Main Menu" ]]; then dplayPrompt elif [[ "$line" == "Show all Tags" ]]; then readComments elif [[ "$line" == "" ]]; then return fi done < <(echo -e "0 Return to Main Menu\n---\nShow all Tags\n---\n$(mpc current --format "Artist: %artist%\nAlbum: %album%\nDate: %date%\nTrack: %track%\nTitle: %title%")" | dmenu_t -p 'Current Song > ') if [[ $seen = 0 ]] then exit fi } # read all tags from file. Note that mpd can only read vorbiscomment properly readComments () { declare -i seen=0 while read line do seen=1 if [[ "$line" == "0 Return to Main Menu" ]]; then dplayPrompt elif [[ "$line" == "Show Tags" ]]; then currentTag elif [[ "$line" == "" ]]; then return fi done < <(echo -e "0 Return to Main Menu\n---\nShow Tags\n---\n$(mpc current --format '%file%' | clerk_helper readcomments)" | dmenu_t -p 'Current Song > ') if [[ $seen = 0 ]] then exit fi } # Messy options menu. dplayOptionsPrompt () { # define variables to be used in menu export status="$(mpc status)" single=$(echo "$status" | tail -1 | awk -F ':' '{ print $5 }' | cut -d ' ' -f 2) random=$(echo "$status" | tail -1 | awk -F ':' '{ print $4 }' | cut -d ' ' -f 2) consume=$(echo "$status" | tail -1 | awk -F ':' '{ print $6 }' | cut -d ' ' -f 2) repeat=$(echo "$status" | tail -1 | awk -F ':' '{ print $3 }' | cut -d ' ' -f 2) if [[ -a /tmp/mpd-sima.pid ]]; then export sima=on else export sima=off fi if [[ "$ssh_lastfm" == "1" ]]; then mpds_check="$(ssh $ssh_host -q -t "pgrep $scrobbler_kill")" if [ -n "$mpds_check" ]; then export scrobble=on else export scrobble=off fi else if pgrep $scrobbler_kill then export scrobble=on else export scrobble=off fi fi export rgain="$(mpc replaygain | cut -d ' ' -f 2)" replayGain () { if [[ $(mpc replaygain | cut -d ' ' -f 2) == album ]]; then mpc replaygain track > /dev/null && export rgain="track" elif [[ $(mpc replaygain | cut -d ' ' -f 2) == track ]]; then mpc replaygain off > /dev/null && export rgain="off" elif [[ $(mpc replaygain | cut -d ' ' -f 2) == off ]]; then mpc replaygain album > /dev/null && export rgain="album" fi } menu=("Q Return to Main Menu" "---" "1 Random: $(echo $random)" "2 Repeat: $(echo $repeat)" "3 Single Mode: $(echo $single)" "4 Consume Mode: $(echo $consume)" "5 Replaygain: $(echo $rgain)" "6 Scrobbling: $(echo $scrobble)" "7 Similar Artists Mode: $(echo $sima)" "---" "8 Set Crossfade $(mpc crossfade | cut -d ':' -f2)" "9 Manage Outputs" "0 Number of Random Songs: $(echo $value)") prompt() { printf "%s\n" "$@" | dmenu_t -auto-select -p "MPD Options > " } case "$(prompt "${menu[@]}")" in 1*) mpc random && dplayOptionsPrompt ;; 2*) mpc repeat && dplayOptionsPrompt ;; 3*) mpc single && dplayOptionsPrompt ;; 4*) mpc consume && dplayOptionsPrompt ;; 5*) replayGain && dplayOptionsPrompt ;; 6*) lastFM && dplayOptionsPrompt ;; 7*) mpdSima && dplayOptionsPrompt ;; 8*) crossfadePrompt ;; 9*) outputPrompt ;; 0*) optionRandomPrompt ;; Q*) dplayPrompt ;; *) exit esac } # toggle similar artist playback mpdSima () { if [[ -a /tmp/mpd-sima.pid ]]; then kill $(cat /tmp/mpd-sima.pid) sleep 1 else mpd-sima -d -p /tmp/mpd-sima.pid sleep 1 fi } # function to change number of random songs in config file optionRandomPrompt() { number="$(echo " " | dmenu_t -p 'Set No. of Songs for random Songs > ')" $sed -i "s/value=.*/value="$number"/" $HOME/.config/clerk/config export value="$number" dplayOptionsPrompt } crossfadePrompt () { menu=("0: Return to Main Menu" "---" "0" "1" "2" "3" "4" "5") prompt() { printf "%s\n" "$@" | dmenu_t -p "Crossfade > " } case "$(prompt "${menu[@]}")" in 0) mpc crossfade 0 && dplayOptionsPrompt ;; 1) mpc crossfade 1 && dplayOptionsPrompt ;; 2) mpc crossfade 2 && dplayOptionsPrompt ;; 3) mpc crossfade 3 && dplayOptionsPrompt ;; 4) mpc crossfade 4 && dplayOptionsPrompt ;; 5) mpc crossfade 5 && dplayOptionsPrompt ;; 0:*) dplayOptionsPrompt ;; *) exit esac } managePlaylists () { menu=("0 Return to Main Menu" "---" "1 Load Playlist" "2 Save Playlist" "3 Load RSS Feed" "4 Crop Playlist" "---" "5 Suspend Playlist" "6 Resume Playlist" "---" "7 Clear Playlist") prompt() { printf "%s\n" "$@" | dmenu_t -auto-select -p "Playlists > " } case "$(prompt "${menu[@]}")" in 0*) dplayPrompt ;; 1*) dplayQueueLoad ;; 2*) dplayQueueSave ;; 3*) loadRSS ;; 4*) mpc crop && dplayQueue ;; 5*) suspendPlaylist ;; 6*) resumePlaylist ;; 7*) mpc clear ;; *) exit esac } # read list of available podcasts. Not using mpd playlists, because mpd does # not support custom names for urls. format of podcast in file is "Name \ URL" loadRSS () { mpc clear podcast=$(echo -e "0 Return to Playlist Menu\n---\n$(cat $HOME/.config/clerk/podcasts | cut -d '\' -f1)" | dmenu_t -p "Choose Podcast > ") if [[ $podcast == "0 Return to Playlist Menu" ]]; then managePlaylists else mpc load $(grep "$podcast" $HOME/.config/clerk/podcasts | cut -d '\' -f2) episode=$(mpc playlist --format "%position%$separator%artist%$separator%title%" | dmenu_t -p "Choose Episode > ") POS=$(echo "$episode" | awk -F "$separator" '{ print $1 }') mpc play "$POS" fi } # suspend current playlist. playlist, song id and play-position are saved to # $HOME/.config/clerk/suspend suspendPlaylist () { playing=$(! mpc status | grep 'playing\|paused') time=$(mpc status | $sed '2!d;s;/.:.*;;;s;.* ;;') position=$(mpc current --format '%position%') if [[ -z "$playing" ]]; then notify-send "clerk" "mpd is not playing, no state to suspend" else mpc rm suspended mpc save suspended rm -f $HOME/.config/clerk/suspend echo "pos="$position"" >> $HOME/.config/clerk/suspend echo "time="$time"" >> $HOME/.config/clerk/suspend if [[ "$stop_after_suspend" == yes ]]; then mpc stop else echo " " fi notify-send "Clerk" "Playlist suspended" managePlaylists fi } # read $HOME/.config/clerk/suspend and restore playlist. Then start playing # from same position that was saved in suspend file resumePlaylist () { http=$(! mpc current --format %file% | grep 'http://') source $HOME/.config/clerk/suspend mpc clear mpc load suspended mpc play $pos mpc toggle sleep 2 mpc seek "$time" mpc toggle notify-send "Clerk" "Resumed last-suspended Playlist" managePlaylists } # Play or delete items from current Queue dplayQueue () { while true; do if [[ -z $POS ]]; then TRACKDISPLAY=("0 Return to Main Menu" "---" "$(mpc playlist --format "%position%$separator%artist%$separator%track%$separator%title%$separator%album%")") else # check if POS is a number. if it is, add 4 to it. POS is later # exported from song ID in playlist. re='^[0-9]+$' if [[ "$POS" =~ $re ]]; then POS=$(( $POS + 2 )) else POS=0 fi TRACKDISPLAY=("0 Return to Main Menu" "---" "$(mpc playlist --format "%position%$separator%artist%$separator%track%$separator%title%$separator%album%")") fi TRACKDISPLAY=$(printf "%s\n" "${TRACKDISPLAY[@]}" | rofi -custom-kb-1 "${play}" -custom-kb-2 "${delete}" -custom-kb-3 "${rate}" -dmenu -selected-row $POS -mesg "${play}: Play, ${delete}: Delete, ${rate}: Rate" -p "Current Queue > ") tempval=$? val=$tempval TITLE=$(echo "$TRACKDISPLAY" | awk -F "$separator" '{ print $4 }') ARTIST=$(echo "$TRACKDISPLAY" | awk -F "$separator" '{ print $2 }') ALBUM=$(echo "$TRACKDISPLAY" | awk -F "$separator" '{ print $5 }') TRACK=$(echo "$TRACKDISPLAY" | awk -F "$separator" '{ print $3 }') export POS=$(echo "$TRACKDISPLAY" | awk -F "$separator" '{ print $1 }') if [[ "$TRACKDISPLAY" == "0 Return to Main Menu" ]]; then dplayPrompt elif [[ -z "$TRACKDISPLAY" ]]; then exit elif [[ "$TRACKDISPLAY" == "${play}: Play, ${delete}: Delete" ]]; then dplayQueue else if [[ $val -eq 11 ]]; then mpc del $POS POS=$(( $POS - 1)) elif [[ $val -eq 0 || $val -eq 10 ]]; then mpc play $POS; elif [[ $val -eq 13 ]]; then rateartist="${ARTIST}" ratealbum="${ALBUM}" ratetrack="${TRACK}" ratetitle="${TITLE}" rateTrack fi fi done } # show all mpd playlists and load them to queue dplayQueueLoad () { playlist=$(echo -e "0 Return to Playlist Menu\n---\n$(mpc lsplaylists)" | dmenu_t -p "Load Playlist > ") if [[ "$playlist" == "0 Return to Playlist Menu" ]]; then managePlaylists else mpc clear mpc load "$playlist" && dplayQueue fi } # save current playlist to playlist file. dplayQueueSave () { while read playlists do if [[ "$playlists" == "0 Return to Main Menu" ]]; then dplayPrompt elif [[ "$playlists" == "Save new Playlist" ]]; then playlist=$(echo "" | dmenu_t -p "Type Name for Playlist > ") if [[ "$playlist" == "" ]]; then dplayQueueSave else mpc save "$playlist" dplayQueue fi else playlist=$(echo -e "0 Return to Playlist Menu\n---\nYes\nNo" | dmenu_t -p "Overwrite Playlist? > ") if [[ "$playlist" == "Yes" ]]; then mpc rm "$playlists" mpc save "$playlists" elif [[ "$playlist" == "No" ]]; then playlist=$(echo "" | dmenu_t -p "Type Name for Playlist > ") if [[ "$playlist" == "" ]]; then dplayQueue else mpc save "$playlist" dplayQueue fi fi fi done < <(echo -e "0 Return to Main Menu\n---\nSave new Playlist\n---\n$(mpc lsplaylists)" | dmenu_t -p "Choose Playlist > ") exit } # enable/disable outputs outputPrompt () { menu="$(echo -e "0 Return to Options Menu\n---\n$(mpc outputs)" | dmenu_t -p "Outputs > ")"; if [[ "$menu" == "0 Return to Options Menu" ]] then dplayOptionsPrompt; elif [[ "$menu" == "" ]]; then exit else mpc toggleoutput $(echo "$menu" | awk '{print $2}'); notify-send "MPD" "$(echo "$menu" | $sed -e 's/enabled$/disabled/;ta;s/disabled$/enabled/;:a;')"; outputPrompt fi } # rate any album rateAlbum () { rating="$(seq 10 | dmenu_t -p "Select Album Rating: > ")" if [[ $rating == "" ]]; then exit else # check if rateartist was defined, if it wasn't use currently playing # track for rating, otherwise use what was delivered in the rate* # variables. if [[ -z "$rateartist" ]]; then artist=$(mpc current -f '%artist%') album=$(mpc current -f '%album%') date=$(mpc current -f '%date%') if [[ -n $(mpc find track "1" albumartist "${artist}" album "${album}" date "${date}") ]]; then track="1" else track="01" fi disc=$(mpc current -f '%disc%') export disc=${disc}; export track=${track}; clerk_helper ratealbum "${artist}" "${album}" "${date}" "${rating}" notify-send "clerk" "rated ${artist} - ${album} with ${rating}" else export disc=${disc}; export track=${track}; clerk_helper ratealbum "${rateartist}" "${ratealbum}" "${ratedate}" "${rating}" notify-send "clerk" "rated ${rateartist} - ${ratealbum} with ${rating}" fi fi } rateTrack () { rating="$(seq 10 | dmenu_t -p "Select Track Rating: > ")" if [[ $rating == "" ]]; then exit else if [[ -z "$rateartist" ]]; then export rating=${rating} rateartist=$(mpc current -f '%artist%') ratetitle=$(mpc current -f '%title%') ratetrack=$(mpc current -f '%track%') ratealbum=$(mpc current -f '%album%') clerk_helper ratetrack "${rateartist}" "${ratealbum}" "${ratetrack}" "${ratetitle}" "${rating}" notify-send "clerk" "rated $(mpc current) with $(echo ${rating})" else clerk_helper ratetrack "${rateartist}" "${ratealbum}" "${ratetrack}" "${ratetitle}" "${rating}" notify-send "clerk" "rated ${rateartist} - ${ratetitle} with $(echo ${rating})" fi fi } # function to instantly rate a track without a submenu. rating is defined on # commandline instantRateTrack () { rateartist=$(mpc current -f '%artist%') ratetitle=$(mpc current -f '%title%') ratetrack=$(mpc current -f '%track%') ratealbum=$(mpc current -f '%album%') export rating=$1 clerk_helper ratetrack "${rateartist}" "${ratealbum}" "${ratetrack}" "${ratetitle}" "${rating}" notify-send "clerk" "rated ${rateartist} - ${ratetitle} with $(echo ${rating})" } # load rated albums with minimum rating of xx loadRatedAlbums () { rating="$(seq 10 | dmenu_t -p "Minimum Rating > ")" if [[ $rating == "" ]]; then exit else albums="$(while read -a line; do dirname "${line[*]}"; done <<< "$(mpc sticker "" find albumrating | grep -E "albumrating=$rating")" | $sed 's/\/\CD.*//g' | sort | uniq | dmenu_t -p "Choose Album > ")" if [[ $albums == "" ]]; then exit else mpc clear && mpc add "$albums" && mpc play fi fi } loadRatedTracks () { rating="$(seq 10 | dmenu_t -p "Rating > ")" if [ rating = "" ]; then exit else cd $HOME/.config/clerk mpc clear songs="$(mpc sticker "" find rating | awk -F 'rating=' '{ print $2 }')" echo "$songs" | mpc add mpc play fi } loadRandomRatedTracks () { number="$(echo " " | dmenu_t -p "Number of Songs > " | xargs echo)" rating="$(seq 10 | dmenu_t -p "Minimum Rating > ")" if [ rating = "" ]; then exit else cd $HOME/.config/clerk mpc clear songs="$(mpc sticker "" find rating | grep -E "rating=$rating|rating=$(echo $(( $rating + 1 )))|rating=$(echo $(( $rating + 2 )))|rating=$(echo $(( $rating + 3 )))|rating=$(echo $(( $rating + 4 )))" | awk -F ':' '{ print $1 }')" echo "$songs" | $shuf -n $number | mpc add mpc play rm -f /tmp/clerk_tracklist fi } loadRandomRating () { rating="$(seq 10 | dmenu_t -p "Minimum Rating > ")" if [ rating = "" ]; then exit else album="$(while read -a line; do dirname "${line[*]}"; done <<< "$(mpc sticker "" find albumrating | grep -E "albumrating=$rating|albumrating=$(echo $(( $rating+1 )))|albumrating=$(echo $(( $rating+2 )))|albumrating=$(echo $(( $rating+3 )))|albumrating=$(echo $(( $rating+4 )))|albumrating=$(echo $(( $rating+5 )))|albumrating=$(echo $(( $rating+6 )))")" | $sed 's/\/\CD.*//g' | $shuf -n1)" mpc clear && mpc add "$album" && mpc play fi } # load random album. Make sure to make each sub item random. this way each # artist has equal chances of being played, no matter how many albums it has. playRandomAlbum () { mpc clear > /dev/null artist="$(mpc list "albumartist" | $shuf -n 1)" album="$(mpc list album "albumartist" "$artist" | $shuf -n 1)" mpc find album "$album" "albumartist" "$artist" | mpc add && mpc play > /dev/null } # same for tracks, no artist should be preferred because it has more tracks. playRandomTracks () { mpc clear > /dev/null artist="$(mpc list "$random_artist" | $shuf -n 1)" album="$(mpc list album "$random_artist" "$artist" | $shuf -n 1)" title="$(mpc list title album "$album" "$random_artist" "$artist" | $shuf -n 1)" mpc find album "$album" "$random_artist" "$artist" title "$title" | mpc add mpc play > /dev/null n=0; while (( n++ < $value -1 )); do artist="$(mpc list "$random_artist" | $shuf -n 1)" album="$(mpc list album "$random_artist" "$artist" | $shuf -n 1)" title="$(mpc list title album "$album" "$random_artist" "$artist" | $shuf -n 1)" mpc find album "$album" "$random_artist" "$artist" title "$title" | mpc add done mpc play > /dev/null exit } addLastMod() { if [[ -z $last_temp ]]; then loadCacheLatest > /dev/null else echo "re-using track list from memory" # unset tracks_temp # export tracks_temp=${tracks_temp} > /dev/null fi menu="0 Return to Browse Menu --- ${last_temp}" help_text=$(echo -e "${add}: Add - ${insert}: Insert - ${replace}: Replace (Default)\n${rate}: Rate - ${addplay}: Add + Play - ${insertplay}: Insert + Play" | column -s '-' -t) line1=$(echo "${help_text}" | head -1) line2=$(echo "${help_text}" | tail -1) HELP="${line1} ${line2}" #HELP="${add}: Add, ${insert}: Insert, ${replace}: Replace (Default), ${rate}: Rate #${addplay}: Add & Play, ${insertplay}: Insert & Play" TRACK_TEMP=$(echo -e "${menu}" | dmenu_t -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-8 "${rate}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -dmenu -select "$entry" -filter "$filter" -format "f¬s" -mesg "${HELP}" -p "Choose Album > ") val=$? TRACK="${TRACK_TEMP#*¬}" unset filter export filter="${TRACK_TEMP%¬*}" if [[ "$TRACK" == "0 Return to Browse Menu" ]] then browseLibPrompt elif [[ -z "$TRACK" ]]; then exit elif [[ "$TRACK" == "${add}: Add, ${insert}: Insert, ${replace}: Replace (Default)" ]]; then addLastMod else artist=$(echo "$TRACK" | awk -F "$separator" '{print $1}') date=$(echo "$TRACK" | awk -F "$separator" '{print $2}') album=$(echo "$TRACK" | awk -F "$separator" '{print $3}') if [[ $val -eq 11 ]]; then mpc search date "$date" album "$album" albumartist "$artist" | mpc insert entry="$TRACK" elif [[ $val -eq 0 || $val -eq 12 ]]; then echo "return code is 13" mpc clear && mpc search date "$date" album "$album" albumartist "$artist" | mpc add mpc play entry="$TRACK" elif [[ $val -eq 10 ]]; then mpc search date "$date" album "$album" albumartist "$artist" | mpc add elif [[ $val -eq 13 ]]; then mpc findadd date "$date" album "$album" albumartist "$artist" mpc searchplay "${album}" elif [[ $val -eq 17 ]]; then if [[ -n $(mpc find -f '%disc%' albumartist "${artist}" album "${album}" date "${date}") ]]; then disc=$(mpc find -f '%disc%' albumartist "${artist}" album "${album}" date "${date}" | head -1) else disc="" fi if [[ -n $(mpc find track "1" albumartist "${artist}" album "${album}" date "${date}") ]]; then track="1" else track="01" fi disc=${disc} track=${track} rateartist="${artist}" ratealbum="${album}" ratedate="${date}" rateAlbum entry="$TRACK" elif [[ $val -eq 14 ]]; then mpc clear && mpc find date "$date" album "$album" albumartist "$artist" | mpc add mpc searchplay "${album}" entry="$TRACK" elif [[ $val -eq 1 ]]; then exit fi addLastMod fi } AddAlbumTags() { if [[ -z $album_temp ]]; then loadCacheAlbums > /dev/null else echo "re-using track list from memory" # unset tracks_temp # export tracks_temp=${tracks_temp} > /dev/null fi menu="0 Return to Browse Menu --- ${album_temp}" help_text=$(echo -e "${add}: Add - ${insert}: Insert - ${replace}: Replace (Default)\n${rate}: Rate - ${addplay}: Add + Play - ${insertplay}: Insert + Play" | column -s '-' -t) line1=$(echo "${help_text}" | head -1) line2=$(echo "${help_text}" | tail -1) HELP="${line1} ${line2}" TRACK_TEMP=$(echo -e "${menu}" | dmenu_t -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-8 "${rate}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -dmenu -filter "$filter" -select "$entry" -format "f¬s" -mesg "${HELP}" -p "Choose Album > ") val=$? TRACK="${TRACK_TEMP#*¬}" unset filter export filter="${TRACK_TEMP%¬*}" if [[ "$TRACK" == "0 Return to Browse Menu" ]]; then browseLibPrompt elif [[ -z "$TRACK" ]]; then exit elif [[ "$TRACK" == "${add}: Add, ${insert}: Insert, ${replace}: Replace (Default), ${rate}: Rate" ]]; then unset line AddAlbumTags else artist=$(echo "$TRACK" | awk -F "$separator" '{print $1}') date=$(echo "$TRACK" | awk -F "$separator" '{print $2}') album=$(echo "$TRACK" | awk -F "$separator" '{print $3}') if [[ $val -eq 11 ]]; then mpc find date "$date" album "$album" albumartist "$artist" | mpc insert entry="$TRACK" elif [[ $val -eq 0 || $val -eq 12 ]]; then mpc clear && mpc find date "$date" album "$album" albumartist "$artist" | mpc add mpc play entry="$TRACK" elif [[ $val -eq 17 ]]; then if [[ -n $(mpc find -f '%disc%' albumartist "${artist}" album "${album}" date "${date}") ]]; then disc=$(mpc find -f '%disc%' albumartist "${artist}" album "${album}" date "${date}" | head -1) else disc="" fi if [[ -n $(mpc find track "1" albumartist "${artist}" album "${album}" date "${date}") ]]; then track="1" else track="01" fi disc=${disc} track=${track} rateartist="${artist}" ratealbum="${album}" ratedate="${date}" rateAlbum entry="$TRACK" elif [[ $val -eq 10 ]]; then mpc search date "$date" album "$album" albumartist "$artist" | mpc add entry="$TRACK" elif [[ $val -eq 14 ]]; then mpc findadd date "$date" album "$album" albumartist "$artist" mpc searchplay "${album}" elif [[ $val -eq 15 ]]; then mpc clear && mpc find date "$date" album "$album" albumartist "$artist" | mpc add mpc searchplay "${album}" elif [[ $val -eq 1 ]]; then exit fi AddAlbumTags fi } AddTrackTags() { if [[ -z $tracks_temp ]]; then loadCacheTracks > /dev/null else echo "re-using track list from memory" # unset tracks_temp # export tracks_temp=${tracks_temp} > /dev/null fi menu="0 Return to Browse Menu --- ${tracks_temp}" help_text=$(echo -e "${add}: Add (Default) - ${insert}: Insert - ${replace}: Replace\n${rate}: Rate - ${addplay}: Add + Play - ${insertplay}: Insert + Play" | column -s '-' -t) line1=$(echo "${help_text}" | head -1) line2=$(echo "${help_text}" | tail -1) HELP="${line1} ${line2}" TRACK_TEMP=$(echo -e "$menu" | dmenu_t -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-8 "${rate}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -dmenu -filter "$filter" -select "$entry" -format "f¬s" -mesg "${HELP}" -p "Choose Track > ") val=$? TRACK="${TRACK_TEMP#*¬}" unset filter export filter="$(echo ${TRACK_TEMP} | awk -F '¬' '{ print $1 }')" if [[ "$TRACK" == "0 Return to Browse Menu" ]] then browseLibPrompt elif [[ -z "$TRACK" ]]; then exit elif [[ "$TRACK" == "${add}: Add | ${insert}: Insert | ${replace}: Replace | ${rate}: Rate" ]]; then AddTrackTags else artist=$(echo "$TRACK" | awk -F "$separator" '{print $1}') date=$(echo "$TRACK" | awk -F "$separator" '{print $2}') album=$(echo "$TRACK" | awk -F "$separator" '{print $3}') track=$(echo "$TRACK" | awk -F "$separator" '{print $4}') title=$(echo "$TRACK" | awk -F "$separator" '{print $5}') if [[ $val -eq 11 ]]; then mpc find artist "$artist" album "$album" title "$title" date "$date" | mpc insert entry="$TRACK" elif [[ $val -eq 12 ]]; then mpc clear mpc findadd artist "$artist" album "$album" title "$title" date "$date" mpc play entry="$TRACK" elif [[ $val -eq 0 || $val -eq 10 ]]; then mpc findadd artist "$artist" album "$album" title "$title" date "$date" entry="$TRACK" elif [[ $val -eq 13 ]]; then mpc findadd artist "$artist" album "$album" title "$title" date "$date" mpc searchplay "${title}" elif [[ $val -eq 14 ]]; then mpc find artist "$artist" album "$album" title "$title" date "$date" | mpc insert mpc searchplay "${title}" elif [[ $val -eq 17 ]]; then rateartist="${artist}" ratealbum="${album}" ratetrack="${track}" ratetitle="${title}" rateTrack entry="$TRACK" elif [[ $val -eq 1 ]]; then exit fi AddTrackTags fi } browseDate() { help_text=$(echo -e "${add}: Add - ${insert}: Insert - ${replace}: Replace\n${addplay}: Add + Play - ${insertplay}: Insert + Play" | column -s '-' -t) line1=$(echo "${help_text}" | head -1) line2=$(echo "${help_text}" | tail -1) HELP="${line1} ${line2}" date=$(echo -e "0 Return to Browse Menu\n---\n$(mpc list date | $tac)" | dmenu_t -mesg "${HELP}" -dmenu -p "Choose Date > ") val=$? if [[ $val -eq 0 ]]; then if [[ "$date" == "0 Return to Browse Menu" ]] then browseLibPrompt else browseDateAdd fi elif [[ $val -eq 10 ]]; then mpc findadd date "${date}" browseDate elif [[ $val -eq 11 ]]; then mpc find date "${date}" | mpc insert browseDate elif [[ $val -eq 12 ]]; then mpc clear; mpc findadd date "${date}"; mpc play browseDate elif [[ $val -eq 13 ]]; then mpc findadd date "${date}" mpc searchplay "${date}" browseDate elif [[ $val -eq 14 ]]; then mpc find date "${date}" | mpc insert mpc searchplay "${date}" browseDate fi } browseDateAdd() { help_text=$(echo -e "${add}: Add - ${insert}: Insert - ${replace}: Replace\n${addplay}: Add + Play - ${insertplay}: Insert + Play" | column -s '-' -t) line1=$(echo "${help_text}" | head -1) line2=$(echo "${help_text}" | tail -1) HELP="${line1} ${line2}" menu=("0 Return to Date Menu" "---" "$(mpc --format "%albumartist%$separator%album%" find date "$date" | uniq)") if [[ -z $line ]]; then select_temp=$(printf "%s\n" "${menu[@]}" | rofi -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -dmenu -format "i¬s" -mesg "${HELP}" -p "Select Album > ") else select_temp=$(printf "%s\n" "${menu[@]}" | rofi -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -dmenu -selected-row $(( $line + 1 )) -format "i¬s" -mesg "${HELP}" -p "Select Album > ") fi val=$? select="${select_temp#*¬}" unset line export line="${select_temp%¬*}" echo "$line" artist=$(echo "$select" | awk -F "$separator" '{print $1}') album=$(echo "$select" | awk -F "$separator" '{print $2}') if [[ "$val" -eq 13 ]]; then mpc findadd date "$date" artist "${artist}" album "${album}" mpc searchplay "${album}" browseDateAdd elif [[ "$val" -eq 15 ]]; then mpc clear && mpc findadd date "$date" artist "${artist}" album "${album}" mpc searchplay "${album}" browseDateAdd elif [[ "$val" -eq 14 ]]; then mpc find date "$date" artist "${artist}" album "${album}" | mpc insert mpc searchplay "${album}" browseDateAdd fi if [[ "$select" == "0 Return to Date Menu" ]] then browseDate else if [[ "$val" -eq 10 ]]; then mpc findadd date "$date" artist "$artist" album "$album" browseDateAdd elif [[ "$val" -eq 11 ]]; then mpc find date "$date" artist "$artist" album "$album" | mpc insert browseDateAdd elif [[ "$val" -eq 12 || "$val" -eq 0 ]]; then mpc clear && mpc findadd date "$date" artist "$artist" album "$album" && mpc play browseDateAdd elif [[ "$val" -eq 1 ]]; then exit fi fi } browseLibPrompt() { menu=("Q Return to Main Menu" "---" "1 Browse by Artist" "2 Browse by Albumartist" "3 Browse by Date" "4 Browse by Genre" "5 Browse latest additions" "---" "6 Choose Albums" "7 Choose Track" "---" "0 Update Album/Track Cache") prompt() { printf "%s\n" "$@" | dmenu_t -auto-select -p "Library Menu > " } case "$(prompt "${menu[@]}")" in 1*) browseArtist ;; 2*) browseAlbumArtist ;; 3*) browseDate ;; 4*) browseGenre ;; 6*) AddAlbumTags ;; 7*) AddTrackTags ;; 0*) updateCache && browseLibPrompt ;; Q*) dplayPrompt ;; 5*) addLastMod ;; *) exit esac } browseAlbum() { echo "$artistentry" export artistentry="$artistentry" HELP="${add}: Add | ${insert}: Insert | ${replace}: Replace (Default) ${addplay}: Add & Play | ${insertplay}: Insert & Play" ALBUMS=$(mpc list album artist "$ARTIST") ALBUM=("0 Return to Artist Menu" "---" "$(mpc --format "%date%$separator%album%" find artist "$ARTIST" | sort | uniq)") ALBUM_TEMP=$(printf "%s\n" "${ALBUM[@]}" | dmenu_t -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -dmenu -select "$albumentry" -filter "$albumfilter" -format "f¬s" -mesg "${HELP}" -p "Choose Album > ") val=$? ALBUM="${ALBUM_TEMP#*¬}" export albumfilter="${ALBUM_TEMP%¬*}" ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') unset line export line="${ALBUM_TEMP%¬*}" if [[ "$val" -eq 10 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc find artist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" | mpc add albumentry="$ALBUM_FINAL" browseAlbum elif [[ "$val" -eq 11 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc find artist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" | mpc insert albumentry="$ALBUM_FINAL" browseAlbum elif [[ "$val" -eq 12 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc clear mpc find artist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" | mpc add mpc play albumentry="$ALBUM_FINAL" browseAlbum elif [[ "$val" -eq 15 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc clear && mpc findadd artist "$ARTIST" album "$ALBUM_FINAL" mpc searchplay "${ALBUM_FINAL}" albumentry="$ALBUM_FINAL" browseAlbum elif [[ "$val" -eq 13 ]]; then export ALBUM_FINAL=$(echo "${ALBUM}" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "${ALBUM}" | awk -F "$separator" '{ print $1 }') mpc findadd artist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" mpc searchplay "${ALBUM_FINAL}" albumentry="$ALBUM_FINAL" browseAlbum elif [[ "$val" -eq 14 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc find artist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" | mpc insert mpc searchplay "${ALBUM_FINAL}" albumentry="$ALBUM_FINAL" browseAlbum elif [[ "$val" -eq 0 ]]; then if [[ "$ALBUM" == "0 Return to Artist Menu" ]]; then unset albumfilter unset albumentry browseArtist else export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') albumentry="${ALBUM_FINAL}" browseTrack fi elif [[ "$val" -eq 1 ]]; then exit fi exit } browseAlbumArtistAlbum() { HELP="${add}: Add | ${insert}: Insert | ${replace}: Replace (Default) ${addplay}: Add & Play | ${insertplay}: Insert & Play" ALBUMS=$(mpc list album albumartist "$ARTIST") ALBUM=("0 Return to Artist Menu" "---" "$(mpc --format "%date%$separator%album%" find albumartist "$ARTIST" | sort | uniq)") ALBUM_TEMP=$(printf "%s\n" "${ALBUM[@]}" | dmenu_t -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -filter "$albumartistalbumfilter" -select "$albumartistalbumentry" -dmenu -format "f¬s" -mesg "${HELP}" -p "Choose Album > ") val=$? ALBUM="${ALBUM_TEMP#*¬}" ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') export albumartistalbumfilter="${ALBUM_TEMP%¬*}" if [[ "$val" -eq 10 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc find albumartist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" | mpc add albumartistalbumentry="$ALBUM_FINAL" browseAlbumArtistAlbum elif [[ "$val" -eq 11 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc find albumartist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" | mpc insert albumartistalbumentry="$ALBUM_FINAL" browseAlbumArtistAlbum elif [[ "$val" -eq 12 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc clear mpc find albumartist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" | mpc add mpc play albumartistalbumentry="$ALBUM_FINAL" browseAlbumArtistAlbum elif [[ "$val" -eq 15 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc clear && mpc findadd albumartist "$ARTIST" album "${ALBUM_FINAL}" mpc searchplay "${ALBUM_FINAL}" albumartistalbumentry="$ALBUM_FINAL" browseAlbumArtistAlbum elif [[ "$val" -eq 13 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc findadd albumartist "$ARTIST" album "${ALBUM_FINAL}" mpc searchplay "${ALBUM_FINAL}" albumartistalbumentry="$ALBUM_FINAL" browseAlbumArtistAlbum elif [[ "$val" -eq 14 ]]; then export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') mpc find albumartist "$ARTIST" album "${ALBUM_FINAL}" | mpc insert mpc searchplay "${ALBUM_FINAL}" albumartistalbumentry="$ALBUM_FINAL" browseAlbumArtistAlbum elif [[ "$val" -eq 0 ]]; then if [[ "$ALBUM" == "0 Return to Artist Menu" ]]; then unset albumartistalbumfilter unset albumartistalbumentry browseAlbumArtist else export ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') export DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') albumartistalbumentry="$ALBUM_FINAL" browseAlbumArtistTrack fi elif [[ "$val" -eq 1 ]]; then exit fi exit } browseTrack() { HELP="${add}: Add (Default) | ${insert}: Insert | ${replace}: Replace ${addall}: Add & Play | ${insertall}: Insert & Play" TRACK=("0 Return to Album Menu" "---" "$(mpc --format "%track%$separator%title%" find artist "$ARTIST" album "$ALBUM_FINAL")") TRACK_TEMP=$(printf "%s\n" "${TRACK[@]}" | dmenu_t -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -filter "$trackfilter" -select "$trackentry" -dmenu -mesg "${HELP}" -format "f¬s" -p "Choose Track > ") val=$? TRACK="${TRACK_TEMP#*¬}" TRACKFINAL=$(echo "$TRACK" | awk -F "$separator" '{ print $2 }') export trackfilter="${TRACK_TEMP%¬*}" if [[ "$val" -eq 10 ]]; then mpc find artist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "$TRACKFINAL" | mpc add trackentry="$TRACK_TEMP" browseTrack elif [[ "$val" -eq 11 ]]; then mpc find artist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "$TRACKFINAL" | mpc insert trackentry="$TRACK_TEMP" browseTrack elif [[ "$val" -eq 12 ]]; then mpc clear mpc find artist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "$TRACKFINAL" | mpc add mpc play trackentry="$TRACK_TEMP" browseTrack elif [[ "$val" -eq 15 ]]; then mpc clear && mpc find artist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "${TRACKFINAL}" |mpc add mpc searchplay "${TRACKFINAL}" trackentry="$TRACK_TEMP" browseTrack elif [[ "$val" -eq 13 ]]; then mpc findadd artist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "${TRACKFINAL}" mpc searchplay "${TRACKFINAL}" trackentry="$TRACK_TEMP" browseTrack elif [[ "$val" -eq 14 ]]; then mpc find artist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "${TRACKFINAL}" | mpc insert mpc searchplay "${TRACKFINAL}" traclentry="$TRACK_TEMP" browseTrack elif [[ "$val" -eq 1 ]]; then exit elif [[ "$val" -eq 0 ]]; then if [[ "$TRACK" = "0 Return to Album Menu" ]]; then unset trackfilter browseAlbum else if [[ -z $(mpc playlist) ]]; then mpc find artist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "$TRACKFINAL" | mpc add && mpc play else mpc find artist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "$TRACKFINAL" | mpc add fi fi fi } browseArtist() { HELP="${add}: Add Artist | ${insert}: Insert Artist | ${replace}: Replace Artist ${addplay}: Add & Play | ${insertplay}: Insert & Play" ARTIST_TEMP=$(echo -e "0 Return to Browse Menu\n---\n$(mpc list artist)" | dmenu_t -mesg "${HELP}" -filter "$artistfilter" -select "$artistentry" -dmenu -format "f¬s" -p "Choose Artist > ") val=$? ARTIST="${ARTIST_TEMP#*¬}" ARTISTFINAL=$(echo "$ARTIST" | awk -F "$separator" '{ print $2 }') export artistfilter="${ARTIST_TEMP%¬*}" if [[ "$ARTIST" == "0 Return to Browse Menu" ]]; then unset artistfilter browseLibPrompt elif [[ "$val" -eq 0 ]]; then ARTIST="$ARTIST" artistentry="$ARTIST" browseAlbum elif [[ "$val" -eq 1 ]]; then exit elif [[ "$val" -eq 10 ]]; then mpc findadd artist "${ARTIST}" artistentry="$ARTIST" browseArtist elif [[ "$val" -eq 11 ]]; then mpc find artist "${ARTIST}" | mpc insert artistentry="$ARTIST" browseArtist elif [[ "$val" -eq 12 ]]; then mpc clear && mpc findadd artist "${ARTIST}" && mpc play artistentry="$ARTIST" browseArtist elif [[ "$val" -eq 13 ]]; then mpc findadd artist "${ARTIST}" mpc searchplay "${ARTIST}" artistentry="$ARTIST" browseArtist elif [[ "$val" -eq 14 ]]; then mpc find artist "${ARTIST}" | mpc insert mpc searchplay "${ARTIST}" artistentry="$ARTIST" browseArtist fi } browseAlbumArtistTrack() { HELP="${add}: Add (Default) | ${insert}: Insert | ${replace}: Replace Track ${addall}: Add & Play | ${insertplay}: Insert & Play" TRACK=("0 Return to Album Menu" "---" "$(mpc --format "%track%$separator%title%" find albumartist "$ARTIST" album "$ALBUM_FINAL")") TRACK_TEMP=$(printf "%s\n" "${TRACK[@]}" | dmenu_t -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -filter "$albumartisttrackfilter" -select "$albumartisttrackentry" -dmenu -mesg "${HELP}" -format "f¬s" -p "Choose Track > ") val=$? TRACK="${TRACK_TEMP#*¬}" TRACKFINAL=$(echo "$TRACK" | awk -F "$separator" '{ print $2 }') export albumartisttrackfilter="${TRACK_TEMP%¬*}" if [[ "$val" -eq 10 ]]; then mpc find albumartist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "$TRACKFINAL" | mpc add albumartisttrackentry="${TRACK}" browseAlbumArtistTrack elif [[ "$val" -eq 11 ]]; then mpc find albumartist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "$TRACKFINAL" | mpc insert albumartisttrackentry="${TRACK}" browseAlbumArtistTrack elif [[ "$val" -eq 12 ]]; then mpc clear mpc find albumartist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "$TRACKFINAL" | mpc add mpc play albumartisttrackentry="${TRACK}" browseAlbumArtistTrack elif [[ "$val" -eq 15 ]]; then mpc clear && mpc findadd albumartist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "${TRACKFINAL}" mpc searchplay "${TRACKFINAL}" albumartisttrackentry="${TRACK}" browseAlbumArtistTrack elif [[ "$val" -eq 13 ]]; then mpc findadd albumartist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "${TRACKFINAL}" mpc searchplay "${TRACKFINAL}" albumartisttrackentry="${TRACK}" browseAlbumArtistTrack elif [[ "$val" -eq 14 ]]; then mpc find albumartist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "${TRACKFINAL}" | mpc insert mpc searchplay "${TRACKFINAL}" albumartisttrackentry="${TRACK}" browseAlbumArtistTrack elif [[ "$val" -eq 1 ]]; then exit elif [[ "$val" -eq 0 ]]; then if [[ "$TRACK" = "0 Return to Album Menu" ]]; then unset albumartisttrackfilter browseAlbumArtistAlbum else if [[ -z $(mpc playlist) ]]; then mpc find albumartist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "$TRACKFINAL" | mpc add && mpc play else mpc find albumartist "$ARTIST" album "$ALBUM_FINAL" date "$DATE" title "$TRACKFINAL" | mpc add fi fi fi } browseAlbumArtist() { HELP="${add}: Add | ${insert}: Insert | ${replace}: Replace ${addplay}: Add & Play | ${insertplay}: Insert & Play" ARTIST_TEMP=$(echo -e "0 Return to Browse Menu\n---\n$(mpc list albumartist)" | dmenu_t -mesg "${HELP}" -filter "$albumartistfilter" -select "$albumartistentry" -dmenu -format "f¬s" -p "Choose Artist > ") val=$? ARTIST="${ARTIST_TEMP#*¬}" ARTISTFINAL=$(echo "$ARTIST" | awk -F "$separator" '{ print $2 }') export albumartistfilter="${ARTIST_TEMP%¬*}" if [[ "$ARTIST" == "0 Return to Browse Menu" ]]; then unset filter browseLibPrompt elif [[ "$val" -eq 0 ]]; then if [[ "$ARTIST" == "0 Return to Browse Menu" ]]; then unset albumartistfilter browseLibPrompt else albumartistentry="${ARTIST}" browseAlbumArtistAlbum fi elif [[ "$val" -eq 1 ]]; then exit elif [[ "$val" -eq 10 ]]; then mpc findadd albumartist "${ARTIST}" albumartistentry="${ARTIST}" browseAlbumArtist elif [[ "$val" -eq 11 ]]; then mpc find albumartist "${ARTIST}" | mpc insert albumartistentry="${ARTIST}" browseAlbumArtist elif [[ "$val" -eq 12 ]]; then mpc clear && mpc findadd artist "${ARTIST}" && mpc play albumartistentry="${ARTIST}" browseAlbumArtist elif [[ "$val" -eq 13 ]]; then mpc findadd albumartist "${ARTIST}" mpc searchplay "${ARTIST}" albumartistentry="${ARTIST}" browseAlbumArtist elif [[ "$val" -eq 14 ]]; then mpc find albumartist "${ARTIST}" | mpc insert mpc searchplay "${ARTIST}" albumartistentry="${ARTIST}" browseAlbumArtist fi } browseGenre () { help_text=$(echo -e "${add}: Add - ${insert}: Insert - ${replace}: Replace (Default)\n${rate}: Rate - ${addplay}: Add + Play - ${insertplay}: Insert + Play" | column -s '-' -t) line1=$(echo "${help_text}" | head -1) line2=$(echo "${help_text}" | tail -1) HELP="${line1} ${line2}" GENRE=$(echo -e "0 Return to Browser Menu\n---\n$(mpc list genre)" | rofi -dmenu -mesg "${HELP}" -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -p "Choose Genre > ") val=$? if [[ $val -eq 0 ]]; then if [[ $GENRE == "0 Return to Browser Menu" ]]; then browseLibPrompt else browseGenre2 fi elif [[ $val -eq 10 ]]; then mpc findadd genre "${GENRE}" browseGenre elif [[ $val -eq 11 ]]; then mpc find genre "${GENRE}" | mpc insert browseGenre elif [[ $val -eq 12 ]]; then mpc clear; mpc findadd genre "${GENRE}"; mpc play browseGenre elif [[ $val -eq 13 ]]; then mpc findadd genre "${GENRE}" mpc searchplay "${GENRE}" browseGenre elif [[ $val -eq 14 ]]; then mpc find genre "${GENRE}" | mpc insert mpc searchplay "${GENRE}" browseGenre fi } browseGenre2 () { HELP="${add}: Add, ${insert}: Insert, ${replace}: Replace (Default) ${addplay}: Add & Play, ${insertplay}: Insert & Play" ALBUM_TEMP=$(echo -e "< Return to Genre Browser\n---\n$(mpc -f "%date%$separator%albumartist%$separator%album%" search genre "$GENRE" | uniq | sort -r)" | rofi -custom-kb-1 "${add}" -custom-kb-2 "${insert}" -custom-kb-3 "${replace}" -custom-kb-4 "${addplay}" -custom-kb-5 "${insertplay}" -dmenu -select "$genreentry" -format "f¬s" -filter "$genrefilter" -mesg "${HELP}" -p "Choose Album > ") val=$? ALBUM="${ALBUM_TEMP#*¬}" export genrefilter="${ALBUM_TEMP%¬*}" ALBUM_FINAL=$(echo "$ALBUM" | awk -F "$separator" '{ print $3 }') DATE=$(echo "$ALBUM" | awk -F "$separator" '{ print $1 }') ARTIST=$(echo "$ALBUM" | awk -F "$separator" '{ print $2 }') unset line export line="${ALBUM_TEMP%¬*}" if [[ "$ALBUM" == "< Return to Genre Browser" ]]; then browseGenre fi if [[ "$val" -eq 10 ]]; then mpc find artist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" | mpc add genreentry="$ALBUM_FINAL" browseGenre2 elif [[ "$val" -eq 11 ]]; then mpc find artist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" | mpc insert genreentry="$ALBUM_FINAL" browseGenre2 elif [[ "$val" -eq 12 ]]; then mpc clear mpc find artist "$ARTIST" date "$DATE" album "$ALBUM_FINAL" | mpc add mpc play genreentry="$ALBUM_FINAL" browseGenre2 elif [[ "$val" -eq 13 ]]; then mpc findadd genre "$GENRE" mpc searchplay "${GENRE}" genreentry="$ALBUM_FINAL" browseGenre2 elif [[ "$val" -eq 14 ]]; then mpc find genre "$GENRE" | mpc insert mpc searchplay "${GENRE}" genreentry="$ALBUM_FINAL" browseGenre2 elif [[ $val -eq 1 ]]; then exit fi } saveAlbumToPlaylist() { declare -i seen=0 while read TRACK do seen=1 if [[ "$TRACK" == "0 Return to Main Menu" ]] then dplayPrompt else artist=$(echo "$TRACK" | awk -F "$separator" '{print $1}') date=$(echo "$TRACK" | awk -F "$separator" '{print $2}') album=$(echo "$TRACK" | awk -F "$separator" '{print $3}') mpc search date "$date" album "$album" albumartist "$artist" | clerk_helper saveto fi done < <(echo -e "0 Return to Main Menu\n---\n$(echo "$album_temp")" | dmenu_t -dmenu -p "Save Album to Playlist > ") if [[ $seen = 0 ]] then exit fi } saveLatestToPlaylist() { declare -i seen=0 while read TRACK do seen=1 if [[ "$TRACK" == "0 Return to Main Menu" ]] then dplayPrompt else artist=$(echo "$TRACK" | awk -F "$separator" '{print $2}') date=$(echo "$TRACK" | awk -F "$separator" '{print $1}') album=$(echo "$TRACK" | awk -F "$separator" '{print $3}') mpc search date "$date" album "$album" albumartist "$artist" | clerk_helper saveto fi done < <(echo -e "0 Return to Main Menu\n---\n$(echo "$last_temp")" | dmenu_t -dmenu -p "Save Album to Playlist > ") if [[ $seen = 0 ]] then exit fi } saveTrackToPlaylist() { declare -i seen=0 while read TRACK do seen=1 if [[ "$TRACK" == "0 Return to Main Menu" ]] then dplayPrompt else artist=$(echo "$TRACK" | awk -F "$separator" '{print $1}') track=$(echo "$TRACK" | awk -F "$separator" '{print $2}') album=$(echo "$TRACK" | awk -F "$separator" '{print $4}') title=$(echo "$TRACK" | awk -F "$separator" '{print $5}') mpc search track "$track" album "$album" title "$title" albumartist "$artist" | clerk_helper saveto fi done < <(echo -e "0 Return to Main Menu\n---\n$(echo "$tracks_temp")" | dmenu_t -dmenu -p "Save Track to Playlist > ") if [[ $seen = 0 ]] then exit fi } ################################################################################ function dmenu_t () { rofi -dmenu -z $(echo "$rofiopts") "$@" } while :; do case $1 in --add) if [[ ! $2 ]]; then echo "Missing argument for --add" echo "Possible values: track, album, latest" elif [[ $2 == track ]]; then AddTrackTags elif [[ $2 == album ]]; then AddAlbumTags elif [[ $2 == latest ]]; then addLastMod fi break ;; --rate) if [[ ! $2 ]]; then echo "Missing arguemtn for --rate" echo "Possible values: track, album, instant" echo "Launching rating menu" ratingPrompt elif [[ $2 == track ]]; then rateTrack elif [[ $2 == album ]]; then rateAlbum elif [[ $2 == instant ]]; then if [[ ! $3 ]]; then echo "Missing argument for --rate instant" echo "Please define rating between 1-10" else instantRateTrack $3 fi elif [[ $2 == load ]]; then mpc clear && mpc sticker "" find rating | grep -E "rating=6|rating=7|rating=8|rating=9|rating=10" | awk -F ':' '{print $1}' | $shuf -n $value | mpc add && mpc play fi break ;; --random) if [[ ! $2 ]]; then echo "Missing argument for --random" echo "Possible values: track, album" elif [[ $2 == track ]]; then playRandomTracks elif [[ $2 == album ]]; then playRandomAlbum fi break ;; --current) currentTag break ;; --browse) if [[ ! $2 ]]; then echo "Missing argument for --browse" echo "Possible values: artist, date, genre, folder, system" elif [[ $2 == artist ]]; then browseArtist elif [[ $2 == albumartist ]]; then browseAlbumArtist elif [[ $2 == date ]]; then browseDate elif [[ $2 == genre ]]; then browseGenre elif [[ $2 == system ]]; then browseFilesystem elif [[ $2 == folder ]]; then browseFolders fi break ;; --backup) if [[ ! $2 ]]; then echo "Missing argument for --backup" echo "Possible values: track, album" elif [[ $2 == track ]]; then backupTrackRatings elif [[ $2 == album ]]; then backupAlbumRatings fi break ;; --restore) if [[ ! $2 ]]; then echo "Missing argument for --restore" echo "Possible values: track, album" elif [[ $2 == track ]]; then restoreTrackRatings elif [[ $2 == album ]]; then restoreAlbumRatings fi break ;; --update) updateCache break ;; --queue) if [[ ! $2 ]]; then echo "Missing argument for --queue" echo "Possible values: show, delete, suspend, resume" elif [[ $2 == show ]]; then dplayQueue elif [[ $2 == delete ]]; then dplayQueueDelete elif [[ $2 == suspend ]]; then suspendPlaylist elif [[ $2 == resume ]]; then resumePlaylist fi break ;; --rss) loadRSS break ;; --manage) managePlaylists ;; --playlist) if [[ $2 == savealbum ]]; then saveAlbumToPlaylist elif [[ $2 == savelast ]]; then saveLatestToPlaylist elif [[ $2 == savetrack ]]; then saveTrackToPlaylist fi break ;; --lastfm) if [[ ! $2 ]]; then echo "Missing argument for --lastfm" echo "Possible values: toggle, check, love" elif [[ $2 == toggle ]]; then lastFM elif [[ $2 == check ]]; then lastFMCheck elif [[ $2 == love ]]; then if [[ scrobbler=mpdscribble ]]; then lastfm-mpd-cli love > /dev/null && notify-send "MPD" "Loved $(mpc current -f '%title%') on LastFM" elif [[ scrobbler=mpdas ]]; then mpc sendmessage mpdas love fi fi break ;; --help|-h) echo "---" echo "clerk: rofi/dmenu based MPD Interface" echo "Copyright © 2013 - 2015 Rasmus Steinke" echo "---" echo "General" echo " --help, -h this help message" echo " --current show currently playing track" echo " --update update album/track caches" echo "" echo "Library" echo " --add adds selection at the end of the queue" echo " --browse browse library" echo " --random play random track or album" echo "" echo "Playlist" echo " --queue manage current queue" echo " --manage manage playlists" echo " --playlist save selection to playlist \"clerk\"" echo " --rss load podcast" echo " (podcast should be placed in ~/.config/clerk/podcasts" echo " with format NAME \ URL)" echo "" echo "Ratings" echo " --rate rate albums or tracks" echo " "load" adds random rated tracks to queue" echo "" echo " --backup backup ratings from mpd sticker database to json file." echo " --restore restore ratings back to mpd sticker database" echo "" echo "LastFM" echo " --lastfm toggle or check last.fm status, love current track" break ;; *) dplayPrompt ;; esac shift done