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