66# Author: Jordan Michaels (jordan@viviotech.net)
77# Copyright: Jordan Michaels, 2010-2018, All rights reserved.
88#
9- # Usage: change_user.sh [ username] [install dir] [engine ] [nobackup ]
9+ # Usage: change_user.sh username /path/to/installdir [nobackup ] [servicename ]
1010#
11- # [username] must start with a lower-case letter and must be alpha-
12- # numeric
13- # [engine] must be either "lucee" or "openbd". Engine name is used
14- # in the control script name, such as "lucee_ctl" or "openbd_ctl"
11+ # username must start with a lower-case letter and must be alphanumeric
12+ # servicename is optional, defaults to lucee_ctl
1513# ----------------------------------------------------------------------------------
1614#
1715# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
7472 myInstallDir=$2 ;
7573fi
7674
77- # the default when [nobackup] is not specified is always to backup control scripts
78- if [[ -z " $3 " ]]; then
79- echo " lucee_ctl will be backed up to lucee_ctl.old" ;
80- myControlNeedsBackup=1;
81- else
82- if [[ " $3 " = " openbd" ]]; then
83- echo " Error: OpenBD is no longer supported by this script." ;
84- exit 1;
85- else
86- if [[ -z " $4 " ]]; then
87- echo " lucee_ctl will be backed up to lucee_ctl.old" ;
88- myControlNeedsBackup=1;
89- else
90- # At this point $3 and $4 both exist.
91- # The legacy $3 argument (lucee) is ignored,
92- # but the old usage is supported to avoid breaking change!
93- if [[ " $3 " = " nobackup" || " $4 " = " nobackup" ]]; then
94- echo " lucee_ctl will NOT be backed up!" ;
95- myControlNeedsBackup=0;
96- else
97- echo " lucee_ctl will be backed up to lucee_ctl.old" ;
98- myControlNeedsBackup=1;
99- fi
100- fi
75+ # default service name
76+ myServiceName=" lucee_ctl"
77+
78+ # parse optional arguments
79+ myControlNeedsBackup=1
80+ for arg in " $3 " " $4 " ; do
81+ if [[ " $arg " = " nobackup" ]]; then
82+ myControlNeedsBackup=0
83+ elif [[ -n " $arg " && " $arg " != " lucee" ]]; then
84+ # treat non-empty, non-legacy values as service name
85+ myServiceName=" $arg "
10186 fi
87+ done
88+
89+ if [[ ${myControlNeedsBackup} -eq 1 ]]; then
90+ echo " ${myServiceName} will be backed up to ${myServiceName} .old" ;
91+ else
92+ echo " ${myServiceName} will NOT be backed up!" ;
10293fi
10394
10495# ##################
@@ -120,7 +111,7 @@ function checkUserExists {
120111function checkGroupExists {
121112 echo -n " Checking to see if group exists..." ;
122113 myGroupNeedsCreating=0;
123- if id -g " ${myUserName} " > /dev/null 2>&1 ; then
114+ if getent group " ${myUserName} " > /dev/null 2>&1 ; then
124115 echo " [FOUND]" ;
125116 else
126117 echo " [NOT FOUND]" ;
@@ -174,28 +165,28 @@ function processControlScriptTemplate {
174165function rebuildControlScript {
175166 echo " Rebuilding Control Scripts for new User..." ;
176167 # backup current control script
177- if [[ ${myControlNeedsBackup} -eq 1 ]]; then
178- # If we're backing up, do it
179- mv ${myInstallDir} /lucee_ctl ${myInstallDir} /lucee_ctl .old;
168+ if [[ ${myControlNeedsBackup} -eq 1 && -f " ${myInstallDir} / ${myServiceName} " ]]; then
169+ # If we're backing up and file exists , do it
170+ mv ${myInstallDir} /${myServiceName} ${myInstallDir} /${myServiceName} .old;
180171 else
181172 # otherwise, just remove the old file
182- rm -f ${myInstallDir} /lucee_ctl
173+ rm -f ${myInstallDir} /${myServiceName}
183174 fi
184-
175+
185176 # create the control script from easier to maintain separate template
186- TomcatControlScript=" ${myInstallDir} /lucee_ctl " ;
177+ TomcatControlScript=" ${myInstallDir} /${myServiceName} " ;
187178 local scriptDir=" $( dirname " ${BASH_SOURCE[0]} " ) "
188-
179+
189180 # Process the template to generate the control script
190181 processControlScriptTemplate " ${scriptDir} /lucee_ctl_template" " $TomcatControlScript "
191-
182+
192183 # make it executable
193- chmod 744 $TomcatControlScript ;
184+ chmod 744 $TomcatControlScript ;
194185
195186 # see if there's a control script in the init directory
196- if [[ -f /etc/init.d/lucee_ctl ]]; then
187+ if [[ -f /etc/init.d/${myServiceName} ]]; then
197188 # if there is, copy the new control script over it
198- cp -f $TomcatControlScript /etc/init.d/lucee_ctl ;
189+ cp -f $TomcatControlScript /etc/init.d/${myServiceName} ;
199190 fi
200191}
201192
0 commit comments