1211485Simp#!/bin/sh
2211485Simp#-
3211485Simp# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4211485Simp#
5211485Simp# Redistribution and use in source and binary forms, with or without
6211485Simp# modification, are permitted provided that the following conditions
7211485Simp# are met:
8211485Simp# 1. Redistributions of source code must retain the above copyright
9211485Simp#    notice, this list of conditions and the following disclaimer.
10211485Simp# 2. Redistributions in binary form must reproduce the above copyright
11211485Simp#    notice, this list of conditions and the following disclaimer in the
12211485Simp#    documentation and/or other materials provided with the distribution.
13211485Simp#
14211485Simp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15211485Simp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16211485Simp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17211485Simp# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18211485Simp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19211485Simp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20211485Simp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21211485Simp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22211485Simp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23211485Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24211485Simp# SUCH DAMAGE.
25211485Simp#
26211485Simp# $FreeBSD$
27211485Simp
28211485Simp# Functions which check and load any optional packages specified in the config
29211485Simp
30211485Simp. ${BACKEND}/functions.sh
31211485Simp. ${BACKEND}/functions-parse.sh
32211485Simp
33211485Simp# Recursively determine all dependencies for this package
34211485Simpdetermine_package_dependencies()
35211485Simp{
36211730Simp  local PKGNAME="${1}"
37211730Simp  local DEPFILE="${2}"
38211485Simp
39220059Sjpaetzel  grep -q "${PKGNAME}" "${DEPFILE}"
40220059Sjpaetzel  if [ $? -ne 0 ]
41211730Simp  then
42211730Simp    echo "${PKGNAME}" >> "${DEPFILE}"
43211730Simp    get_package_dependencies "${PKGNAME}" "1"
44211485Simp
45211730Simp    local DEPS="${VAL}"
46211730Simp    for d in ${DEPS}
47211730Simp    do
48211730Simp      determine_package_dependencies "${d}" "${DEPFILE}"
49211730Simp    done
50211730Simp  fi
51211485Simp};
52211485Simp
53211485Simp# Fetch packages dependencies from a file
54211485Simpfetch_package_dependencies()
55211485Simp{
56211730Simp  local DEPFILE
57211730Simp  local DEPS
58211730Simp  local SAVEDIR
59211485Simp
60211730Simp  DEPFILE="${1}"
61211730Simp  DEPS=`cat "${DEPFILE}"`
62211730Simp  SAVEDIR="${2}"
63211485Simp
64211730Simp  for d in ${DEPS}
65211730Simp  do
66211730Simp    get_package_short_name "${d}"
67211730Simp    SNAME="${VAL}"
68211485Simp
69211730Simp    get_package_category "${SNAME}"
70211730Simp    CATEGORY="${VAL}"
71211485Simp
72211730Simp    fetch_package "${CATEGORY}" "${d}" "${SAVEDIR}"
73211730Simp  done
74211485Simp};
75211485Simp
76211485Simp# Check for any packages specified, and begin loading them
77211485Simpinstall_packages()
78211485Simp{
79247734Sjpaetzel  echo "Checking for packages to install..."
80247734Sjpaetzel  sleep 2
81247734Sjpaetzel
82211730Simp  # First, lets check and see if we even have any packages to install
83211730Simp  get_value_from_cfg installPackages
84211485Simp
85247734Sjpaetzel  # Nothing to do?
86247734Sjpaetzel  if [ -z "${VAL}" ]; then return; fi
87211486Simp
88247734Sjpaetzel  echo "Installing packages..."
89247734Sjpaetzel  sleep 3
90247734Sjpaetzel
91247734Sjpaetzel  local PKGPTH
92247734Sjpaetzel
93247734Sjpaetzel  HERE=`pwd`
94247734Sjpaetzel  rc_halt "mkdir -p ${FSMNT}${PKGTMPDIR}"
95247734Sjpaetzel
96247734Sjpaetzel  # Determine the directory we will install packages from
97247734Sjpaetzel  get_package_location
98247734Sjpaetzel  rc_halt "cd ${PKGDLDIR}"
99247734Sjpaetzel
100247734Sjpaetzel  # Set the location of the INDEXFILE
101247734Sjpaetzel  INDEXFILE="${TMPDIR}/INDEX"
102247734Sjpaetzel
103247734Sjpaetzel  if [ ! -f "${INDEXFILE}" ]; then
104247734Sjpaetzel    get_package_index
105247734Sjpaetzel  fi
106247734Sjpaetzel
107247734Sjpaetzel  if [ ! -f "${TMPDIR}/INDEX.parsed" -a "$INSTALLMEDIUM" = "ftp" ]; then
108247734Sjpaetzel    parse_package_index
109247734Sjpaetzel  fi
110247734Sjpaetzel
111247734Sjpaetzel  # What extension are we using for pkgs?
112247734Sjpaetzel  PKGEXT="txz"
113247734Sjpaetzel  get_value_from_cfg pkgExt
114247734Sjpaetzel  if [ -n "${VAL}" ]; then 
115247734Sjpaetzel     strip_white_space ${VAL}
116247734Sjpaetzel     PKGEXT="$VAL"
117247734Sjpaetzel  fi
118247734Sjpaetzel  export PKGEXT
119247734Sjpaetzel  
120247734Sjpaetzel  # We dont want to be bothered with scripts asking questions
121247734Sjpaetzel  PACKAGE_BUILDING=yes
122247734Sjpaetzel  export PACKAGE_BUILDING
123247734Sjpaetzel
124247734Sjpaetzel  # Lets start by cleaning up the string and getting it ready to parse
125247734Sjpaetzel  get_value_from_cfg_with_spaces installPackages
126247734Sjpaetzel  PACKAGES="${VAL}"
127247734Sjpaetzel  echo_log "Packages to install: `echo $PACKAGES | wc -w | awk '{print $1}'`"
128247734Sjpaetzel  for i in $PACKAGES
129247734Sjpaetzel  do
130247734Sjpaetzel    if ! get_package_name "${i}"
131211730Simp    then
132247734Sjpaetzel      echo_log "Unable to locate package ${i}"
133247734Sjpaetzel      continue
134211730Simp    fi
135211486Simp
136247734Sjpaetzel    PKGNAME="${VAL}"
137211485Simp
138247734Sjpaetzel    # Fetch package + deps, but skip if installing from local media
139247734Sjpaetzel    if [ "${INSTALLMEDIUM}" = "ftp" ] ; then
140247734Sjpaetzel      DEPFILE="${FSMNT}/${PKGTMPDIR}/.${PKGNAME}.deps"
141247734Sjpaetzel      rc_nohalt "touch ${DEPFILE}"
142247734Sjpaetzel      determine_package_dependencies "${PKGNAME}" "${DEPFILE}"
143247734Sjpaetzel      fetch_package_dependencies "${DEPFILE}" "${FSMNT}/${PKGTMPDIR}"
144247734Sjpaetzel    fi
145211485Simp
146247734Sjpaetzel    # Set package location
147247734Sjpaetzel    case "${INSTALLMEDIUM}" in
148247734Sjpaetzel      usb|dvd|local) PKGPTH="${PKGTMPDIR}/All/${PKGNAME}" ;;
149247734Sjpaetzel                  *) PKGPTH="${PKGTMPDIR}/${PKGNAME}" ;;
150247734Sjpaetzel    esac
151211485Simp
152247734Sjpaetzel    # See if we need to determine the package format we are working with
153247734Sjpaetzel    if [ -z "${PKGINFO}" ] ; then
154247734Sjpaetzel      tar tqf "${FSMNT}${PKGPTH}" '+MANIFEST' >/dev/null 2>/dev/null	
155247734Sjpaetzel      if [ $? -ne 0 ] ; then
156247734Sjpaetzel        PKGADD="pkg_add -C ${FSMNT}" 
157247734Sjpaetzel        PKGINFO="pkg_info" 
158247734Sjpaetzel      else
159247734Sjpaetzel        PKGADD="pkg -c ${FSMNT} add"
160247734Sjpaetzel        PKGINFO="pkg info"
161247734Sjpaetzel        bootstrap_pkgng
162211730Simp      fi
163247734Sjpaetzel    fi
164211486Simp
165247734Sjpaetzel    # If the package is not already installed, install it!
166247734Sjpaetzel    if ! run_chroot_cmd "${PKGINFO} -e ${PKGNAME}" >/dev/null 2>/dev/null
167247734Sjpaetzel    then
168247734Sjpaetzel      echo_log "Installing package: ${PKGNAME}"
169247734Sjpaetzel      rc_nohalt "${PKGADD} ${PKGPTH}"
170247734Sjpaetzel    fi
171211485Simp
172247734Sjpaetzel    if [ "${INSTALLMEDIUM}" = "ftp" ] ; then
173247734Sjpaetzel      rc_nohalt "rm ${DEPFILE}"
174247734Sjpaetzel    fi
175247734Sjpaetzel
176247734Sjpaetzel  done
177247734Sjpaetzel
178247734Sjpaetzel  echo_log "Package installation complete!"
179247734Sjpaetzel
180247734Sjpaetzel  # Cleanup after ourselves
181247734Sjpaetzel  rc_halt "cd ${HERE}"
182247734Sjpaetzel  if [ "${INSTALLMEDIUM}" = "ftp" ] ; then
183247734Sjpaetzel    rc_halt "rm -rf ${FSMNT}${PKGTMPDIR}" >/dev/null 2>/dev/null
184247734Sjpaetzel  else
185247734Sjpaetzel    rc_halt "umount ${FSMNT}${PKGTMPDIR}" >/dev/null 2>/dev/null
186247734Sjpaetzel    rc_halt "rmdir ${FSMNT}${PKGTMPDIR}" >/dev/null 2>/dev/null
187211730Simp  fi
188211485Simp};
189