#!/bin/bash

setupGeneral() {
	cat <<-EODesc

[General]
    We will start by setting the general parameters defining the program's
    behavior.
	EODesc
	cat <<-EODesc

    Target load (integer):
        Defines how parallel processing will behave. AtOM will try to keep the
        1 minute load average between <load> and <load>+1 by adjusting
       concurrency.  Initial concurrency will be set to half of that value.
	EODesc
	expr='^[0-9]*$'
	comeagain() {
		read					\
			-e				\
			-p'Target load: (integer) '	\
			${maxload+-i"$maxload"}		\
			value
		if [ -n "$value" ] && [[ $value =~ $expr ]]
		then
			maxload="$value"
		else
			echo "Invalid max-load value: $value" >&2
			comeagain
		fi
	}
	comeagain
	cat <<-EODesc

    Load interval (seconds, integer):
        How often should we check the load average and adjust concurrency. Set
        this too low, and concurrency may be increased too quickly. Set this
        too high, and AtOM will not adapt quickly enough to load increase. In
        both cases, your hard drive will suffer. In my experience, 30 seconds
        is a good value.
	EODesc
	comeagain() {
		read					\
			-e				\
			-p'Load interval: (integer) '	\
			-i${loadinterval:-30}		\
			value
		if [[ $value =~ $expr ]]
		then
			loadinterval="$value"
		else
			echo "Invalid load-interval value: $value" >&2
			comeagain
		fi
	}
	comeagain
	cat <<-EODesc

    Ionice <class> [niceness]:
        IO-hungry processes will be run with ionice class <class> and niceness
        [niceness] (if applicable). See man ionice for details.
	EODesc
	comeagain() {
		read					\
			-e				\
			-p'Ionice: <1-3> [0-7] '	\
			-i"${class:-3} ${niceness}"	\
			class niceness
		case $class in
			1)
				# real-time class, only root can do that
				if (( UID ))
				then
					echo "IO class 'realtime' is"\
					"not available to unprivileged"\
					"users" >&2
					comeagain
				fi
				if [ -n "$niceness" ]		\
				&& (( niceness >= 0 && niceness <= 7 ))
				then
					ionice="ionice -c1 -n$niceness "
				else
					echo "Invalid IO priority"\
					"'$niceness'" >&2
					comeagain
				fi
			;;
			2)
				if [ -n "$niceness" ]		\
				&& (( niceness >= 0 && niceness <= 7 ))
				then
					ionice="ionice -c2 -n$niceness "
				else
					echo "Invalid IO priority"\
					"'$niceness'" >&2
					comeagain
				fi
			;;
			3)
				ionice="ionice -c3 "
			;;
			*)
				echo "Invalid ionice parameters $value"\
				>&2
				comeagain
			;;
		esac
	}
	comeagain
	cat <<-EODesc

    Temporary Directory (path):
        Name speaks for itself: this is where FIFOs (for communicating with
        sqlite) and temporary WAVE files will be created.  Note that debug logs
        (if enabled) will go there too.
	EODesc
	read							\
		-e						\
		-i"${tempdir:-$HOME/.atom/tmp}"			\
		-p'Temporary directory (<TAB> for completion): '\
		tempdir
	cat <<-EODesc

		    Database (filename):
	EODesc
	read							\
		-e						\
		-i"${database:-$HOME/.atom/atom.db}"		\
		-p'Database file (<TAB> for completion): '	\
		database
}
