Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / bin / cygwin_setup
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         cygwin_setup
5 # Description:  This script will perform additional setup to configure the
6 #               local machine into the cygwin enviornment for Salira
7 # Author:       Andrew@DeFaria.com
8 # Created:      Fri Oct  5 15:30:16  2001
9 # Modified:
10 # Language:     Bash Shell
11 #
12 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
13 #
14 ################################################################################
15 # Set me to command name
16 me=$(basename $0)
17
18 declare -i max_steps=14
19 declare -i step=0
20
21 cygwin_setup_version="2.1"
22
23 function usage {
24   if [ "_$1" != "_" ]; then
25     echo "$me: Error: $1"
26     echo
27   fi
28   echo "Usage: $me [US | China]"
29   exit 1
30 } # usage
31
32 function step {
33   msg="$1"
34   let step=step+1
35
36   if [ $step -lt 10 ]; then
37     echo -e "Step  \c"
38   else
39     echo -e "Step \c"
40   fi
41
42   echo "$step of $max_steps: $msg"
43 } # step
44
45 function init {
46   subnet=$(ipconfig | grep "IP Address" | awk '{ print $NF }' | cut -f1 -d.)
47
48   if [[ $subnet = 192 || $subnet = 172 ]]; then
49     region=us
50   else
51     region=china
52   fi
53   
54   if [ "$region" != "us" -a "$region" != "china" ]; then
55     usage
56   fi
57   
58   # Determine what site parms to use
59   if [ "$region" = "us" ]; then
60     # Use US site parms
61     site_parms=/view/official/Tools/adm/etc/site_parms.US
62     . $site_parms
63   else
64     # Use Shanghai site parms
65     site_parms=/view/official/Tools/adm/etc/site_parms.CN
66     . $site_parms
67   fi
68   
69   adm="$SITE_TOOLS_PATH/adm"
70   
71   # Current machine's OS
72   OS=$(uname -s | cut -f2 -d-)
73   
74   # Current machine's hostname
75   hostname=$(echo $(hostname) | tr [:upper:] [:lower:])
76
77   # Announce ourselves
78   echo "Running $me version ($cygwin_setup_version) $hostname [Windows: $OS] in region: $region"
79 } # init
80
81 ################################################################################
82 #
83 # Setup home
84 #
85 ################################################################################
86 function setup_home_mount {
87   step "Setting up home mount"
88
89   # Home directory
90   mount -bsf "//$SITE_HOME_SERVER/$SITE_HOME_SHARENAME" /"$SITE_REGION"
91 } # setup_home_mount
92
93 ################################################################################
94 #
95 # Clearcase views
96 #
97 ################################################################################
98 function setup_clearcase_views {
99   step "Setting up view mount points"
100
101   if [ $hostname = $SITE_VIEW_SERVER ]; then
102     if [ $region = "us" ]; then
103       mount -bsf E:/Views "$SITE_SNAPSHOT_VIEW_PATH"
104     else
105       mount -bsf D:/ "$SITE_SNAPSHOT_VIEW_PATH"
106     fi
107   else
108     mount -bsf "$SITE_VIEW_STORAGE" "$SITE_SNAPSHOT_VIEW_PATH"
109   fi
110
111   # Mount dynamic views
112   mount -bsf M: "$SITE_DYNAMIC_VIEW_PATH"
113 } # setup_clearcase_views
114
115 ################################################################################
116 #
117 # Setup tools mount
118 #
119 ################################################################################
120 function setup_tools_mount {
121   step "Setting up tools mount point"
122
123   mount -bsf //$SITE_BUILD_SERVER/Tools /tools
124 } # setup_tools_mount
125
126 ################################################################################
127 #
128 # Setup release mount
129 #
130 ################################################################################
131 function setup_release_mount {
132   step "Set up release mount point"
133
134   mount -bsf //$(echo $SITE_RELEASE_SERVER | 
135     cut -f1 -d' ')/$(echo $SITE_RELEASE_PATH | cut -f1 -d' ') /release
136 } # setup_release_mount
137
138 ################################################################################
139 #
140 # Set cygdrive prefix to /dev
141 #
142 ################################################################################
143 function set_cygdrive_prefix {
144   step "Setting cygdrive-prefix to /dev"
145
146   mount -s --change-cygdrive-prefix /dev
147
148   # Remove user level cygdrive-prefix (Need to do this with regedit)
149   regedit /s $(cygpath -w "$SITE_TOOLS_PATH/bin/FixCygwin.reg")
150 } # set_cygdrive_prefix
151
152 ################################################################################
153 #
154 # Link passwd file
155 #
156 ################################################################################
157 function link_passwd_file {
158   step "Linking to common password file"
159
160   if [ ! -f /etc/passwd.local ]; then
161     if [ ! -L /etc/passwd ]; then
162       cp /etc/passwd /etc/passwd.local
163     fi
164   fi
165
166   if [ ! -L /etc/passwd ]; then
167     if [ "$OS" != "4.0" ]; then
168       rm /etc/passwd
169       ln -s "$SITE_TOOLS_PATH/adm/etc/passwd.$SITE_NAME" /etc/passwd
170     else
171       cp "$SITE_TOOLS_PATH/adm/etc/passwd.$SITE_NAME" /etc/passwd
172     fi
173   else
174    if [ "$OS" = "4.0" ]; then
175       # Fix up NT 4.0 machines (they don't like symlinked /etc/passwd files!)
176       rm /etc/passwd
177       cp "$SITE_TOOLS_PATH/adm/etc/passwd.$SITE_NAME" /etc/passwd
178     else
179       # Check to see if link is correct
180       linkname=$(ls -l /etc/passwd | awk '{print $NF}')
181       if [ "$linkname" != "$SITE_TOOLS_PATH/adm/etc/passwd.$SITE_NAME" ]; then
182         rm /etc/passwd
183         ln -s "$SITE_TOOLS_PATH/adm/etc/passwd.$SITE_NAME" /etc/passwd
184       fi
185     fi
186   fi
187 } # link_passwd_file
188
189 ################################################################################
190 #
191 # Link group file
192 #
193 ################################################################################
194 function link_group_file {
195   step "Linking to common group file"
196
197   if [ ! -f /etc/group.local ]; then
198     if [ ! -L /etc/group ]; then
199       cp /etc/group /etc/group.local
200     fi
201   fi
202
203   if [ ! -L /etc/group ]; then
204     rm /etc/group
205     ln -s "$SITE_TOOLS_PATH/adm/etc/group" /etc/group
206   else
207     # Check to see if link is correct
208     linkname=$(ls -l /etc/group | awk '{print $NF}')
209     if [ "$linkname" != "$SITE_TOOLS_PATH/adm/etc/group" ]; then
210       rm /etc/group
211       ln -s "$SITE_TOOLS_PATH/adm/etc/group" /etc/group
212     fi
213   fi
214 } # link_group_file
215
216 ################################################################################
217 #
218 # Link site_parms
219 #
220 ################################################################################
221 function link_site_parms {
222   step "Linking to common site_parms file"
223
224   if [ ! -L /etc/site_parms ]; then
225     rm /etc/site_parms
226     ln -s "$site_parms" /etc/site_parms
227   else
228     # Check to see if link is correct
229     linkname=$(ls -l /etc/site_parms | awk '{print $NF}')
230     if [ "$linkname" != "$SITE_TOOLS_PATH/adm/etc/site_parms" ]; then
231       rm /etc/site_parms
232       ln -s "$site_parms" /etc/site_parms
233     fi
234   fi
235 } # link_site_parms
236
237 ################################################################################
238 #
239 # Link /etc/profile
240 #
241 ################################################################################
242 function link_profile {
243   step "Linking to common profile file"
244
245   if [ ! -f /etc/profile.orig ]; then
246     if [ ! -L /etc/profile ]; then
247       cp /etc/profile /etc/profile.orig
248     fi
249   fi
250
251   if [ ! -L /etc/profile ]; then
252     rm /etc/profile
253     ln -s "$SITE_TOOLS_PATH/adm/etc/profile" /etc/profile
254   else
255     # Check to see if link is correct
256     linkname=$(ls -l /etc/profile | awk '{print $NF}')
257     if [ "$linkname" != "$SITE_TOOLS_PATH/adm/etc/profile" ]; then
258       rm /etc/profile
259       ln -s "$SITE_TOOLS_PATH/adm/etc/profile" /etc/profile
260     fi
261   fi
262 } # link_profile
263
264 ################################################################################
265 #
266 # Setup inetutil config
267 #
268 ################################################################################
269 function setup_inetutil_config {
270   step "Setup inetutil config"
271
272   # First save any pre-existing /etc/motd
273   if [ -f /etc/motd ]; then
274     cp /etc/motd /etc/motd.$$
275   fi
276
277   rm -f /etc/ftpusers /etc/ftpwelcome /etc/inetd.conf /etc/motd /etc/shells
278   iu-config > /dev/null
279
280   # In order to allow anonymous ftp access we need to clear /etc/ftpusers.
281   # Do this only for the $anonymous_ftp_server for now
282   if [ $hostname = $SITE_ANON_FTP_SERVER ]; then
283     cat /dev/null > /etc/ftpusers
284   fi
285
286   # Now replace that saved /etc/motd if it existed, otherwise remove the boring
287   # /etc/motd that iu-config creates. First check to see if the user has a 
288   # personalized /etc/motd in /etc/motd.save
289   if [ -f /etc/motd.save ]; then
290     # User had a personalized motd so move it into place and remove any prior
291     # copies
292     mv /etc/motd.save /etc/motd
293     rm -f /etc/motd.$$
294   elif [ -f /etc/motd.$$ ]; then
295     # Reinstall previous motd
296     # First update uname -a line
297     uname -a > /etc/motd
298
299     # Remove old uname -a line if present
300     grep -ve "^cygwin" /etc/motd.$$ >> /etc/motd.$$
301
302     # Cleanup
303     rm -f /etc/motd.$$
304   else
305     # No saved motd or previous motd. Remove /etc/motd which will cause us
306     # to prompt for the information later.
307     rm /etc/motd
308   fi
309
310   # Need to hardlink /usr/bin/cygwin1.dll & /usr/sbin/cygwin1.dll
311   # 12/17/2001: Stopped hardlinking cygwin1.dll. Enforcing having Windows system
312   # environment variables instead. For this we need Cygwin's bin in the path. 
313   # User should also set CYGWIN=ntsec in a Windows system environment variable.
314   if [ -f /usr/sbin/cygwin1.dll ]; then
315     rm -f /usr/sbin/cygwin1.dll
316     #ln /usr/bin/cygwin1.dll /usr/sbin/cygwin1.dll
317     echo "Warning: Please make sure that you have a Windows *SYSTEM* environment"
318     echo "         variable named CYGWIN set to the value of \"ntsec\" and that"
319     echo "         you have <Cygwin>\bin inserted into the Windows *SYSTEM*"
320     echo "         environment variable named PATH"
321   fi
322   
323   # Set up anonymous ftp iff we are on the $anonymous_ftp_server
324   if [ $hostname = $SITE_ANON_FTP_SERVER ]; then
325     # Toggle on write access to ~ftp/bin
326     chmod +w ~ftp/bin
327   
328     # Remove old copies of ls and cygwin1.dll
329     rm -f ~ftp/bin/ls.exe 
330     rm -f ~ftp/bin/cygwin1.dll
331   
332     # Install new copies (Note hardlinks will not work here since ~ftp/bin is
333     # on another file system. Doing an ln simply does a copy anyway)
334     # 12/17/2001: Skipping copying of cygwin1.dll as noted above
335     cp /bin/cygwin1.dll ~ftp/bin/cygwin1.dll
336     cp /bin/ls.exe ~ftp/bin/ls.exe
337   
338     # Set security
339     chmod 555 ~ftp/bin/cygwin1.dll
340     chmod 111 ~ftp/bin/ls.exe
341     chown Administrator ~ftp/bin/cygwin1.dll 
342     chown Administrator ~ftp/bin/ls.exe
343     chmod -w ~ftp/bin
344   fi
345 } # setup_inetutil_config
346   
347 ################################################################################
348 #
349 # Setup inetd
350 #
351 ################################################################################
352 function setup_inetd {
353   step "Setup inetd"
354
355   # Install inetd as a service
356   /usr/sbin/inetd --install-as-service
357   
358   # Start inetd service
359   inetd_started=$(net start | grep -i inetd)
360   
361   if [ -z "$inetd_started" ]; then
362     net start inetd > /tmp/inetd.log 2>&1 
363
364     if [ $? -ne 0 ]; then
365       echo "ERROR: Unable to start inetd service"
366       cat /tmp/inetd.log
367       return
368     else
369       rm -f /tmp/inetd.log
370     fi
371   fi
372 } # setup_inetd
373
374 ################################################################################
375 #
376 # Setup ssh
377 #
378 ################################################################################
379 function setup_ssh {
380   step "Setup ssh"
381
382   # Remove previous service (if any)
383   cygrunsrv -R sshd > /dev/null 2>&1 
384
385   # Set up hosts configuration
386   echo "ntsec smbntsec" | ssh-host-config -y > /tmp/ssh-host-config.log 2>&1
387
388   # Check for error
389   if [ $? -ne 0 ]; then
390     echo "ERROR: An error occurred setting up ssh:"
391     cat /tmp/ssh-host-config.log
392     return
393   fi
394
395   # Start service
396   net start sshd > /tmp/ssh-host-config.log 2>&1 
397
398   if [ $? -ne 0 ]; then
399     echo "ERROR: Unable to start ssh service:"
400     cat /tmp/ssh-host-config.log
401     return
402   else
403     rm -f /tmp/ssh-host-config.log
404   fi
405 } # setup_ssh
406
407 ################################################################################
408 #
409 # Setup ssmtp
410 #
411 ################################################################################
412 function setup_ssmtp {
413   step "Setup ssmtp"
414   
415   # Setup SMTP
416   $SITE_TOOLS_PATH/adm/bin/setup_ssmtp
417 } # setup_ssmtp
418
419 ################################################################################
420 #
421 # Setup cron
422 #
423 ################################################################################
424 function setup_cron {
425   step "Setup cron"
426   
427   # Setup cron
428   $SITE_TOOLS_PATH/adm/bin/setup_cron
429 } # setup_cron
430
431 ################################################################################
432 #
433 # Create /etc/motd
434 #
435 ################################################################################
436 function create_motd {
437   step "Gathering machine specific information"
438
439   if [ ! -f /etc/motd ]; then
440     $SITE_TOOLS_PATH/adm/bin/make_motd
441     made_motd=true
442   else
443     echo "Skipped: Machine info already gathered"
444   fi
445
446   # Fixup /etc/ftpwelcome
447   host=$(hostname | tr [:upper:] [:lower:])
448   echo "Welcome to $host's ftp service" > /etc/ftpwelcome
449 } # create_motd
450
451 ################################################################################
452 #
453 # Update machines file
454 #
455 ################################################################################
456 function update_machines_file {
457   step "Registering this machine"
458
459  if [ ! -z "$made_motd" ]; then
460     $SITE_TOOLS_PATH/adm/bin/update_machine_info
461   else
462     echo "Skipped: Machine already registered"
463   fi
464 } # update_machines_file
465
466 ################################################################################
467 #
468 # Main
469 #
470 ################################################################################
471 init
472 setup_home_mount
473 setup_clearcase_views
474 setup_tools_mount
475 setup_release_mount
476 set_cygdrive_prefix
477 link_passwd_file
478 link_group_file
479 link_site_parms
480 link_profile
481 setup_inetutil_config
482 setup_inetd
483 setup_ssmtp
484 setup_cron
485 setup_ssh