aboutsummaryrefslogtreecommitdiff
path: root/install.sh
blob: 75f4c3cf986035b69046ead894be581dff4c459d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash

function fail(){
	EXCODE=$1
	shift
	echo "$@" >&2
	exit "$EXCODE"
}

WORKDIR=$(realpath "$(dirname "$0")")
cd "$WORKDIR" || fail 1 "The working directory could not be determined."

# For hooks
export WORKDIR

# === CODE BELOW HERE ===

for f in lib/*.sh; do
	source "$f" || fail 1 "Failed to load $f"
done

debug "Working in $WORKDIR"
debug "Homedir is $HOME"

if [ ! -f "config.csv" ] || [ ! -f "sets.csv" ]; then
	# TODO Create them
	fail 1 "Configuration files do not exist"
fi

#['name']='install location relative to $HOME'
declare -A CONFIGS
while IFS=";" read -r CFG DEST _; do
	CONFIGS[$CFG]="$DEST"
done < config.csv
unset CFG DEST

#['name']='list of keys of CONFIGS (or SETS; Beware of BRB)'
declare -A SETS
while IFS=";" read -r SET PKGS _; do
	SETS[$SET]="$PKGS"
done < sets.csv
unset SET PKGS

if [ $# -eq 0 ]; then
	print_help "$0"
	exit 1
fi

CMD=$1
shift

case $CMD in
	install)
		if [ $# -gt 0 ]; then
			selected=( "$@" )
		else
			selected=( $(multiselector "${!CONFIGS[@]}" "${!SETS[@]}" ) )
		fi
		for cnf in "${selected[@]}"; do
			choose_target "$cnf"
		done

		call_hook housekeeping;;
	hook)
		call_hook "$1";;
	add)
		test -e "$1" || fail 1 "Target file not found: $1"

		TARGET=$(realpath "--relative-to=$HOME" "$1")
		NAME=$(basename "$TARGET")
		RELPATH=$(dirname "$TARGET")

		# NOTE: This does not check SETS. Could be a problem
		test -n "${CONFIGS[$NAME]}" && fail 1 "An object with the same name is already managed."

		cp -r "$1" ./ || fail 1 "Failed to copy configuration"
		echo "$NAME;$RELPATH" >> config.csv

		call_hook post_add "$NAME" "$TARGET"

		echo "Config was installed successfully."
		echo "It can now be installed with $0 install $NAME"
		echo "The following files were changed: config.csv $NAME"
		;;
	*)
		echo Invalid command: "$CMD"
		exit 1
		;;
esac