1#!/bin/sh
2#-
3# Copyright (c) 2012-2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27#
28############################################################ INCLUDES
29
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32f_dprintf "%s: loading includes..." "$0"
33f_include $BSDCFG_SHARE/dialog.subr
34f_include $BSDCFG_SHARE/mustberoot.subr
35
36BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
37f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
38
39f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&
40	pgm="${ipgm:-$pgm}"
41
42############################################################ FUNCTIONS
43
44# dialog_menu_main
45#
46# Display the dialog(1)-based application main menu.
47#
48dialog_menu_main()
49{
50	local prompt=
51	local menu_list="
52		'X' '$msg_exit'
53		'1' '$msg_toggle_startup_services'
54		'2' '$msg_view_edit_startup_configuration'
55		'3' '$msg_miscellaneous_startup_services'
56	" # END-QUOTE
57	local defaultitem= # Calculated below
58	local hline="$hline_arrows_tab_enter"
59
60	local height width rows
61	eval f_dialog_menu_size height width rows \
62	                        \"\$DIALOG_TITLE\"     \
63	                        \"\$DIALOG_BACKTITLE\" \
64	                        \"\$prompt\"           \
65	                        \"\$hline\"            \
66	                        $menu_list
67
68	# Obtain default-item from previously stored selection
69	f_dialog_default_fetch defaultitem
70
71	local menu_choice
72	menu_choice=$( eval $DIALOG \
73		--title \"\$DIALOG_TITLE\"         \
74		--backtitle \"\$DIALOG_BACKTITLE\" \
75		--hline \"\$hline\"                \
76		--ok-label \"\$msg_ok\"            \
77		--cancel-label \"\$msg_cancel\"    \
78		--default-item \"\$defaultitem\"   \
79		--menu \"\$prompt\"                \
80		$height $width $rows               \
81		$menu_list                         \
82		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
83	)
84	local retval=$?
85	f_dialog_data_sanitize menu_choice
86	f_dialog_menutag_store "$menu_choice"
87	f_dialog_default_store "$menu_choice"
88	return $retval
89}
90
91############################################################ MAIN
92
93# Incorporate rc-file if it exists
94[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
95
96#
97# Process command-line arguments
98#
99while getopts h$GETOPTS_STDARGS flag; do
100	case "$flag" in
101	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
102	esac
103done
104shift $(( $OPTIND - 1 ))
105
106#
107# Initialize
108#
109f_dialog_title "$msg_startup"
110f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
111f_mustberoot_init
112
113#
114# Launch application main menu
115#
116while :; do
117	dialog_menu_main || f_die
118	f_dialog_menutag_fetch mtag
119
120	command=
121	case "$mtag" in
122	X) break ;;
123	1) command=rcvar  ;; # Toggle Startup Services
124	2) command=rcconf ;; # View/Edit Startup Configuration
125	3) command=misc   ;; # Miscellaneous Startup Services
126	esac
127
128	if [ "$command" ]; then
129		$BSDCFG_LIBE/$APP_DIR/$command ${USE_XDIALOG:+-X}
130	else
131		f_die 1 "$msg_unknown_startup_menu_selection"
132	fi
133done
134
135exit $SUCCESS
136
137################################################################################
138# END
139################################################################################
140