aboutsummaryrefslogtreecommitdiffstats
path: root/packages/altboot/files/altbootctl
blob: 58976afd0dfad817dc071a69108548446f3d34f1 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#! /bin/sh
#
# Copyright Matthias Hentges <devel@hentges.net> (c) 2006
# License: GPL (see http://www.gnu.org/licenses/gpl.txt for a copy of the license)
#
# Filename: hentges-setup.sh
# Date: 03-Jun-06

die() {
	echo -e "$*"
	exit 1
}


PERMANENT_CFG="/etc/altboot-`uname -r | cut -c1-3`.cfg"
TEMPORARY_CFG="/etc/altboot-`uname -r | cut -c1-3`.cfg.next-reboot"

CFG="$PERMANENT_CFG"
SCRIPT_CONFIG="/etc/altbootctl.conf"

test -e "$SCRIPT_CONFIG" && . "$SCRIPT_CONFIG" || die "$SCRIPT_CONFIG not found"

# $1= Setting, $2 = new value
set_pref() {
	
	if ( cat "$CFG" | grep -q "$1" )
	then
		cat "$CFG" | sed "/^$1/s/\(.*\)\=\(.*\)/\1\=\"$2\"/" > ${CFG}_
		mv ${CFG}_  ${CFG}
	else
		echo "$1=\"$2\"" >> "${CFG}"
	fi
}

# $1= Setting
get_pref() {
	n="$1"
	value="`cat "${CFG}" | sed -n "/^$n/s/\(.*\)\=\\"\(.*\)\\"/\2/p"`"
	#echo "cat "${CFG}" | sed -n "/^$n/s/\(.*\)\=\"\(.*\)\"/\2/p""
	test -z "$value" && die "Couldn't get value for [$n]"
	echo "$value"
}

ask_pref() {
	SETTING_NAME="$1"
	
	SETTING_TEXT_="${SETTING_NAME}_TEXT"
	SETTING_TEXT="`eval echo -e \\$${SETTING_TEXT_}`"	

	SETTING_VALUES_="${SETTING_NAME}_VALUES"
	SETTING_VALUES="`eval echo -e \\$${SETTING_VALUES_}`"	
	
	SETTING_OLD_VALUE="`get_pref "$SETTING_NAME"`"
	test -z "$SETTING_TEXT" -o -z "$SETTING_VALUES" && die "ask_pref: [$1] -> [$SETTING_TEXT] [$SETTING_VALUES]"
	
	#echo "[$1] -> $SETTING_OLD_VALUE"
	#echo -e "$SETTING_TEXT"
	

	if test "$SETTING_VALUES" = "<STRING>"
	then				
		while true
		do
			echo -e "\nPlease enter a new value [$SETTING_OLD_VALUE]"
			echo -en "Your Choice: "
			read junk
			
			echo ""
						
			while true
			do
				echo -n "Is the new value of [$junk] correct? [Y|n] "
				read junk2
				
				case "$junk2" in
				y|Y|"")		END_LOOP=true
						break ;;
				*)		END_LOOP=false
						break ;;
				esac
			done
			
			if test "$END_LOOP" = "true"
			then
				SETTING_NEW_VALUE="$junk"
				break
			fi
		
		done
		
	else	
		# If it's not a string, it's a fixed list of possible settings
		echo -e "\nSelect one of the following:\n"	
		cnt=1	
		
		echo " $SETTING_VALUES"
		for val in $SETTING_VALUES
		do
			if test "$val" != "$SETTING_OLD_VALUE"
			then
				echo -e "\t[$cnt] $val"
			else
				echo -e "\t[$cnt] $val *"
				SETTING_OLD_VALUE_NUM="$cnt"
			fi
			let cnt=$cnt+1
		done

		echo ""
		while true
		do
			echo -en "Your choice [$SETTING_OLD_VALUE_NUM]: "
			read junk		

			if test -n "$junk"
			then
				cnt=1 ; SETTING_NEW_VALUE=""
				for val in $SETTING_VALUES
				do
					if test "$junk" = "$cnt"
					then
						SETTING_NEW_VALUE="$val"
						break
					fi

					let cnt=$cnt+1
				done
			else
				SETTING_NEW_VALUE="$SETTING_OLD_VALUE"
			fi

			test -n "$SETTING_NEW_VALUE" && break
		done
		
	fi
		
	
	if test "$SETTING_NEW_VALUE" != "$SETTING_OLD_VALUE"
	then
		set_pref "$SETTING_NAME" "$SETTING_NEW_VALUE"
		echo "Changed $SETTING_NAME to $SETTING_NEW_VALUE"
	else
		echo "$SETTING_NAME remains unchanged"
	fi
	echo -e "\n"
}

build_menu() {
	for setting in $TARGETS
	do	
		# SETTING_MENUPOS contains $setting_MENUPOS
		SETTING_MENUPOS_="${setting}_MENUPOS"
		SETTING_MENUPOS="`eval echo -e \\$${SETTING_MENUPOS_}`"	

		# Store all entries in variables named after the menu name
		MENU_NAME="SUBMENU_${SETTING_MENUPOS}"
		export "${MENU_NAME}"="`eval echo -e \\$${MENU_NAME} ` $setting"
		
		
		echo "$AVAILABLE_MENUES" | grep -q "$SETTING_MENUPOS" || AVAILABLE_MENUES="$SETTING_MENUPOS $AVAILABLE_MENUES"
						
	done	
	
	#echo "[$AVAILABLE_MENUES]"
	
	while true
	do
		echo -e "\nSelect a menu:\n"
		cnt=1
		for menu in $AVAILABLE_MENUES Exit
		do
			MENU_NAME="SUBMENU_$menu"
			echo -e "\t[$cnt] $menu"
			let cnt=$cnt+1
		done
		
		echo ""
		
		while true
		do
			echo -n "Your Choice: "
			read junk

			cnt=1 ; GET_MENU=""
			for menu in $AVAILABLE_MENUES Exit
			do
				if test "$cnt" = "$junk"
				then
					GET_MENU="$menu"
					break
				fi
				let cnt=$cnt+1
			done

			if test "$GET_MENU" = "Exit"
			then
				EXIT_PROG=true
				break
			else
				EXIT_PROG=false
			fi
			
			if test -n "$GET_MENU"
			then
				show_menu "$menu"
				break
			fi
		done		
		test "$EXIT_PROG" = true && break
	done
}

show_menu() {
	MENU_NAME="SUBMENU_$1"

	
	while true
	do
		echo -e "\nEntries in this menu:\n"
		cnt=1
		for entry in `eval echo -e \\$${MENU_NAME}` Back
		do
			MENU_NAME_TEXT="${entry}_TEXT" || MENU_NAME_TEXT="${entry}"
			
			test "$entry" != Back && tmp_="`eval echo -e \\$${MENU_NAME_TEXT}`" || tmp_="Back"
			
			echo -e "\t[$cnt] $tmp_"
			let cnt=$cnt+1
		done	

		echo ""
	
		while true
		do
			echo -en "Your Choice: "
			read junk

			cnt=1 ; GET_ENTRY=""
			for entry in `eval echo -e \\$${MENU_NAME}` Back
			do
				if test "$cnt" = "$junk"
				then
					GET_ENTRY="$entry"
					break
				fi
				let cnt=$cnt+1
			done

			if test "$GET_ENTRY" = "Back"
			then
				EXIT_MENU=true
				break
			else
				EXIT_MENU=false
			fi
			
			
			# At this point the user has selected a menuitem != "Back"
			if test -n "$GET_ENTRY"
			then
				select_config
				ask_pref "$GET_ENTRY"
				break
			fi
		done	
		
		test "$EXIT_MENU" = true && break		
	done
}

select_config() {
	echo -e "\nChange this setting only for the next reboot or permanently?\n"

	echo -e "Select one of the following:\n"

	echo -e "\t[1] Only for the next reboot"
	echo -e "\t[2] Permanently"
	echo ""
	
	while true
	do
		echo -en "Your Choice: "
		read junk
		
		case "$junk" in
		2)	CFG="$PERMANENT_CFG"
			break ;;
		1)	CFG="$TEMPORARY_CFG"
			break ;;
		esac		
	done
	
	# Supress error message about missing config
	! test -e "$CFG" && touch "$CFG"
	
}

go() {
	build_menu
	
	exit 0
	for setting in $TARGETS
	do
		ask_pref "$setting"
	done
	
}


! test -e "$CFG" && die "[$CFG] not found, exiting"
go