1169689Skan#!/bin/sh
2169689Skan#-
3169689Skan# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4169689Skan#
5169689Skan# Redistribution and use in source and binary forms, with or without
6169689Skan# modification, are permitted provided that the following conditions
7169689Skan# are met:
8169689Skan# 1. Redistributions of source code must retain the above copyright
9169689Skan#    notice, this list of conditions and the following disclaimer.
10169689Skan# 2. Redistributions in binary form must reproduce the above copyright
11169689Skan#    notice, this list of conditions and the following disclaimer in the
12169689Skan#    documentation and/or other materials provided with the distribution.
13169689Skan#
14169689Skan# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15169689Skan# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16169689Skan# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17169689Skan# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18169689Skan# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19169689Skan# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20169689Skan# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21169689Skan# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22169689Skan# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23169689Skan# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24169689Skan# SUCH DAMAGE.
25169689Skan#
26169689Skan# $FreeBSD$
27169689Skan
28169689Skan# functions.sh
29169689Skan# Library of functions which pc-sysinstall may call upon for parsing the config
30169689Skan
31169689Skan# which gets the value of a setting in the provided line
32169689Skanget_value_from_string()
33169689Skan{
34169689Skan  if [ -n "${1}" ]
35169689Skan  then
36169689Skan    export VAL="`echo ${1} | cut -d '=' -f 2-`"
37169689Skan  else
38169689Skan    echo "Error: Did we forgot to supply a string to parse?"
39169689Skan    exit 1
40169689Skan  fi
41169689Skan};
42169689Skan
43169689Skan# Get the value from the cfg file including spaces
44169689Skanget_value_from_cfg_with_spaces()
45169689Skan{
46169689Skan  if [ -n "${1}" ]
47169689Skan  then
48169689Skan    export VAL="`grep ^${1}= ${CFGF} | head -n 1 | cut -d '=' -f 2-`"
49169689Skan  else
50169689Skan    exit_err "Error: Did we forgot to supply a setting to grab?"
51169689Skan  fi
52169689Skan};
53169689Skan
54169689Skan
55169689Skan# Get the value from the cfg file
56169689Skanget_value_from_cfg()
57169689Skan{
58169689Skan  if [ -n "${1}" ]
59169689Skan  then
60169689Skan    export VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
61169689Skan  else
62169689Skan    exit_err "Error: Did we forgot to supply a setting to grab?"
63169689Skan  fi
64169689Skan};
65169689Skan
66169689Skan# Checks the value of a setting in the provided line with supplied possibilities
67169689Skan# 1 = setting we are checking,  2 = list of valid values
68169689Skanif_check_value_exists()
69169689Skan{
70169689Skan  if [ -n "${1}" -a -n "${2}" ]
71169689Skan  then
72169689Skan    # Get the first occurrence of the setting from the config, strip out whitespace
73169689Skan
74169689Skan    VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
75169689Skan    if [ -z "${VAL}" ]
76169689Skan    then
77169689Skan      # This value doesn't exist, lets return
78169689Skan      return 0
79169689Skan    fi
80169689Skan
81169689Skan
82169689Skan    VALID="1"
83169689Skan    for i in ${2}
84169689Skan    do
85169689Skan      VAL=`echo "$VAL"|tr A-Z a-z`
86169689Skan      if [ "$VAL" = "${i}" ]
87169689Skan      then 
88169689Skan        VALID="0"
89169689Skan      fi
90169689Skan    done 
91169689Skan    if [ "$VALID" = "1" ]
92169689Skan    then
93169689Skan      exit_err "Error: ${1} is set to unknown value $VAL"
94169689Skan    fi
95169689Skan  else
96169689Skan    exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
97169689Skan  fi
98169689Skan};
99169689Skan
100169689Skan# Checks the value of a setting in the provided line with supplied possibilities
101169689Skan# 1 = setting we are checking,  2 = list of valid values
102169689Skancheck_value()
103169689Skan{
104169689Skan  if [ -n "${1}" -a -n "${2}" ]
105169689Skan  then
106169689Skan    # Get the first occurrence of the setting from the config, strip out whitespace
107169689Skan    VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
108169689Skan    VALID="1"
109169689Skan    for i in ${2}
110169689Skan    do
111169689Skan      if [ "$VAL" = "${i}" ]
112169689Skan      then 
113169689Skan        VALID="0"
114169689Skan      fi
115169689Skan    done 
116169689Skan    if [ "$VALID" = "1" ]
117169689Skan    then
118169689Skan      exit_err "Error: ${1} is set to unknown value $VAL"
119169689Skan    fi
120169689Skan  else
121169689Skan    exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
122169689Skan  fi
123169689Skan};
124169689Skan
125169689Skan# Checks for the presence of the supplied arguments in the config file
126169689Skan# 1  = values to confirm exist
127169689Skanfile_sanity_check()
128169689Skan{
129169689Skan  if [ -n "$CFGF" -a -n "$1" ]
130169689Skan  then
131169689Skan    for i in $1
132169689Skan    do
133169689Skan      grep -q "^${i}=" $CFGF 2>/dev/null
134169689Skan      if [ $? -eq 0 ]
135169689Skan      then
136169689Skan        LN=`grep "^${i}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
137169689Skan        if [ -z "${LN}" ]
138169689Skan        then
139169689Skan          echo "Error: Config fails sanity test! ${i}= is empty"
140169689Skan          exit 1
141169689Skan        fi
142169689Skan      else
143169689Skan        echo "Error: Config fails sanity test! Missing ${i}="
144169689Skan        exit 1
145169689Skan      fi
146169689Skan    done
147169689Skan  else
148169689Skan    echo "Error: Missing config file, and / or values to sanity check for!"
149169689Skan    exit 1
150169689Skan  fi
151169689Skan};
152169689Skan
153169689Skan
154169689Skan# Function which merges the contents of a new config into the specified old one
155169689Skan# Only works with <val>= type configurations
156169689Skanmerge_config()
157169689Skan{
158169689Skan  OLDCFG="${1}"
159169689Skan  NEWCFG="${2}"
160169689Skan  FINALCFG="${3}"
161169689Skan
162169689Skan  # Copy our oldcfg to the new one, which will be used as basis
163169689Skan  cp ${OLDCFG} ${FINALCFG}
164169689Skan
165169689Skan  # Remove blank lines from new file
166  cat ${NEWCFG} | sed '/^$/d' > ${FINALCFG}.tmp
167
168  # Set our marker if we've found any
169  FOUNDMERGE="NO"
170
171  while read newline
172  do
173   echo ${newline} | grep -q "^#" 2>/dev/null
174   if [ $? -ne 0 ] ; then
175     VAL="`echo ${newline} | cut -d '=' -f 1`"
176     cat ${OLDCFG} | grep -q ${VAL} 2>/dev/null
177     if [ $? -ne 0 ] ; then
178       if [ "${FOUNDMERGE}" = "NO" ] ; then
179         echo "" >> ${FINALCFG}
180         echo "# Auto-merged values from newer ${NEWCFG}" >> ${FINALCFG}
181         FOUNDMERGE="YES"
182       fi
183       echo "${newline}" >> ${FINALCFG}
184     fi
185   fi
186  done < ${FINALCFG}.tmp
187  rm ${FINALCFG}.tmp
188
189};
190
191# Loop to check for a specified mount-point in a list
192check_for_mount()
193{
194  MNTS="${1}"
195  FINDMNT="${2}"
196
197  # Check if we found a valid root partition
198  for CHECKMNT in `echo ${MNTS} | sed 's|,| |g'`
199  do
200    if [ "${CHECKMNT}" = "${FINDMNT}" ] ; then
201      return 0
202    fi
203  done
204    
205  return 1
206};
207
208# Function which returns the next line in the specified config file
209get_next_cfg_line()
210{
211  CURFILE="$1"
212  CURLINE="$2"
213
214  FOUND="1"
215  
216  while read line
217  do
218    if [ "$FOUND" = "0" ] ; then
219      export VAL="$line"
220      return
221    fi
222    if [ "$line" = "${CURLINE}" ] ; then
223      FOUND="0"
224    fi
225  done <${CURFILE}
226
227  # Got here, couldn't find this line or at end of file, set VAL to ""
228  export VAL=""
229};
230