1#!/bin/sh
2
3# installer - start installer frontend and backend as per pfi config.
4# $Id: installer,v 1.20 2005/04/13 03:32:16 cpressey Exp $
5
6### SUBS ###
7cleanup()
8{
9	killall -q dfuife_curses dfuibe_installer
10}
11
12background_backend()
13{
14	RENDEZVOUS=$1
15	TRANSPORT=$2
16	$pfi_backend \
17	    -o $SOURCE_DIR \
18	    -r $RENDEZVOUS \
19	    -t $TRANSPORT \
20	    >/dev/null 2>&1
21	RESULT=$?
22	case "$RESULT" in
23	0)
24		;;
25	5)
26		$pfi_shutdown_command
27		;;
28	*)
29		;;
30	esac
31}
32
33is_serial()
34{
35	# Detect if we are currently connected via a serial console
36	if [ "X`/usr/bin/kenv -q console`" == "Xcomconsole" ]; then
37		return 0 # return success
38	fi
39	return 1
40}
41
42setup_term()
43{
44	# If TERM has not been set manually (ie: still 'dialup' or from /etc/ttyd), we ask the user what they want to use
45	if [ "X`tty |cut -c6-9`" == "Xttyd" ]; then
46		newterm=${TERM}
47		if [ "X`/usr/bin/kenv smbios.bios.vendor`" == "XSeaBIOS" ]; then
48			# installation on a virtial machine uses this type of simulated bios often, so we can do better than vt100 (eg:vt220-co, vt320-co, cons50-w)
49			newterm="xterm"
50		elif [ "X${TERM}" == "Xdialup" ]; then
51			newterm="vt100"
52		fi
53		echo ""
54		echo -n "What is your terminal type (provide value termcap name)? [${newterm}]: "
55		read input
56		[ "${input}" = '' ] && input=$newterm
57		export TERM="${input}"
58		echo "set new TERM=$TERM"
59	fi
60	TTY_BAUD=`stty speed`
61	if [ $TTY_BAUD -lt 38400 ]; then
62		echo -n "Your serial connection is quite slow ($TTY_BAUD), causing installer slow down. Continue Anyway ? [Y/n]: "
63		read input
64		[ "${input}" == "N" ] || [ "${input}" == "n" ] && exit 0
65	fi
66}
67
68installer_start()
69{
70	# Console start sequence:
71	# - Backend (and all other logging) goes to console (ttyv0)
72	# - curses frontend starts on ttyv1.
73	# - Uses vidcontrol -s 2 to switch to ttyv1 once the frontend is up.
74
75	echo -n "Starting installer.  "
76
77	if [ -r /etc/defaults/pfi.conf ]; then
78		. /etc/defaults/pfi.conf
79	fi
80
81	if [ -r /etc/pfi.conf ]; then
82		echo "Reading /etc/pfi.conf ..."
83		. /etc/pfi.conf
84	else
85		echo "/etc/pfi.conf not found, starting interactive install."
86	fi
87
88	# We can set up any install variables and such
89	# here by examining pfi_* variables.
90
91	if [ "X$pfi_run" != "X" ]; then
92		pfi_frontend=none
93		$pfi_run
94	fi
95
96	case "X$pfi_dfui_transport" in
97	Xnpipe)
98		RENDEZVOUS="installer"
99		;;
100	Xtcp)
101		RENDEZVOUS="9999"
102		;;
103	*)
104		echo "Unsupported DFUI transport '$pfi_dfui_transport'."
105		return
106		;;
107	esac
108
109	if [ "X$pfi_frontend" = "Xauto" ]; then
110		if [ "X$TTY_INST" = "X" ]; then
111			if $(is_serial); then
112				setup_term
113				RENDEZVOUS="installer"
114				pfi_dfui_transport="npipe"
115				TTY=$(tty)
116				pfi_frontend="curseslog"
117			else
118				if $(is_installmedia); then
119					TTY=/dev/ttyv1
120					pfi_frontend="cursesvty"
121				else
122					TTY=$(tty)
123					pfi_frontend="curseslog"
124				fi
125			fi
126		else
127			pfi_frontend="cursesx11"
128		fi
129	fi
130
131	case "X$pfi_frontend" in
132	Xqt)
133		$pfi_backend \
134		    -o $SOURCE_DIR \
135		    -r $RENDEZVOUS \
136		    -t $pfi_dfui_transport
137		RESULT=$?
138		;;
139	Xcgi)
140		$pfi_backend \
141		    -o $SOURCE_DIR \
142		    -r $RENDEZVOUS \
143		    -t $pfi_dfui_transport
144		RESULT=$?
145		;;
146	Xcursesvty)
147		ps auwwwxxx > /tmp/ps.txt
148		if grep -q dfuife_curses /tmp/ps.txt; then
149			# Frontend is already running.
150		else
151			ESCDELAY=$pfi_curses_escdelay \
152			    /usr/sbin/dfuife_curses \
153				-r $RENDEZVOUS \
154				-t $pfi_dfui_transport \
155				-b /usr/share/installer/fred.txt \
156			    2>/dev/ttyv0 <$TTY >$TTY &
157		fi
158		rm -f /tmp/ps.txt
159		sleep 1
160		vidcontrol -s 2 </dev/ttyv0
161		$pfi_backend \
162		    -o $SOURCE_DIR \
163		    -r $RENDEZVOUS \
164		    -t $pfi_dfui_transport
165		RESULT=$?
166		sleep 1
167		killall dfuife_curses
168		vidcontrol -s 1 </dev/ttyv0
169		;;
170	Xcurseslog)
171		ps auwwwxxx > /tmp/ps.txt
172		if grep -q dfuife_curses /tmp/ps.txt; then
173			# Frontend is already running.
174		else
175			ESCDELAY=$pfi_curses_escdelay \
176			    /usr/sbin/dfuife_curses \
177				-r $RENDEZVOUS \
178				-t $pfi_dfui_transport \
179				-b /usr/share/installer/fred.txt \
180			    2>/tmp/dfuife_curses.log <$TTY >$TTY &
181		fi
182		rm -f /tmp/ps.txt
183		sleep 1
184		$pfi_backend \
185		    -o $SOURCE_DIR \
186		    -r $RENDEZVOUS \
187		    -t $pfi_dfui_transport \
188		    >/dev/null 2>&1
189		RESULT=$?
190		sleep 1
191		killall -q dfuife_curses
192		;;
193	Xcursesx11)
194		ps auwwwxxx > /tmp/ps.txt
195		if grep -q dfuife_curses /tmp/ps.txt; then
196			echo "Frontend is already running"
197		else
198			ESCDELAY=$pfi_curses_escdelay \
199			/usr/sbin/dfuife_curses \
200			-r $RENDEZVOUS \
201			-t $pfi_dfui_transport \
202			-b /usr/share/installer/fred.txt \
203			>$TTY_INST <$TTY_INST 2>&1 &
204		fi
205		rm -f /tmp/ps.txt
206		sleep 1
207		$pfi_backend \
208		    -o $SOURCE_DIR \
209		    -r $RENDEZVOUS \
210		    -t $pfi_dfui_transport
211		RESULT=$?
212		sleep 1
213		killall dfuife_curses
214		;;
215	Xnone)
216		RESULT=0
217		;;
218	*)
219		echo "Unknown installer frontend '$pfi_frontend'."
220		return
221		;;
222	esac
223
224	case "$RESULT" in
225	0)
226		;;
227	5)
228		$pfi_shutdown_command
229		;;
230	*)
231		;;
232	esac
233}
234
235is_installmedia()
236{
237    local _ttyv1=$(grep -w "^ttyv1" /etc/ttys)
238    local guest=$(sysctl -n kern.vmm_guest)
239
240    #
241    # ttyv1 isn't configured for the install media so use
242    # that as a clue for now. Vkernels will be forced
243    # to use 'curseslog' to avoid polluting its only
244    # terminal.
245    #
246    [ "${guest}" = "vkernel" ] && return 1;
247
248    if [ -z "${_ttyv1}" ]; then
249	return 0	# Return success, it's a USB image, ISO etc.
250    else
251	return 1
252    fi
253}
254
255### MAIN ###
256
257if [ $# -gt 1 ]; then
258	echo "usage: installer [source_directory]"
259	exit 1
260elif [ $# = 1 -a ! -d $1 ]; then
261	echo "source_directory does not exist or is no directory"
262	exit 1
263fi
264
265trap cleanup EXIT SIGTERM SIGINT
266
267#
268# Source directory for the installation
269#
270if [ $# = 1 ]; then
271	SOURCE_DIR=$1
272else
273	SOURCE_DIR=/
274fi
275
276ps auwwwxxx > /tmp/ps.txt
277if grep -q dfuibe_installer /tmp/ps.txt; then
278	# Installer is already running. Log in normally.
279	rm -f /tmp/ps.txt
280else
281	rm -f /tmp/ps.txt
282	installer_start
283fi
284
285### END of installer ###
286