#! /usr/bin/env bash
#
# i3-settree
# Restore an i3 layout using a list of saved trees
# Copyright (C) 2018 salt <salt@lap-th-e560-0>
#
# Distributed under terms of the MIT license.
#

# Pedantry
# shellcheck disable=1117

# Globals
name="$(basename -- "$0")"
treespath="${XDG_CONFIG_DIR:-$HOME/.config}/i3/trees"

# Helpers
log() {
	[ -z "$1" ] && return 1
	printf "%s: %s\n" \
		"$name" \
		"$1"
	return 0
}
prepare_input() {
	options=""
	for file in "$treespath"/*; do
		if ! [ -r "$file" ]; then
			# Cannot read the file; skip it
			continue
		fi
		if ! [[ "$file" = *".json" ]]; then
			# File is not a json structure; skip it
			continue
		fi
		if ! [ -f "$file" ]; then
			# Glob failed and no files exist; error
			return 1
		fi
		if [ -z "$options" ]; then
			options="$(basename -s ".json" -- "$file")"
		else
			options="$options
$(basename -s ".json" -- "$file")"
		fi
	done
	printf "%s" \
		"$options"
	return 0
}

# Steps
step_validate_deps() {
	# Ensure we have all the programs we need to do this
	for dep in rofi i3-msg; do
		if ! command -v "$dep" > /dev/null 2>&1; then
			log "Could not validate dependency \"$dep\""
			return 1
		fi
	done
	return 0
}
step_validate_files() {
	# Ensure we actually have tree files to set
	if ! [ -d "$treespath" ]; then
		mkdir -p "$treespath" > /dev/null 2>&1
		if [ -d "$treespath" ]; then
			log "Trees path was created: $treespath"
			log "Put your modified i3-save-tree trees there"
			return 1
		else
			log "Failed to create trees path: $treespath"
			log "Are permissions set correctly? Is the filesystem mounted read-write?"
			return 1
		fi
	fi
	return 0
}
step_get_input() {
	# Get the user input, store it in $choice
	choice="$(rofi -dmenu -p "i3 tree" <<< "$(prepare_input)")"
	choicefile="$treespath/$choice.json"
	if [ -z "$choice" ]; then
		log "User cancelled the operation"
		return 1
	else
		log "Choice: $choice"
		log "Choicefile: $choicefile"
	fi
}
step_act() {
	# Do the do
	# Using a case statement here in case I want to implement saving
	# functionality later
	case "$choice" in
		*)
			i3-msg "append_layout $choicefile" > /dev/null 2>&1
			if [ -r "$choicefile.rc" ]; then
				log "Running $choicefile.rc"
				"$choicefile".rc
			fi
			return $!
			;;
	esac
}

# Main
main() {
	step_validate_deps || return 50
	step_validate_files || return 51
	step_get_input || return 52
	step_act
	return $!
}

main "$@"