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