1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# Copyright (c) 2013-2018 Devin Teske
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28# $FreeBSD$
29#
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33
34error()
35{
36	bsddialog --backtitle "$OSNAME Installer" --title "Error" --msgbox "$1" 0 0
37	exit 1
38}
39
40FETCH_DISTRIBUTIONS=""
41LOCAL_DISTRIBUTIONS=""
42for dist in $DISTRIBUTIONS; do
43	if [ ! -f $BSDINSTALL_DISTDIR/$dist ]; then
44		FETCH_DISTRIBUTIONS="$FETCH_DISTRIBUTIONS $dist"
45	else
46		LOCAL_DISTRIBUTIONS="$LOCAL_DISTRIBUTIONS $dist"
47	fi
48done
49LOCAL_DISTRIBUTIONS=`echo $LOCAL_DISTRIBUTIONS`	# Trim white space
50FETCH_DISTRIBUTIONS=`echo $FETCH_DISTRIBUTIONS`	# Trim white space
51
52if [ -z "$FETCH_DISTRIBUTIONS" ]; then
53	echo $BSDINSTALL_DISTDIR >&2
54	exit 0
55fi
56
57ALL_DISTRIBUTIONS="$DISTRIBUTIONS"
58WANT_DEBUG=
59
60# Download to a directory in the new system as scratch space
61BSDINSTALL_FETCHDEST="$BSDINSTALL_CHROOT/usr/freebsd-dist"
62mkdir -p "$BSDINSTALL_FETCHDEST" || error "Could not create directory $BSDINSTALL_FETCHDEST"
63
64if [ -z "$BSDINSTALL_DISTSITE" ]; then
65	exec 3>&1
66	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&3)
67	MIRROR_BUTTON=$?
68	exec 3>&-
69	test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
70	export BSDINSTALL_DISTSITE
71fi
72
73BSDINSTALL_DISTDIR_ORIG="$BSDINSTALL_DISTDIR"
74export BSDINSTALL_DISTDIR="$BSDINSTALL_FETCHDEST"
75export FTP_PASSIVE_MODE=YES
76
77if [ -f "$BSDINSTALL_DISTDIR_ORIG/MANIFEST" ]; then
78	cp "$BSDINSTALL_DISTDIR_ORIG/MANIFEST" "$BSDINSTALL_DISTDIR/MANIFEST"
79	VERIFY_MANIFEST_SIG=0
80else
81	FETCH_DISTRIBUTIONS="MANIFEST $FETCH_DISTRIBUTIONS"
82	VERIFY_MANIFEST_SIG=1
83
84	# XXX actually verify signature on manifest
85	bsddialog --backtitle "$OSNAME Installer" --title "Warning" --msgbox "Manifest not found on local disk and will be fetched from an unverified source. This is a potential security risk. If you do not wish to proceed, press control-C now." 0 0
86fi
87
88if [ ! -z "$LOCAL_DISTRIBUTIONS" ]; then
89	# Copy local stuff first
90	env DISTRIBUTIONS="$LOCAL_DISTRIBUTIONS" \
91		BSDINSTALL_DISTSITE="file://$BSDINSTALL_DISTDIR_ORIG" \
92		bsdinstall distfetch || \
93		error "Failed to fetch distribution from local media"
94fi
95
96export DISTRIBUTIONS="$FETCH_DISTRIBUTIONS"
97
98# Iterate through the distribution list and set a flag if debugging
99# distributions have been selected.
100for _DISTRIBUTION in $DISTRIBUTIONS; do
101	case $_DISTRIBUTION in
102		*-dbg.*)
103			[ -e $BSDINSTALL_DISTDIR/$_DISTRIBUTION ] \
104				&& continue
105			WANT_DEBUG=1
106			DEBUG_LIST="\n$DEBUG_LIST\n$_DISTRIBUTION"
107			;;
108		*)
109			;;
110	esac
111done
112
113# Fetch the distributions.
114bsdinstall distfetch
115rc=$?
116
117if [ $rc -ne 0 ]; then
118	# If unable to fetch the remote distributions, recommend
119	# deselecting the debugging distributions, and retrying the
120	# installation, since failure to fetch *-dbg.txz should not
121	# be considered a fatal installation error.
122	msg="Failed to fetch remote distribution"
123	if [ ! -z "$WANT_DEBUG" ]; then
124		# Trim leading and trailing newlines.
125		DEBUG_LIST="${DEBUG_LIST%%\n}"
126		DEBUG_LIST="${DEBUG_LIST##\n}"
127		msg="$msg\n\nPlease deselect the following distributions"
128		msg="$msg and retry the installation:"
129		msg="$msg\n$DEBUG_LIST"
130	fi
131	error "$msg"
132fi
133
134echo $BSDINSTALL_DISTDIR >&2
135
136