From 83058754bb745968b0ddd69d578e635294aa01df Mon Sep 17 00:00:00 2001
From: Salt <rehashedsalt@cock.li>
Date: Fri, 14 Jun 2019 06:11:02 -0500
Subject: [PATCH] jptgdp: Add rofi menu, autoplay option

---
 jptgdp | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/jptgdp b/jptgdp
index b5071c3..067f643 100755
--- a/jptgdp
+++ b/jptgdp
@@ -41,6 +41,10 @@ validatedeps() {
 	done
 	return 0
 }
+sanitize() {
+	[ -z "$1" ] && return 1
+	_return="${1//[^ a-zA-Z0-9\[\]|()_-]/}"
+}
 
 # Traps
 trapexit() {
@@ -64,6 +68,7 @@ files are cached in your Music folder under "JPTGDP Songs" for offline use.
 
   -c			Clears the cache (which can become quite large)
   -d			Download only; don't queue anything up
+  -p			Play the playlist after it is enqueued
   -s			Shuffle the playlist
 
   -r <directory>	Start up rofi, if installed, and present a listing of
@@ -77,7 +82,23 @@ EOF
 }
 rofimenu() {
 	validatedeps rofi || error "$_return is not currently installed" 1
-	error "Not yet implemented" 1
+	[ -d "$_optrofidir" ] || error "Could not open directory \"$_optrofidir\"" 2
+	files=$(find "$_optrofidir" -type f -name \*.gdp)
+	if [ -n "$files" ]; then
+		# Strip file suffixes for a cleaner menu
+		playlists=""
+		while read file; do
+			filebase="$(basename -- "$file")"
+			filebase="${filebase%.gdp}"
+			[ -n "$playlists" ] && playlists+=$'\n'
+			playlists+="$filebase"
+		done <<< "$files"
+	else
+		error "No playlists found" 61
+	fi
+	choice="$(rofi -dmenu -p "$_name" <<< "$playlists")"
+	[ -z "$choice" ] && error "User aborted at selection" 62
+	playlist "$_optrofidir"/"$choice".gdp
 	return 0
 }
 playlist() {
@@ -87,7 +108,8 @@ playlist() {
 	while read line; do
 		[ -z "$line" ] && continue
 		rm "$_tmpfile"* > /dev/null 2>&1
-		filename="$_musicdir/${line//[^ a-zA-Z0-9\[\]|()_-]/}"
+		sanitize "$line"
+		filename="$_musicdir/$_return"
 		if ! [ -f "$filename" ]; then
 			log "Finding a song for \"$line\""
 			youtube-dl \
@@ -106,6 +128,10 @@ playlist() {
 			fi
 		fi
 		[ -z "$_optdownloadonly" ] && audacious -e "$filename"
+		if [ -n "$_optautoplay" ]; then
+			audacious -p
+			unset _optautoplay
+		fi
 	done < <(if [ -n "$_optshuffle" ]; then shuf "$1"; else cat "$1"; fi)
 	if [ -z "$_optdownloadonly" ]; then
 		log "Finished building queue"
@@ -123,7 +149,7 @@ main() {
 	trap trapexit EXIT
 
 	# Actual program stuff
-	while getopts ":cdf:r:sh" opt; do
+	while getopts ":cdf:pr:sh" opt; do
 		case $opt in
 			c)
 				clearcache
@@ -135,8 +161,12 @@ main() {
 			f)
 				_optfile="$OPTARG"
 				;;
+			p)
+				_optautoplay=1
+				;;
 			r)
 				_optrofi=1
+				_optrofidir="$OPTARG"
 				;;
 			s)
 				_optshuffle=1