Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / adm / bin / update_machine_info
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         update_machine_info
5 # Description:  Updates machine info file ($adm_base/data/machines)
6 # Author:       Andrew@DeFaria.com
7 # Created:      Fri Apr 30 14:13:56 PDT 1999
8 # Language:     Korn Shell
9 #
10 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
11 #
12 ################################################################################
13 # Set me to command name
14 me=$(basename $0)
15
16 # Set adm_base
17 adm_base=${adm_base:-//sonscentral/Corporate/Software/adm}
18
19 # Set adm_fpath
20 adm_fpath=${adm_fpath:-$adm_base/functions}
21
22 # Source functions
23 . $adm_fpath/common
24
25 # Set machines
26 machines=${machines:-$adm_base/data/machines}
27
28 tmp_file=/tmp/machines.$$
29 machine=$(uname -n | tr [:upper:] [:lower:])
30 ip=$(ipconfig | grep "IP Address" | awk '{ print $NF }')
31 mod=$(uname -m)
32 osversion=$(uname -s | sed 's/CYGWIN_//')
33
34 verbose=false
35
36 while getopts vd OPT; do
37   case $OPT in
38     v) verbose=true
39     ;;
40
41     d) debug=true
42     ;;
43   esac
44 done
45
46 cleartool=$(type -p cleartool)
47 debug "cleartool = $cleartool"
48
49 if [ ! -z "$cleartool" ]; then
50   ccversion=$("$cleartool" -version | head -1)
51 else
52   ccversion="Non ClearCase Machine"
53 fi
54
55 debug "ccversion = $ccversion"
56 if [[ $ccversion = *ClearCase\ LT* ]]; then
57   debug "Found Clearcase LT"
58   ccversion="Lite $(echo $ccversion | cut -c30-40)"
59 elif [[ $ccversion = *ClearCase\ version* ]]; then
60   debug "Found Clearcase version"
61   ccversion="Full $(echo $ccversion | cut -f3 -d' ')"
62 fi
63
64 debug "ccversion = $ccversion"
65 # Unix doesn't really have a way to store such information such as owner,
66 # usage and location. Attempt to ascertain this info from /etc/motd.
67 if [ -f /etc/motd ]; then
68   owner=$(grep "^Owner:" /etc/motd | tr -s " " | cut -f2- -d" ")
69   usage=$(grep "^Usage:" /etc/motd | tr -s " " | cut -f2- -d" ")
70   phone=$(grep "^Phone:  " /etc/motd | tr -s " " | cut -f2- -d" ")
71   class=$(grep "^Class:" /etc/motd | tr -s " " | cut -f2- -d" ")
72   location=$(grep "^Location:" /etc/motd | tr -s " " | cut -f2- -d" ")
73 fi
74
75 if [ -z "$owner" ]; then
76   owner="Unknown"
77 fi
78
79 if [ -z "$usage" ]; then
80   usage="Unknown"
81 fi
82
83 if [ -z "$phone" ]; then
84   phone="Unknown"
85 fi
86
87 if [ -z "$class" ]; then
88   class="Unknown"
89 fi
90
91 if [ -z "$location" ]; then
92   location="Unknown"
93 fi
94
95 rm -f $tmp_file
96
97 if [ $verbose = "true" ]; then
98   echo "Machine: $machine"
99   echo "IP Address: $ip"
100   echo "Model: $mod"
101   echo "OS Version: $osversion"
102   echo "ClearCase Version: $ccversion"
103   echo "Owner: $owner"
104   echo "Phone: $phone"
105   echo "Usage: $usage"
106   echo "Class: $class"
107   echo -e "Location: $location\n"
108   echo -e "Updating machine list...\c"
109 fi
110
111 # Add machine if not already present
112 grep -i "^$machine" $machines > /dev/null 2>&1
113
114 if [ $? -ne 0 ]; then
115   echo "$machine" >> $machines
116 fi
117
118 while read line; do
119   if [ "$(echo $line | cut -f1 -d:)" = $machine ]; then
120     echo "$machine:$ip:$mod:$osversion:$ccversion:$owner:$phone:$usage:$class:$location:" >> $tmp_file
121   else
122     echo $line >> $tmp_file
123   fi
124 done < $machines
125
126 mv $tmp_file $machines
127
128 if [ $verbose = "true" ]; then
129   echo "done"
130 fi