#!/usr/bin/env bash

# Copyright (c) 2018 rehashedsalt/vintagesalt
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

## Clean up some variables
# General
unset TERMINAL
# Specific to these dtfscripts
unset DTF_SERVICES
unset DTF_TASKS
# Polybar
unset PB_MONITOR
unset PB_WM_RESTACK
unset PB_BAR_PRIMARY_MODULES_LEFT PB_BAR_PRIMARY_MODULES_CENTER PB_BAR_PRIMARY_MODULES_RIGHT
unset PB_BAR_PRIMARY_2_MODULES_LEFT PB_BAR_PRIMARY_2_MODULES_CENTER PB_BAR_PRIMARY_2_MODULES_RIGHT
unset PB_BAR_SECONDARY_MODULES_LEFT PB_BAR_SECONDARY_MODULES_CENTER PB_BAR_SECONDARY_MODULES_RIGHT
unset PB_BAR_SECONDARY_2_MODULES_LEFT PB_BAR_SECONDARY_2_MODULES_CENTER PB_BAR_SECONDARY_2_MODULES_RIGHT
unset PB_MODULE_BAR_WIDTH
unset PB_MODULE_ETH_INTERFACE
unset PB_MODULE_WLAN_INTERFACE
# bspwm monitorset.sh
unset BSPWM_BORDER_WIDTH
unset BSPWM_DESKTOPS_TOTAL
unset BSPWM_DESKTOPS_PRIMARY
unset BSPWM_WINDOW_GAP

function dtf_log() {
	name="$(basename $0 .sh)"
	if [ -z ${1+x} ]; then return 1; fi
	printf "$name: $1\n"
}

## Patch some directories
if [[ "$PATH" != *"$HOME/.local/bin"* ]]; then
	dtf_log "Patching path for $HOME/.local/bin"
	export PATH="$HOME/.local/bin:$PATH"
fi
if [[ "$XDG_CONFIG_HOME" == "" ]]; then
	dtf_log "Correcting configuration directory"
	export XDG_CONFIG_HOME="$HOME/.config"
fi
if [[ "$XDG_RUNTIME_DIR" == "" ]]; then
	dtf_log "Correcting runtime directory"
	export XDG_RUNTIME_DIR="/run/user/$UID"
fi

## Set some more variables
export DTF_CONFIG_HOME="$XDG_CONFIG_HOME/dtfscripts"
export DTF_SERVICES_DIR="$DTF_CONFIG_HOME/services"
export DTF_TASKS_DIR="$DTF_CONFIG_HOME/tasks"

## Load device-specific scripts
if [[ -x "$DTF_CONFIG_HOME/devices/$(hostname).rc" ]]; then
	dtf_log "Sourcing devices/$(hostname).rc"
	source $HOME/.config/dtfscripts/devices/$(hostname).rc
fi

## Determine which tasks to run
# If DTF_TASKS is set, use that
# Otherwise, run everything
if [[ "$DTF_TASKS" == "" ]]; then
	dtf_log "Performing all tasks"
	for task in $DTF_TASKS_DIR/*; do
		export DTF_TASKS="$DTF_TASKS $(basename "$task")"
	done
else
	dtf_log "Performing a subset of all tasks"
fi

## Define (and export) a task run function
function dtf_task_run() {
	[ -z ${1+x} ] && return 1
	task_full="$DTF_TASKS_DIR/$1"
	(
	if ! [ -r "$task_full" ]; then
		dtf_log "Task is unreadable or missing: $task"
		exit 1
	fi
	if ! [ -x "$task_full" ]; then
		dtf_log "Task is unexecutable: $task"
		exit 1
	fi
	source "$DTF_CONFIG_HOME/common_task.sh"
	source "$task_full"
	)
}
export -f dtf_task_run

## Run tasks
for task in $DTF_TASKS; do
	dtf_task_run $task
done

## Determine which services to run
# Same as above
if [[ "$DTF_SERVICES" == "" ]]; then
	dtf_log "Starting all available services"
	for service in $DTF_SERVICES_DIR/*.dtf; do
		export DTF_SERVICES="$DTF_SERVICES $(basename "$service")"
	done
else
	dtf_log "Starting a subset of available services"
fi

## Define (and export) service functions
function dtf_service_validate() {
	service_full="$DTF_SERVICES_DIR/$1"
	if ! [ -r "$service_full" ]; then
		dtf_log "Service is unreadable or missing: $1"
		return 1
	fi
	if ! [ -x "$service_full" ]; then
		dtf_log "Service is unexecutable: $1"
		return 1
	fi
	return 0
}
export -f dtf_service_validate

function dtf_service_start() {
	service_full="$DTF_SERVICES_DIR/$1"
	dtf_service_validate $1 || return 1
	(
	source "$DTF_CONFIG_HOME/common_service.sh"
	source "$service_full"
	isup && [[ "$service_kill_on_reload" == "true" ]] && stop
	prestart || exit 1
	prestart-extra || exit 1
	start
	start-extra
	)
}
export -f dtf_service_start

## Run startup scripts
for service in $DTF_SERVICES; do
	dtf_service_start $service
done

dtf_log "Finished initialization"