1#!/bin/sh
2#-
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2010 iXsystems, Inc.  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: stable/12/usr.sbin/pc-sysinstall/backend/functions-installpackages.sh 326275 2017-11-27 15:28:26Z pfg $
29
30# Functions which check and load any optional packages specified in the config
31
32. ${BACKEND}/functions.sh
33. ${BACKEND}/functions-parse.sh
34
35# Recursively determine all dependencies for this package
36determine_package_dependencies()
37{
38  local PKGNAME="${1}"
39  local DEPFILE="${2}"
40
41  grep -q "${PKGNAME}" "${DEPFILE}"
42  if [ $? -ne 0 ]
43  then
44    echo "${PKGNAME}" >> "${DEPFILE}"
45    get_package_dependencies "${PKGNAME}" "1"
46
47    local DEPS="${VAL}"
48    for d in ${DEPS}
49    do
50      determine_package_dependencies "${d}" "${DEPFILE}"
51    done
52  fi
53};
54
55# Fetch packages dependencies from a file
56fetch_package_dependencies()
57{
58  local DEPFILE
59  local DEPS
60  local SAVEDIR
61
62  DEPFILE="${1}"
63  DEPS=`cat "${DEPFILE}"`
64  SAVEDIR="${2}"
65
66  for d in ${DEPS}
67  do
68    get_package_short_name "${d}"
69    SNAME="${VAL}"
70
71    get_package_category "${SNAME}"
72    CATEGORY="${VAL}"
73
74    fetch_package "${CATEGORY}" "${d}" "${SAVEDIR}"
75  done
76};
77
78# Check for any packages specified, and begin loading them
79install_packages()
80{
81  echo "Checking for packages to install..."
82  sleep 2
83
84  # First, lets check and see if we even have any packages to install
85  get_value_from_cfg installPackages
86
87  # Nothing to do?
88  if [ -z "${VAL}" ]; then return; fi
89
90  echo "Installing packages..."
91  sleep 3
92
93  local PKGPTH
94
95  HERE=`pwd`
96  rc_halt "mkdir -p ${FSMNT}${PKGTMPDIR}"
97
98  # Determine the directory we will install packages from
99  get_package_location
100  rc_halt "cd ${PKGDLDIR}"
101
102  # Set the location of the INDEXFILE
103  INDEXFILE="${TMPDIR}/INDEX"
104
105  if [ ! -f "${INDEXFILE}" ]; then
106    get_package_index
107  fi
108
109  if [ ! -f "${TMPDIR}/INDEX.parsed" -a "$INSTALLMEDIUM" = "ftp" ]; then
110    parse_package_index
111  fi
112
113  # What extension are we using for pkgs?
114  PKGEXT="txz"
115  get_value_from_cfg pkgExt
116  if [ -n "${VAL}" ]; then
117     strip_white_space ${VAL}
118     PKGEXT="$VAL"
119  fi
120  export PKGEXT
121
122  # We dont want to be bothered with scripts asking questions
123  PACKAGE_BUILDING=yes
124  export PACKAGE_BUILDING
125
126  # Lets start by cleaning up the string and getting it ready to parse
127  get_value_from_cfg_with_spaces installPackages
128  PACKAGES="${VAL}"
129  echo_log "Packages to install: `echo $PACKAGES | wc -w | awk '{print $1}'`"
130  for i in $PACKAGES
131  do
132    if ! get_package_name "${i}"
133    then
134      echo_log "Unable to locate package ${i}"
135      continue
136    fi
137
138    PKGNAME="${VAL}"
139
140    # Fetch package + deps, but skip if installing from local media
141    if [ "${INSTALLMEDIUM}" = "ftp" ] ; then
142      DEPFILE="${FSMNT}/${PKGTMPDIR}/.${PKGNAME}.deps"
143      rc_nohalt "touch ${DEPFILE}"
144      determine_package_dependencies "${PKGNAME}" "${DEPFILE}"
145      fetch_package_dependencies "${DEPFILE}" "${FSMNT}/${PKGTMPDIR}"
146    fi
147
148    # Set package location
149    case "${INSTALLMEDIUM}" in
150      usb|dvd|local) PKGPTH="${PKGTMPDIR}/All/${PKGNAME}" ;;
151                  *) PKGPTH="${PKGTMPDIR}/${PKGNAME}" ;;
152    esac
153
154    # See if we need to determine the package format we are working with
155    if [ -z "${PKGINFO}" ] ; then
156      tar tqf "${FSMNT}${PKGPTH}" '+MANIFEST' >/dev/null 2>/dev/null
157      if [ $? -ne 0 ] ; then
158        PKGADD="pkg_add -C ${FSMNT}"
159        PKGINFO="pkg_info"
160      else
161        PKGADD="pkg -c ${FSMNT} add"
162        PKGINFO="pkg info"
163        bootstrap_pkgng
164      fi
165    fi
166
167    # If the package is not already installed, install it!
168    if ! run_chroot_cmd "${PKGINFO} -e ${PKGNAME}" >/dev/null 2>/dev/null
169    then
170      echo_log "Installing package: ${PKGNAME}"
171      rc_nohalt "${PKGADD} ${PKGPTH}"
172    fi
173
174    if [ "${INSTALLMEDIUM}" = "ftp" ] ; then
175      rc_nohalt "rm ${DEPFILE}"
176    fi
177
178  done
179
180  echo_log "Package installation complete!"
181
182  # Cleanup after ourselves
183  rc_halt "cd ${HERE}"
184  if [ "${INSTALLMEDIUM}" = "ftp" ] ; then
185    rc_halt "rm -rf ${FSMNT}${PKGTMPDIR}" >/dev/null 2>/dev/null
186  else
187    rc_halt "umount ${FSMNT}${PKGTMPDIR}" >/dev/null 2>/dev/null
188    rc_halt "rmdir ${FSMNT}${PKGTMPDIR}" >/dev/null 2>/dev/null
189  fi
190};
191