summaryrefslogtreecommitdiff
path: root/backend/backend.sh.in
blob: e4613872089a09765c95a7f3912d8243d51dd149 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash

cd "$(dirname "$0")"
SEARCHPATH="@DATA_INSTALL_DIR@"
[ ! -e "$SEARCHPATH" -a -e ./modules ] && SEARCHPATH="."

for i in $(find $SEARCHPATH/modules/ -type f | sort); do
	source $i
done

function print_usage()
{
cat <<EOF >&2
$(basename $0):

 -ni
        NonInteractive mode, no prompt is shown and INSTALLER_OUTPUT is set to 1
 -e command
        Run "command"

EOF
exit 0
}

function set_debug()
{
    case $1 in
    on)
        PS4="<acritoxinstaller debug> "
        set -x
        ;;
    off)
        set +x
        PS4="+"
        ;;
    esac
}

# Synopsis: get_root ["X"]
#
# This function makes sure the user has root-privileges.
# Argument X means it won't use "sudo" to gain root-privileges, because sudo doesn't support X-applications
function get_root()
{
	if ((UID)); then
		p="$PWD/$(basename "$0")"
		if [ -x /usr/bin/sudo -a "$1" != "X" ]; then
			if /usr/bin/sudo -n -l "$p" &>/dev/null; then
				/usr/bin/sudo "$p" "${O_ARGS[@]}"
				exit $?
			fi
		fi
		if [ -x /usr/lib/kde4/libexec/kdesu ]; then
			/usr/lib/kde4/libexec/kdesu --noignorebutton -d -- "$p" --pid $$ "${O_ARGS[@]}" &>/dev/null
			exit $?
		fi
		if [ -z "$as_root" ]; then
			for as_root in /usr/bin/kdesu /usr/bin/gksu exec
			do
				[ -x $as_root ] && break
			done
		fi
		$as_root "$p" --pid $$ "${O_ARGS[@]}" &>/dev/null
		exit $?
	fi
}

O_ARGS=( "${@}" )
while [ "$1" ]
do
	case $1 in
	"-ni")
		export INSTALLER_OUTPUT=1
		;;
	"-e")
		shift
		eval "$@"
		exit $?
		;;
	"--pid")
		exec 0<"/proc/$2/fd/0" 1>"/proc/$2/fd/1" 2>"/proc/$2/fd/2"
		shift
		;;
	"-h")
		print_usage
		;;
	esac
	shift
done

get_root

EOT="$(echo -e '\004')"
while true
do
	if ((INSTALLER_OUTPUT)); then
	        echo "<acritoxinstaller prompt>"
		IFS=" " read -d "$EOT" -r command params
	else
		IFS=" " read -er -p "installer> " command params
	fi
	declare -F ${command}_pre &>/dev/null && eval "${command}_pre"
	eval "$command $params"
	declare -F ${command}_post &>/dev/null && eval "${command}_post"
done