Project

General

Profile

Download (4.62 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
# functions.sh
3
# Library of functions which pc-sysinstall may call upon for parsing the config
4

    
5
# which gets the value of a setting in the provided line
6
get_value_from_string()
7
{
8
  if [ ! -z "${1}" ]
9
  then
10
    VAL="`echo ${1} | cut -d '=' -f 2`"
11
    export VAL
12
  else
13
    echo "Error: Did we forgot to supply a string to parse?"
14
    exit 1
15
  fi
16
};
17

    
18
# Get the value from the cfg file including spaces
19
get_value_from_cfg_with_spaces()
20
{
21
  if [ ! -z "${1}" ]
22
  then
23
    VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2`
24
    export VAL
25
  else
26
    exit_err "Error: Did we forgot to supply a setting to grab?"
27
  fi
28
};
29

    
30

    
31
# Get the value from the cfg file
32
get_value_from_cfg()
33
{
34
  if [ ! -z "${1}" ]
35
  then
36
    VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2 | tr -d ' '`
37
    export VAL
38
  else
39
    exit_err "Error: Did we forgot to supply a setting to grab?"
40
  fi
41
};
42

    
43
# Checks the value of a setting in the provided line with supplied possibilities
44
# 1 = setting we are checking,  2 = list of valid values
45
if_check_value_exists()
46
{
47
  if [ ! -z "${1}" -a ! -z "${2}" ]
48
  then
49
    # Get the first occurance of the setting from the config, strip out whitespace
50

    
51
    VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2 | tr -d ' '`
52
    if [ -z "${VAL}" ]
53
    then
54
      # This value doesn't exist, lets return
55
      return 0
56
    fi
57

    
58

    
59
    VALID="1"
60
    for i in ${2}
61
    do
62
      if [ "$VAL" = "${i}" ]
63
      then 
64
        VALID="0"
65
      fi
66
    done 
67
    if [ "$VALID" = "1" ]
68
    then
69
      exit_err "Error: ${1} is set to unknown value $VAL"
70
    fi
71
  else
72
    exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
73
  fi
74
};
75

    
76
# Checks the value of a setting in the provided line with supplied possibilities
77
# 1 = setting we are checking,  2 = list of valid values
78
check_value()
79
{
80
  if [ ! -z "${1}" -a ! -z "${2}" ]
81
  then
82
    # Get the first occurance of the setting from the config, strip out whitespace
83
    VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2 | tr -d ' '`
84
    VALID="1"
85
    for i in ${2}
86
    do
87
      if [ "$VAL" = "${i}" ]
88
      then 
89
        VALID="0"
90
      fi
91
    done 
92
    if [ "$VALID" = "1" ]
93
    then
94
      exit_err "Error: ${1} is set to unknown value $VAL"
95
    fi
96
  else
97
    exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
98
  fi
99
};
100

    
101
# Checks for the presense of the supplied arguements in the config file
102
# 1  = values to confirm exist
103
file_sanity_check()
104
{
105
  if [ ! -z "$CFGF" -a ! -z "$1" ]
106
  then
107
    for i in $1
108
    do
109
       grep "^${i}=" $CFGF >/dev/null 2>/dev/null
110
       if [ "$?" = "0" ]
111
       then
112
         LN=`grep "^${i}=" ${CFGF} | head -n 1 | cut -d '=' -f 2 | tr -d ' '`
113
         if [ -z "${LN}" ]
114
         then
115
           echo "Error: Config fails sanity test! ${i}= is empty"
116
           exit 1
117
         fi
118
       else
119
         echo "Error: Config fails sanity test! Missing ${i}="
120
         exit 1
121
       fi
122
    done
123
  else
124
    echo "Error: Missing config file, and / or values to sanity check for!"
125
    exit 1
126
  fi
127
};
128

    
129

    
130
# Function which merges the contents of a new config into the specified old one
131
# Only works with <val>= type configurations
132
merge_config()
133
{
134
  OLDCFG="${1}"
135
  NEWCFG="${2}"
136
  FINALCFG="${3}"
137

    
138
  # Copy our oldcfg to the new one, which will be used as basis
139
  cp ${OLDCFG} ${FINALCFG}
140

    
141
  # Remove blank lines from new file
142
  cat ${NEWCFG} | sed '/^$/d' > ${FINALCFG}.tmp
143

    
144
  # Set our marker if we've found any
145
  FOUNDMERGE="NO"
146

    
147
  while read newline
148
  do
149
   echo ${newline} | grep "^#" >/dev/null 2>/dev/null
150
   if [ "$?" != "0" ] ; then
151
     VAL="`echo ${newline} | cut -d '=' -f 1`"
152
     cat ${OLDCFG} | grep ${VAL} >/dev/null 2>/dev/null
153
     if [ "$?" != "0" ] ; then
154
       if [ "${FOUNDMERGE}" = "NO" ] ; then
155
         echo "" >> ${FINALCFG}
156
         echo "# Auto-merged values from newer ${NEWCFG}" >> ${FINALCFG}
157
         FOUNDMERGE="YES"
158
       fi
159
       echo "${newline}" >> ${FINALCFG}
160
     fi
161
   fi
162
  done < ${FINALCFG}.tmp
163
  rm ${FINALCFG}.tmp
164

    
165
};
166

    
167
# Loop to check for a specified mount-point in a list
168
check_for_mount()
169
{
170
  MNTS="${1}"
171
  FINDMNT="${2}"
172

    
173
  # Check if we found a valid root partition
174
  for CHECKMNT in `echo ${MNTS} | sed 's|,| |g'`
175
  do
176
    if [ "${CHECKMNT}" = "${FINDMNT}" ] ; then
177
      return 0
178
    fi
179
  done
180
    
181
  return 1
182
};
183

    
184
# Function which returns the next line in the specified config file
185
get_next_cfg_line()
186
{
187
  CURFILE="$1"
188
  CURLINE="$2"
189

    
190
  FOUND="1"
191
  
192
  while read line
193
  do
194
    if [ "$FOUND" = "0" ] ; then
195
      VAL="$line" ; export VAL
196
      return
197
    fi
198
    if [ "$line" = "${CURLINE}" ] ; then
199
      FOUND="0"
200
    fi
201
  done <${CURFILE}
202

    
203
  # Got here, couldn't find this line or at end of file, set VAL to ""
204
  VAL="" ; export VAL
205
};
(11-11/18)