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
31error()
32{
33	dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox $1 0 0
34	exit 1
35}
36
37FETCH_DISTRIBUTIONS=""
38LOCAL_DISTRIBUTIONS=""
39for dist in $DISTRIBUTIONS; do
40	if [ ! -f $BSDINSTALL_DISTDIR/$dist ]; then
41		FETCH_DISTRIBUTIONS="$FETCH_DISTRIBUTIONS $dist"
42	else
43		LOCAL_DISTRIBUTIONS="$LOCAL_DISTRIBUTIONS $dist"
44	fi
45done
46LOCAL_DISTRIBUTIONS=`echo $LOCAL_DISTRIBUTIONS`	# Trim white space
47FETCH_DISTRIBUTIONS=`echo $FETCH_DISTRIBUTIONS`	# Trim white space
48
49if [ -z "$FETCH_DISTRIBUTIONS" ]; then
50	echo $BSDINSTALL_DISTDIR >&2
51	exit 0
52fi
53
54ALL_DISTRIBUTIONS="$DISTRIBUTIONS"
55WANT_DEBUG=
56
57# Download to a directory in the new system as scratch space
58BSDINSTALL_FETCHDEST="$BSDINSTALL_CHROOT/usr/freebsd-dist"
59mkdir -p "$BSDINSTALL_FETCHDEST" || error "Could not create directory $BSDINSTALL_FETCHDEST"
60
61if [ -z "$BSDINSTALL_DISTSITE" ]; then
62	exec 3>&1
63	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&3)
64	MIRROR_BUTTON=$?
65	exec 3>&-
66	test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
67	export BSDINSTALL_DISTSITE
68fi
69
70BSDINSTALL_DISTDIR_ORIG="$BSDINSTALL_DISTDIR"
71export BSDINSTALL_DISTDIR="$BSDINSTALL_FETCHDEST"
72export FTP_PASSIVE_MODE=YES
73
74if [ -f "$BSDINSTALL_DISTDIR_ORIG/MANIFEST" ]; then
75	cp "$BSDINSTALL_DISTDIR_ORIG/MANIFEST" "$BSDINSTALL_DISTDIR/MANIFEST"
76	VERIFY_MANIFEST_SIG=0
77else
78	FETCH_DISTRIBUTIONS="MANIFEST $FETCH_DISTRIBUTIONS"
79	VERIFY_MANIFEST_SIG=1
80
81	# XXX actually verify signature on manifest
82	dialog --backtitle "FreeBSD 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
83fi
84
85if [ ! -z "$LOCAL_DISTRIBUTIONS" ]; then
86	# Copy local stuff first
87	env DISTRIBUTIONS="$LOCAL_DISTRIBUTIONS" \
88		BSDINSTALL_DISTSITE="file://$BSDINSTALL_DISTDIR" \
89		bsdinstall distfetch || \
90		error "Failed to fetch distribution from local media"
91fi
92
93export DISTRIBUTIONS="$FETCH_DISTRIBUTIONS"
94
95# Iterate through the distribution list and set a flag if debugging
96# distributions have been selected.
97for _DISTRIBUTION in $DISTRIBUTIONS; do
98	case $_DISTRIBUTION in
99		*-dbg.*)
100			[ -e $BSDINSTALL_DISTDIR/$_DISTRIBUTION ] \
101				&& continue
102			WANT_DEBUG=1
103			DEBUG_LIST="\n$DEBUG_LIST\n$_DISTRIBUTION"
104			;;
105		*)
106			;;
107	esac
108done
109
110# Fetch the distributions.
111bsdinstall distfetch
112rc=$?
113
114if [ $rc -ne 0 ]; then
115	# If unable to fetch the remote distributions, recommend
116	# deselecting the debugging distributions, and retrying the
117	# installation, since failure to fetch *-dbg.txz should not
118	# be considered a fatal installation error.
119	msg="Failed to fetch remote distribution"
120	if [ ! -z "$WANT_DEBUG" ]; then
121		# Trim leading and trailing newlines.
122		DEBUG_LIST="${DEBUG_LIST%%\n}"
123		DEBUG_LIST="${DEBUG_LIST##\n}"
124		msg="$msg\n\nPlease deselect the following distributions"
125		msg="$msg and retry the installation:"
126		msg="$msg\n$DEBUG_LIST"
127	fi
128	error "$msg"
129fi
130
131echo $BSDINSTALL_DISTDIR >&2
132
133