summaryrefslogtreecommitdiffstats
path: root/.displayinit
blob: 3e96b6cd8840c8bc255072b9d1ddb56d854f6597 (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
#!/bin/sh

run_parts()
{
	local level="${1}"
	local action="${2}"
	local i=

	if [ "x${level}" = 'x' ]; then
		level="${HOME}/.displayinit.d/"
	else
		level="${HOME}/.displayrc.d/${level}"
	fi
	for i in "${level}"*; do
		if [ -x "${i}" ]; then
			if ! "${i}" "${action}"; then
				return 1
			fi
		fi
	done

	return 0
}

main()
{
	local action=

	if [ ${#} -ne 1 ]; then
		usage >&2
		return 1
	fi
	action="${1}"

	# Enable services on first run.
	if ! [ -d "${HOME}/.displayrc.d" ]; then
		mkdir "${HOME}/.displayrc.d"
		if ! run_parts 'enable'; then
			return 1
		fi
	fi
	
	# Start the session.
	if ! run_parts "${action}"; then
		return 1
	fi

	# Main loop.
	rm -f ~/.displayrc.fifo
	mkfifo -m 0600 ~/.displayrc.fifo
	while read action; do
		case "${action}" in
			'reload')
				run_parts "${action}"
				;;
			'stop')
				run_parts "${action}"
				break
				;;
		esac
	done <~/.displayrc.fifo
	rm -f ~/.displayrc.fifo
}

main "${@}"