#!/bin/bash

# Copyright © 2012-2026 ScriptFanix
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# A copy of the GNU General Public License v3 is includded in the LICENSE file
# at the root of the project.

setupSource() {
	cat <<-EODesc

[Source]
    Here we will define which directory AtOM should look for media files, and
    what it should completely ignore.
	EODesc
	cat <<-EODesc

		    Path (path):
		        Which directory to scan for new media files.
	EODesc
	comeagain() {
		read							\
			-e						\
			-i"${sourcepath:-/var/lib/mpd/music}"		\
			-p'Music collection (<TAB> for completion): '	\
			sourcepath
		# Reject paths that don't exist; AtOM doesn't create the source
		if ! [ -d "$sourcepath" ]
		then
			echo "$sourcepath does not exist or is not a"	\
				"directory!" >&2
			comeagain
		fi
	}
	comeagain
	cat <<-EODesc

    Skip (path):
        Files in these directories will be ignored.
        Path is relative to $sourcepath.

        This prompt will loop until an empty string is encountered.
	EODesc
	# Change into the source directory so readline tab-completion resolves
	# relative paths correctly while the user types skip entries
	cd "$sourcepath"
	# Remember how many skip entries already exist so we know when to stop
	# re-presenting existing entries vs. treating blanks as "done"
	count=${#skippeddirectories[@]}
	for (( i=0 ; 1 ; i++ ))
	do
		read							\
			-e						\
			# Pre-fill with the existing entry at this index, if any
			${skippeddirectories[i]+-i"${skippeddirectories[i]}"}\
			-p'Skip: '					\
			value
		if [ -n "$value" ]
		then
			skippeddirectories[i]="$value"
		elif (( i < count ))
		then
			# Blank input on a previously set entry removes it
			unset skippeddirectories[i]
		else
			# Blank input beyond the old list signals end of input
			break
		fi
	done
	unset count
	# Return to the prior directory; output suppressed to keep the UI clean
	cd - >/dev/null
}
