#!/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.

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
	# Check input is an integer
	# Reused across multiple prompts in this function
	expr='^[0-9]*$'
	comeagain() {
		read					\
			-e				\
			-p'Target load: (integer) '	\
			# Pre-fill with the existing value when re-running setup
			${maxload+-i"$maxload"}		\
			value
		if [ -n "$value" ] && [[ $value =~ $expr ]]
		then
			maxload="$value"
		else
			echo "Invalid max-load value: $value" >&2
			# Recurse until we get a valid value
			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] '	\
			# Default to class 3 (idle) when no prior value exists
			-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)
				# Best-effort class; niceness 0-7 is mandatory
				if [ -n "$niceness" ]		\
				&& (( niceness >= 0 && niceness <= 7 ))
				then
					ionice="ionice -c2 -n$niceness "
				else
					echo "Invalid IO priority"\
					"'$niceness'" >&2
					comeagain
				fi
			;;
			3)
				# Idle class; no niceness level is accepted
				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
	# Tab-completion (-e) works here because readline is active
	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
}
