1#	$OpenBSD: dot.profile,v 1.51 2023/02/20 01:55:41 kn Exp $
2#	$NetBSD: dot.profile,v 1.1 1995/12/18 22:54:43 pk Exp $
3#
4# Copyright (c) 2009 Kenneth R. Westerback
5# Copyright (c) 1995 Jason R. Thorpe
6# Copyright (c) 1994 Christopher G. Demetriou
7# All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17# 3. All advertising materials mentioning features or use of this software
18#    must display the following acknowledgement:
19#	This product includes software developed by Christopher G. Demetriou.
20# 4. The name of the author may not be used to endorse or promote products
21#    derived from this software without specific prior written permission
22#
23# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#
34
35# Turn off Strict Bourne shell.
36set +o sh
37
38export VNAME=$(sysctl -n kern.osrelease)
39export VERSION="${VNAME%.*}${VNAME#*.}"
40export ARCH=$(sysctl -n hw.machine)
41export OBSD="OpenBSD/$ARCH $VNAME"
42export PATH=/sbin:/bin:/usr/bin:/usr/sbin:/
43
44umask 022
45
46# emacs-style command line editing.
47set -o emacs
48
49TIMEOUT_PERIOD_SEC=5
50
51# Stop the background timer.
52stop_timeout() {
53	local _pid;
54	if [ -f /tmp/dotpid ]; then
55		_pid=$(cat /tmp/dotpid)
56		kill -KILL -$_pid 2>/dev/null
57		wait $_pid 2>/dev/null
58		rm /tmp/dotpid
59	fi
60}
61
62# Start a timeout process, in case install gets hung somehow
63start_timeout() {
64	set -m
65	(
66		sleep $TIMEOUT_PERIOD_SEC && kill $$
67	) &
68	echo $! > /tmp/dotpid
69	set +m
70}
71
72if [[ -z $DONEPROFILE ]]; then
73	DONEPROFILE=YES
74
75	# Extract rootdisk from last 'root on ...' dmesg line.
76	rootdisk=$(dmesg | sed -E '/^root on ([^ ]+) .*$/h;$!d;g;s//\1/')
77	mount -u /dev/${rootdisk:-rd0a} /
78
79	# Create a fake rc that just returns 1 and throws us back.
80	echo ! : >/etc/rc
81
82	# Create working directories with proper permissions in /tmp.
83	mkdir -m u=rwx,go=rx -p /tmp/{ai,i}
84
85	# Start autoconfiguration daemons.
86	# Hide legit "already running" errors when reentering the installer.
87	[[ -x /sbin/resolvd ]] && /sbin/resolvd 2>/dev/null
88	[[ -x /sbin/dhcpleased ]] && /sbin/dhcpleased 2>/dev/null
89	[[ -x /sbin/slaacd ]] && /sbin/slaacd 2>/dev/null
90
91	# Set up some sane tty defaults.
92	echo 'erase ^?, werase ^W, kill ^U, intr ^C, status ^T'
93	stty newcrt werase ^W intr ^C kill ^U erase ^? status ^T
94
95	cat <<__EOT
96
97Welcome to the $OBSD installation program.
98__EOT
99	# try unattended install
100	/autoinstall -x
101
102	# Set timer to automatically start unattended installation or upgrade
103	# if netbooted or if a response file is found in / after a timeout,
104	# but only the very first time around.
105	timeout=false
106	if [[ ! -f /tmp/ai/noai ]] && { ifconfig netboot >/dev/null 2>&1 ||
107		[[ -f /auto_install.conf ]] ||
108		[[ -f /auto_upgrade.conf ]]; }; then
109
110		echo "Starting non-interactive mode in ${TIMEOUT_PERIOD_SEC} seconds..."
111		>/tmp/ai/noai
112
113		# Set trap handlers to remove timer if the shell is interrupted,
114		# killed or about to exit.
115		trap 'exit 1' INT
116		trap 'timeout=true' TERM
117		trap 'stop_timeout' EXIT
118
119		# Stop monitoring background processes to avoid printing job
120		# completion notices in interactive shell mode.
121		# Silence "[1] <pid>" on stderr when starting the timer.
122		set +m
123		start_timeout 2>/dev/null
124	fi
125
126	while :; do
127		read REPLY?'(I)nstall, (U)pgrade, (A)utoinstall or (S)hell? '
128
129		# Begin the automatic installation if the timeout has expired.
130		if $timeout; then
131			timeout=false
132			echo
133			REPLY=a
134		else
135			# User has made a choice; stop the read timeout.
136			stop_timeout
137		fi
138
139		case $REPLY in
140		[aA]*)	/autoinstall && break;;
141		[iI]*)	/install && break;;
142		[uU]*)	/upgrade && break;;
143		[sS]*)	break;;
144		esac
145	done
146fi
147