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

getConfig() {
	# Read the config file line by line; 'key' gets the first word, 'value'
	# the rest
	while read key value
	do
		case $key in
			'#'*)
				#comment - skip comment lines
			;;
			'')
				#empty line - skip blank lines
			;;
			'[general]')
				# Switch parsing context to the [general] section
				context=General
			;;
			'[source]')
				# Switch parsing context to the [source] section
				context=Source
			;;
			\[*\])
				# Any other [section] header is a destination name
				context=Destination
				destination="${key#[}"
				destination="${destination%]}"
				destinations+=("${destination%]}")	# Append to list of destinations
			;;
			*)
				# Dispatch key=value to the handler for the current section context
				getConfig$context
			;;
		esac
	done < "$cffile"
}
