#!/bin/bash

# written by viniciorl (https://viniciorl.xyz/) for https://archive.lolicore.net/
# once you have changed the sloc variable the first thing you should run is lolicore update

# change sloc value to specify where song list should be located
sloc="$HOME/.lolicorelist"

function updatecheck() {

echo "
################################################################################################
## This script is no longer supported by viniciorl, scream at the lolicore people for updates ##
################################################################################################
"

}

case $1 in

play)

if [ "$2" == "-s" ]
then

	song="$(shuf -n 1 "$sloc")"
	echo "Playing $(echo $song | cut -d/ -f4-)
"
	mpv $3 "$(echo "$song" | sed 's/%/%25/g;s/ /%20/g;s/\[/%5B/g;s/\]/%5D/g;s/</%3C/g;s/>/%3E/g;s/#/%23/g;s/{/%7B/g;s/}/%7D/g;s/|/%7C/g;s/\\/%5C/g;s/\^/%5E/g;s/~/%7E/g;s/`/%60/g;s/\;/%3B/g;s/?/%3F/g;s/@/%40/g;s/=/%3D/g;s/&/%26/g;s/\$/%24/g')"

else

	song="$(grep -i "$2" $sloc | shuf -n 1)"
        echo "Playing $(echo $song | cut -d/ -f4-)
"
        mpv $3 "$(echo "$song" | sed 's/%/%25/g;s/ /%20/g;s/\[/%5B/g;s/\]/%5D/g;s/</%3C/g;s/>/%3E/g;s/#/%23/g;s/{/%7B/g;s/}/%7D/g;s/|/%7C/g;s/\\/%5C/g;s/\^/%5E/g;s/~/%7E/g;s/`/%60/g;s/\;/%3B/g;s/?/%3F/g;s/@/%40/g;s/=/%3D/g;s/&/%26/g;s/\$/%24/g')"

fi

;;

playlist)

if [ "$2" == "-n" ]
then

	song="$(shuf -n $3 $sloc)"
	echo "Playing randomly generated playlist with $3 items...
	"
	mpv $4 $(echo "$song" | shuf | sed 's/%/%25/g;s/ /%20/g;s/\[/%5B/g;s/\]/%5D/g;s/</%3C/g;s/>/%3E/g;s/#/%23/g;s/{/%7B/g;s/}/%7D/g;s/|/%7C/g;s/\\/%5C/g;s/\^/%5E/g;s/~/%7E/g;s/`/%60/g;s/\;/%3B/g;s/?/%3F/g;s/@/%40/g;s/=/%3D/g;s/&/%26/g;s/\$/%24/g' | tr '\n' ' ')

else

	song="$(grep -i "$2" $sloc)"
	echo "Playing:
$(grep -i "$2" $sloc | cut -d/ -f4-)
"
	mpv $3 $(echo "$song" | shuf | sed 's/%/%25/g;s/ /%20/g;s/\[/%5B/g;s/\]/%5D/g;s/</%3C/g;s/>/%3E/g;s/#/%23/g;s/{/%7B/g;s/}/%7D/g;s/|/%7C/g;s/\\/%5C/g;s/\^/%5E/g;s/~/%7E/g;s/`/%60/g;s/\;/%3B/g;s/?/%3F/g;s/@/%40/g;s/=/%3D/g;s/&/%26/g;s/\$/%24/g' | tr '\n' ' ')

fi

;;

search)

if [ "$2" != "" ]
then
	grep -i "$2" $sloc |\
	while read line
	do
	echo "$(echo "$line" | sed 's/%/%25/g;s/ /%20/g;s/\[/%5B/g;s/\]/%5D/g;s/</%3C/g;s/>/%3E/g;s/#/%23/g;s/{/%7B/g;s/}/%7D/g;s/|/%7C/g;s/\\/%5C/g;s/\^/%5E/g;s/~/%7E/g;s/`/%60/g;s/\;/%3B/g;s/?/%3F/g;s/@/%40/g;s/=/%3D/g;s/&/%26/g;s/\$/%24/g') | $(echo $line | rev | cut -d/ -f1 | rev)"
	done
else
	echo "Missing search pattern
USAGE: lolicore search <pattern>"
fi

;;

update)

	curl https://archive.lolicore.net/filelist.txt > "$sloc"

updatecheck

;;

help)

echo "USAGE: lolicore <command> <arguments>

COMMANDS
play <search> <mpv options> - plays a random song from the file list in mpv
                              if <search> is used it plays a random song that matches <search> anywhere on its file path (not case sensitive)
			      <mpv options> are passed to mpv when it plays the file.
                              if you want to pass an option to mpv without searching use -s on the search field
  Examples:
    lolicore play noise --volume=1 - plays a random file that contains noise on its file path and plays it in mpv with the --volume=1 option to not blow my ears out
    lolicore play \"goretrance x\" - plays a random file that contains goretrance x on its file path
    lolicore play -s \"--no-audio-display --volume=50\" - plays a random file on mpv with the options --no-audio-display and --volume=50

playlist <options/search> <mpv options> - adds all of the files that match <search> anywhere on their filename to an mpv playlist and plays them on mpv
                                          if -n option is used the script will generate a random playlist with the amount of files specified
					  while using -n formatting changes to 
					  playlist -n <number of files to add to playlist> <mpv options>
  Examples:
    lolicore playlist -n 10 --volume=20 - generates a playlist with 10 files randomly selected with mpv at the volume set to 20
    lolicore playlist hardcore --force-window=immediate - generates a playlist with all of the files that have hardcore on their file path and forces mpv to spawn a window

search <pattern> - shows files that match <pattern> in file list anywhere on its path.

update - updates the file list and checks for script updates.

help - displays this message and checks for script updates."

updatecheck

;;

*)

echo "USAGE: lolicore <command> <arguments>

COMMANDS
play <search> <mpv options> - plays a file
playlist <options/search> <mpv options> - generates and plays a playlist
search <pattern> - searches file list
update - updates the file list
help - run this for more information"

updatecheck

;;

esac
