~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/dist/util/upgrade_modules/inst_upgrade.sh

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# SGE configuration script (Upgrade/Downgrade)
 
4
# Scriptname: inst_upgrade.sh
 
5
# Module: common upgrade functions
 
6
#
 
7
#___INFO__MARK_BEGIN__
 
8
##########################################################################
 
9
#
 
10
#  The Contents of this file are made available subject to the terms of
 
11
#  the Sun Industry Standards Source License Version 1.2
 
12
#
 
13
#  Sun Microsystems Inc., March, 2001
 
14
#
 
15
#
 
16
#  Sun Industry Standards Source License Version 1.2
 
17
#  =================================================
 
18
#  The contents of this file are subject to the Sun Industry Standards
 
19
#  Source License Version 1.2 (the "License"); You may not use this file
 
20
#  except in compliance with the License. You may obtain a copy of the
 
21
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
22
#
 
23
#  Software provided under this License is provided on an "AS IS" basis,
 
24
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
25
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
26
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
27
#  See the License for the specific provisions governing your rights and
 
28
#  obligations concerning the Software.
 
29
#
 
30
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
31
#
 
32
#  Copyright: 2001 by Sun Microsystems, Inc.
 
33
#
 
34
#  All Rights Reserved.
 
35
#
 
36
##########################################################################
 
37
#___INFO__MARK_END__
 
38
 
 
39
 
 
40
#--------------------------------------------------------------------------
 
41
# WelcomeTheUserUpgrade - upgrade welcome message
 
42
#
 
43
WelcomeTheUserUpgrade()
 
44
{
 
45
   $INFOTEXT -u "\nWelcome to the Grid Engine Upgrade"
 
46
   $INFOTEXT "\nBefore you continue with the upgrade, read these hints:\n\n" \
 
47
             "   - Your terminal window should have a size of at least\n" \
 
48
             "     80x24 characters\n\n" \
 
49
             "   - At any time during the upgrade process, use your standard \n" \
 
50
             "     interrupt key to abort the upgrade. Typically, the interrupt \n" \
 
51
             "     key combination is Ctrl-C.\n\n" \
 
52
             "The upgrade procedure will take approximately 1-2 minutes.\n"
 
53
   $INFOTEXT -wait -auto $AUTO -n "Hit <RETURN> to continue >> "
 
54
   $CLEAR
 
55
}
 
56
 
 
57
#-------------------------------------------------------------------------------
 
58
# GetBackupedAdminUser: Get admin user form the cluster configuration backup
 
59
# TODO: Cleanup duplicit with inst_common.sh GetAdminUser()
 
60
GetBackupedAdminUser()
 
61
{
 
62
   ADMIN_USER=`BootstrapGetValue "$UPGRADE_BACKUP_DIR/cell" admin_user`
 
63
   euid=`$SGE_UTILBIN/uidgid -euid`
 
64
 
 
65
   TMP_USER=`echo "$ADMINUSER" |tr "A-Z" "a-z"`
 
66
   if [ \( -z "$TMP_USER" -o "$TMP_USER" = "none" \) -a $euid = 0 ]; then
 
67
      ADMINUSER=default
 
68
   fi
 
69
 
 
70
   if [ "$SGE_ARCH" = "win32-x86" ]; then
 
71
      HOSTNAME=`hostname | tr "a-z" "A-Z"`
 
72
      ADMINUSER="$HOSTNAME+$ADMINUSER"
 
73
   fi
 
74
}
 
75
 
 
76
#TODO: Use it in inst_sge
 
77
#-------------------------------------------------------------------------------
 
78
# FileGetValue: Get values from a file for appropriate key
 
79
#  $1 - PATH to the file
 
80
#  $2 - key: e.g: qmaster_spool_dir | ignore_fqdn | default_domain, etc.
 
81
FileGetValue()
 
82
{
 
83
   if [ $# -ne 2 ]; then
 
84
      $INFOTEXT "Expecting 2 arguments for FileGetValue. Got %s." $#
 
85
      exit 1
 
86
   fi
 
87
   if [ ! -f "$1" ]; then
 
88
      $INFOTEXT "No file %s found" $1
 
89
      exit 1
 
90
   fi
 
91
   echo `cat $1 | grep "$2" | awk '{ print $2}' 2>/dev/null`
 
92
}
 
93
 
 
94
#Helper to get bootstrap file values
 
95
#See FileGetValue
 
96
BootstrapGetValue()
 
97
{
 
98
   FileGetValue "$1/bootstrap" $2
 
99
}
 
100
 
 
101
#-------------------------------------------------------------------------------
 
102
# CheckUpgradeUser: Check if valid user performs the upgrade
 
103
#
 
104
CheckUpgradeUser()
 
105
{
 
106
   if [ $euid -ne 0 -a `whoami` != "$ADMINUSER" ]; then
 
107
      $INFOTEXT "\nUpgrade procedure must be started as a root or admin user.\nCurrent user is `whoami`."
 
108
      exit 1
 
109
   elif [ "$OLD_ADMIN_USER" != "$ADMIN_USER" ]; then
 
110
      $INFOTEXT "\nCannot use this backup for the upgrade.\n" \
 
111
                "   current admin user: '$OLD_ADMIN_USER'\n" \
 
112
                "   admin user in the backup: '$ADMIN_USER'\n" \
 
113
                "Seems like this is not a backup of this cluster. If you want really continue \n" \
 
114
                "with this backup manually edit bootstrap file in the backup to have the same \n" \
 
115
                "admin user as your cluster currently has."
 
116
      exit 1
 
117
   fi
 
118
}
 
119
 
 
120
#-------------------------------------------------------------------------------
 
121
# GetBackupDirectory: Ask for backup directory. Udes during the upgrade.
 
122
#
 
123
GetBackupDirectory()
 
124
{
 
125
   done=false
 
126
   while [ $done = false ]; do
 
127
      $CLEAR
 
128
      $INFOTEXT -u "\nType the complete path to the Grid Engine configuration backup directory."
 
129
      $INFOTEXT -n " Backup directory  >> "
 
130
      eval UPGRADE_BACKUP_DIR=`Enter $UPGRADE_BACKUP_DIR`
 
131
      version=`cat "${UPGRADE_BACKUP_DIR}/version" 2>/dev/null`
 
132
      backup_date=`cat "${UPGRADE_BACKUP_DIR}/backup_date" 2>/dev/null`
 
133
      if [ -n "$version" -a -n "$backup_date" -a -d "${UPGRADE_BACKUP_DIR}/cell" ]; then
 
134
         #Ask if correct
 
135
         $INFOTEXT -n "\nFound backup from $version version created on $backup_date"
 
136
         $INFOTEXT -auto $AUTO -ask "y" "n" -def "y" -n "\nContinue with this backup directory (y/n) [y] >> "
 
137
         if [ $? -eq 0 ]; then
 
138
            done=true
 
139
                 return 0
 
140
         fi
 
141
      else
 
142
         $INFOTEXT -n "\n$UPGRADE_BACKUP_DIR is not a valid backup directory!"
 
143
         $INFOTEXT -auto $AUTO -ask "y" "n" -def "y" -n "\nEnter a new backup directory or exit ('n') (y/n) [y] >> "
 
144
         if [ $? -ne 0 ]; then
 
145
            exit 0
 
146
         fi
 
147
      fi
 
148
   done
 
149
}
 
150
 
 
151
#-------------------------------------------------------------------------------
 
152
# RestoreCell: Restore backuped cell directory
 
153
#   $1 - BACKUPED_CELL_DIRECTORY
 
154
RestoreCell()
 
155
{
 
156
   old_cell=$1
 
157
   if [ ! -d  "$SGE_ROOT/$SGE_CELL/common" ]; then
 
158
      ExecuteAsAdmin mkdir -p "$SGE_ROOT/$SGE_CELL/common"
 
159
   fi
 
160
   
 
161
   FILE_LIST="bootstrap
 
162
qtask
 
163
sge_aliases
 
164
sge_request
 
165
shadow_masters
 
166
accounting
 
167
dbwriter.conf"
 
168
 
 
169
   for f in $FILE_LIST; do
 
170
      if [ -f "${old_cell}/$f" ]; then
 
171
         ExecuteAsAdmin cp -f "${old_cell}/$f" "$SGE_ROOT/$SGE_CELL/common"
 
172
      fi
 
173
   done
 
174
   
 
175
   #Modify bootstrap file
 
176
   ExecuteAsAdmin $CHMOD 666 "$SGE_ROOT/$SGE_CELL/common/bootstrap"
 
177
   #Version string
 
178
   ReplaceLineWithMatch "$SGE_ROOT/$SGE_CELL/common/bootstrap" 666 'Version.*' "Version: $SGE_VERSION"
 
179
   #ADMIN_USER
 
180
   if [ $ADMINUSER != default ]; then
 
181
      admin_user_value="admin_user              $ADMINUSER"
 
182
   else
 
183
      admin_user_value="admin_user              none"
 
184
   fi
 
185
   ReplaceLineWithMatch "$SGE_ROOT/$SGE_CELL/common/bootstrap" 666 'admin_user.*' "$admin_user_value"
 
186
   #TODO: default_domain, ignore_fqdn?
 
187
   #BINARY PATH
 
188
   ReplaceLineWithMatch "$SGE_ROOT/$SGE_CELL/common/bootstrap" 666 'binary_path.*' "binary_path             $SGE_ROOT/bin"
 
189
   #QMASTER SPOOL DIR
 
190
   ReplaceLineWithMatch "$SGE_ROOT/$SGE_CELL/common/bootstrap" 666 'qmaster_spool_dir.*' "qmaster_spool_dir       $QMDIR"
 
191
   #PRODUCT MODE
 
192
   ReplaceLineWithMatch "$SGE_ROOT/$SGE_CELL/common/bootstrap" 666 'security_mode.*' "security_mode           $PRODUCT_MODE"
 
193
   
 
194
   #Remove gdi_threads if present
 
195
   RemoveLineWithMatch "$SGE_ROOT/$SGE_CELL/common/bootstrap" 666 'gdi_threads.*'
 
196
   
 
197
   #Add threads info if missing
 
198
   cat $SGE_ROOT/$SGE_CELL/common/bootstrap | grep "threads" >/dev/null 2>&1
 
199
   do_bootstrap_upgrade=$?
 
200
   if [ $do_bootstrap_upgrade -eq 1 ]; then
 
201
      $ECHO "listener_threads        2" >> $SGE_ROOT/$SGE_CELL/common/bootstrap
 
202
      $ECHO "worker_threads          2" >> $SGE_ROOT/$SGE_CELL/common/bootstrap
 
203
      $ECHO "scheduler_threads       1" >> $SGE_ROOT/$SGE_CELL/common/bootstrap
 
204
   fi
 
205
   
 
206
   if [ "$SGE_ENABLE_JMX" = "true" ]; then
 
207
      ReplaceOrAddLine "$SGE_ROOT/$SGE_CELL/common/bootstrap" 666 'jvm_threads.*' "jvm_threads             1"
 
208
   else
 
209
      ReplaceOrAddLine "$SGE_ROOT/$SGE_CELL/common/bootstrap" 666 'jvm_threads.*' "jvm_threads             0"
 
210
   fi
 
211
   ExecuteAsAdmin $CHMOD 644 "$SGE_ROOT/$SGE_CELL/common/bootstrap"
 
212
}
 
213
 
 
214
#-------------------------------------------------------------------------------
 
215
# RestoreJMX: Ask if JMX setting from the backup should be used
 
216
#   $1 - BACKUPED_JMX_DIRECTORY
 
217
RestoreJMX()
 
218
{
 
219
   old_jmx=$1
 
220
   if [ "$SGE_ENABLE_JMX" = "true" -a -d "${old_jmx}" ]; then
 
221
      $CLEAR
 
222
      $INFOTEXT -n "\nFound JMX settings in the backup"
 
223
      $INFOTEXT -auto $AUTO -ask "y" "n" -def "y" -n "\nUse the JMX settings from the backup ('y') or reconfigure ('n') (y/n) [y] >> "
 
224
      if [ $? -eq 0 ]; then
 
225
         #Use backup
 
226
         ExecuteAsAdmin cp -r "${old_jmx}" "$SGE_ROOT/$SGE_CELL/common"
 
227
         SGE_ENABLE_JMX=false
 
228
      fi
 
229
   fi
 
230
}
 
231
 
 
232
#-------------------------------------------------------------------------------
 
233
# NewIJS: Ask if JMX setting from the backup should be used
 
234
#   $1 - BACKUPED_JMX_DIRECTORY
 
235
SavedOrNewIJS()
 
236
{
 
237
   $CLEAR
 
238
   newIJS=false
 
239
   $INFOTEXT -u "Interactive Job Support (IJS) Selection"   
 
240
   $INFOTEXT -auto $AUTO -ask "y" "n" -def "y" -n "\nThe backup configuration includes information for running \n" \
 
241
                                                  "interactive jobs. Do you want to use the IJS information from \n" \
 
242
                                                  "the backup ('y') or use new default values ('n') (y/n) [y] >> "
 
243
   if [ $? -ne 0 ]; then
 
244
      $INFOTEXT -n "\nUsing new interactive job support default setting for a new installation."
 
245
      newIJS=true
 
246
   fi
 
247
   $INFOTEXT -wait -auto $AUTO -n "\nHit <RETURN> to continue >> "
 
248
   $CLEAR
 
249
}
 
250
 
 
251
#-------------------------------------------------------------------------------
 
252
# AskForNewSequenceNumber: Ask for the next sequence number
 
253
#   $1 - file with the seq_number
 
254
#   $2 - "job" | "AR"
 
255
AskForNewSequenceNumber()
 
256
{
 
257
   NEXT_SEQ_NUMBER=""
 
258
   if [ ! -f "$1" ]; then #set to 0 is no file in backup and don't ask
 
259
      NEXT_SEQ_NUMBER=0
 
260
      return
 
261
   fi
 
262
   NEXT_SEQ_NUMBER=`cat "${1}" 2>/dev/null`
 
263
   if [ -z "$NEXT_SEQ_NUMBER" -o "$NEXT_SEQ_NUMBER" -lt 0 ]; then
 
264
      NEXT_SEQ_NUMBER=0
 
265
   else
 
266
      #Add 1000 and round up
 
267
      NEXT_SEQ_NUMBER=`expr $NEXT_SEQ_NUMBER / 1000 + 1`
 
268
      NEXT_SEQ_NUMBER=`expr $NEXT_SEQ_NUMBER \* 1000`
 
269
   fi
 
270
   if [ "$2" != "job" -a "$2" != "AR" ]; then
 
271
      $INFOTEXT "Invalid value '"$2"' provided to AskForNewSequenceNumber"
 
272
      exit 1
 
273
   fi
 
274
   $CLEAR
 
275
   $INFOTEXT -u "Provide a value to use for the next %s ID." "$2"
 
276
   $INFOTEXT -n "\nBackup contains last %s ID. As a suggested value, we added 1000 \n" \
 
277
                "to that number and rounded it to the nearest 1000.\n" \
 
278
                "Increase the value, if appropriate.\n" \
 
279
                          "Choose the new next %s ID [%s] >> " "$2" "$2" "$NEXT_SEQ_NUMBER"
 
280
   eval NEXT_SEQ_NUMBER=`Enter $NEXT_SEQ_NUMBER`
 
281
   $INFOTEXT -wait -auto $AUTO -n "\nHit <RETURN> to continue >> "
 
282
   $CLEAR
 
283
}
 
284
 
 
285
#-------------------------------------------------------------------------------
 
286
# RestoreSequenceNumberFiles: Ask for the next sequence number
 
287
#   $1 - qmaster_spool_dir
 
288
RestoreSequenceNumberFiles()
 
289
{
 
290
   QMASTER_SPOOL_DIR=$1
 
291
   AskForNewSequenceNumber "${UPGRADE_BACKUP_DIR}/jobseqnum" "job"
 
292
   SafelyCreateFile "$QMASTER_SPOOL_DIR/jobseqnum" 644 $NEXT_SEQ_NUMBER
 
293
   AskForNewSequenceNumber "${UPGRADE_BACKUP_DIR}/arseqnum" "AR"
 
294
   SafelyCreateFile "$QMASTER_SPOOL_DIR/arseqnum" 644 $NEXT_SEQ_NUMBER
 
295
}
 
296
 
 
297
#Copy of inst_execd.sh UnInstWinHelperSvc but modified for pre62
 
298
#TODO: needs to be done on every windows execd host
 
299
#TODO: cleanup duplicit with UnInstWinHelper()
 
300
UninstWinHelperPre62() {
 
301
   tmp_path=$PATH
 
302
   PATH=/usr/contrib/win32/bin:/common:$SAVED_PATH
 
303
   export PATH
 
304
        
 
305
   WIN_SVC="N1 Grid Engine Helper Service"
 
306
   WIN_DIR=`winpath2unix $SYSTEMROOT`
 
307
   $INFOTEXT " Testing, if windows helper service is installed!\n"
 
308
   eval "net pause \"$WIN_SVC\"" > /dev/null 2>&1
 
309
   ret=$?
 
310
   if [ "$ret" = 0 ]; then
 
311
      ret=2
 
312
      $INFOTEXT "   ... a service is installed!"
 
313
      $INFOTEXT -log "   ... a service is installed!"
 
314
      $INFOTEXT "   ... stopping service!"
 
315
      $INFOTEXT -log "   ... stopping service!"
 
316
 
 
317
      while [ "$ret" -ne 0 ]; do
 
318
         eval "net continue \"$WIN_SVC\"" > /dev/null 2>&1
 
319
         ret=$?
 
320
      done
 
321
   else
 
322
      $INFOTEXT "   ... no service installed!"   
 
323
      $INFOTEXT -log "   ... no service installed!"   
 
324
   fi
 
325
        
 
326
   if [ -f "$WIN_DIR"/N1_Helper_Service.exe ]; then
 
327
      $INFOTEXT "   ... found service binary!" 
 
328
      $INFOTEXT -log "   ... found service binary!" 
 
329
      $INFOTEXT "   ... uninstalling service!"
 
330
      $INFOTEXT -log "   ... uninstalling service!"
 
331
      $WIN_DIR/Sun_Helper_Service.exe -uninstall
 
332
      rm $WIN_DIR/Sun_Helper_Service.exe
 
333
   fi
 
334
 
 
335
   PATH=$tmp_path
 
336
   export PATH
 
337
}
 
338
 
 
339
#Select spooling method
 
340
# $1 - backued spooling method
 
341
SelectNewSpooling() 
 
342
{
 
343
   backuped_spooling_method=$1
 
344
   if [ "$backuped_spooling_method" != "berkeleydb" -a "$backuped_spooling_method" != "classic" ]; then
 
345
      $INFOTEXT "Invalid arg $1 to SelectNewSpooling"
 
346
      exit 1
 
347
   fi
 
348
        
 
349
   keep=false
 
350
        
 
351
   $CLEAR
 
352
   $INFOTEXT -auto $AUTO -ask "y" "n" -def "y" -n "\nUse backuped %s spooling method ('y') or use new spooling configuration ('n') (y/n) [y] >> " \
 
353
             $backuped_spooling_method
 
354
   if [ $? -eq 0 ]; then
 
355
      keep=true
 
356
      SPOOLING_METHOD=`BootstrapGetValue $SGE_ROOT/$SGE_CELL/common "spooling_method"`
 
357
      SPOOLING_LIB=`BootstrapGetValue $SGE_ROOT/$SGE_CELL/common "spooling_lib"`
 
358
      SPOOLING_ARGS=`BootstrapGetValue $SGE_ROOT/$SGE_CELL/common "spooling_params"`
 
359
      if [ "$SPOOLING_METHOD" = "berkeleydb" ]; then
 
360
         ExecuteAsAdmin rm -rf "$SPOOLING_ARGS"/*
 
361
      else #Classic
 
362
         tmp_spool=`echo $SPOOLING_ARGS | awk -F";" '{print $1}' | awk '{print $2}'`
 
363
         list="configuration
 
364
local_conf
 
365
sched_configuration"
 
366
         for f in $list; do
 
367
            ExecuteAsAdmin rm -rf "${tmp_spool}/${f}"
 
368
         done
 
369
      fi
 
370
   else
 
371
      SetSpoolingOptions
 
372
   fi
 
373
        
 
374
   if [ "$keep" = false ]; then
 
375
      ReplaceLineWithMatch "$SGE_ROOT/$SGE_CELL/common/bootstrap" 644 'spooling_method.*' "spooling_method         $SPOOLING_METHOD"
 
376
      ReplaceLineWithMatch "$SGE_ROOT/$SGE_CELL/common/bootstrap" 644 'spooling_lib.*'    "spooling_lib            $SPOOLING_LIB"
 
377
      ReplaceLineWithMatch "$SGE_ROOT/$SGE_CELL/common/bootstrap" 644 'spooling_params.*' "spooling_params         $SPOOLING_ARGS"
 
378
   fi
 
379
}
 
380
 
 
381
#AddDummyConfiguration - add a dummy configuration so we can start qmaster
 
382
#
 
383
AddDummyConfiguration() 
 
384
{
 
385
   CFG_EXE_SPOOL=$QMDIR/execd
 
386
   MAILER=mailx
 
387
   XTERM=xterm
 
388
   CFG_MAIL_ADDR=none
 
389
   CFG_GID_RANGE=20000-21000
 
390
   QLOGIN_COMMAND=builtin
 
391
   QLOGIN_DAEMON=builtin
 
392
   RLOGIN_COMMAND=builtin
 
393
   RLOGIN_DAEMON=builtin
 
394
   RSH_COMMAND=builtin
 
395
   RSH_DAEMON=builtin
 
396
   #TODO: add timestamp
 
397
   TMPC=/tmp/configuration
 
398
   rm -f $TMPC
 
399
   SafelyCreateFile $TMPC 666 ""
 
400
   PrintConf >> $TMPC 
 
401
   ExecuteAsAdmin $SPOOLDEFAULTS configuration $TMPC
 
402
   ExecuteAsAdmin rm -f $TMPC
 
403
}
 
404
 
 
405
#For copy upgrade type
 
406
PrepareConfiguration()
 
407
{
 
408
   $CLEAR
 
409
   tmp_range=`FileGetValue "$UPGRADE_BACKUP_DIR/configurations/global" "gid_range"`
 
410
   echo "$tmp_range" | grep -- "-" >/dev/null 2>&1
 
411
   if [ $? -ne 0 ]; then #single value
 
412
      GID_RANGE=`expr $tmp_range + 100`
 
413
   else                  #range
 
414
      tmp_start=`echo $tmp_range | awk -F"-" ' { print $1 }'`
 
415
      tmp_end=`echo $tmp_range | awk -F"-" ' { print $2 }'`
 
416
      tmp_range=`expr $tmp_end - $tmp_start`
 
417
      tmp_end=`expr $tmp_end + 100`
 
418
      GID_RANGE="${tmp_end}-`expr $tmp_end + $tmp_range`"
 
419
   fi
 
420
   GetConfiguration "$SGE_ROOT/$SGE_CELL/spool" `FileGetValue "$UPGRADE_BACKUP_DIR/configurations/global" "administrator_mail"`
 
421
}
 
422
 
 
423
ReplaceLineWithMatch()
 
424
{
 
425
   repFile="${1:?Need the file name to operate}"
 
426
   filePerms="${2:?Need file final permissions}"
 
427
   repExpr="${3:?Need an expression, where to replace}" 
 
428
   replace="${4:?Need the replacement text}" 
 
429
 
 
430
   #Return if no match
 
431
   grep "${repExpr}" $repFile >/dev/null 2>&1
 
432
   if [ $? -ne 0 ]; then
 
433
      return
 
434
   fi
 
435
   #We need to change the file
 
436
   ExecuteAsAdmin touch ${repFile}.tmp
 
437
   ExecuteAsAdmin chmod 666 ${repFile}.tmp
 
438
  
 
439
   SEP="|"
 
440
   echo "$repExpr $replace" | grep "|" >/dev/null 2>&1
 
441
   if [ $? -eq 0 ]; then
 
442
      echo "$repExpr $replace" | grep "%" >/dev/null 2>&1
 
443
      if [ $? -ne 0 ]; then
 
444
         SEP="%"
 
445
      else
 
446
         echo "$repExpr $replace" | grep "?" >/dev/null 2>&1
 
447
         if [ $? -ne 0 ]; then
 
448
            SEP="?"
 
449
         else
 
450
            $INFOTEXT "repExpr $replace contains |,% and ? characters: cannot use sed"
 
451
            exit 1
 
452
         fi
 
453
      fi
 
454
   fi
 
455
   #We need to change the file
 
456
   sed -e "s${SEP}${repExpr}${SEP}${replace}${SEP}g" "$repFile" >> "${repFile}.tmp"
 
457
   ExecuteAsAdmin mv -f "${repFile}.tmp"  "${repFile}"
 
458
   ExecuteAsAdmin chmod "${filePerms}" "${repFile}"
 
459
}
 
460
 
 
461
ReplaceOrAddLine()
 
462
{
 
463
   repFile="${1:?Need the file name to operate}"
 
464
   filePerms="${2:?Need file final permissions}"
 
465
   repExpr="${3:?Need an expression, where to replace}" 
 
466
   replace="${4:?Need the replacement text}" 
 
467
   
 
468
   #Does the pattern exists
 
469
   grep "${repExpr}" "${repFile}" > /dev/null 2>&1
 
470
   if [ $? -eq 0 ]; then #match
 
471
      ReplaceLineWithMatch "$repFile" "$filePerms" "$repExpr" "$replace"
 
472
   else                  #line does not exist
 
473
      echo "$replace" >> "$repFile"
 
474
   fi
 
475
}
 
476
 
 
477
#Remove line with maching expression
 
478
RemoveLineWithMatch()
 
479
{
 
480
   remFile="${1:?Need the file name to operate}"
 
481
   filePerms="${2:?Need file final permissions}"
 
482
   remExpr="${3:?Need an expression, where to remove lines}"
 
483
   
 
484
   #Return if no match
 
485
   grep "${remExpr}" $remFile >/dev/null 2>&1
 
486
   if [ $? -ne 0 ]; then
 
487
      return
 
488
   fi
 
489
 
 
490
   #We need to change the file
 
491
   ExecuteAsAdmin touch ${remFile}.tmp
 
492
   ExecuteAsAdmin chmod 666 ${remFile}.tmp
 
493
   sed -e "/${remExpr}/d" "$remFile" > "${remFile}.tmp"
 
494
   ExecuteAsAdmin mv -f "${remFile}.tmp"  "${remFile}"
 
495
   ExecuteAsAdmin chmod "${filePerms}" "${remFile}"
 
496
}