Merge branch 'master' of https://git.code.sf.net/p/cdesktopenv/code
[oweals/cde.git] / cde / admin / IntegTools / dbTools / udbToAny.ksh
1 #!/bin/ksh 
2
3 # ------------------------------------------------------------- 
4 #  udbToAny.ksh 
5
6 #     This script was leveraged from "databaseConversionTool.ksh" 
7 # it should provide a superset of the functionality of that script; 
8 # however the primary motivation was to get away from the use of
9 # multiple divergent parsers for the ".udb" style database.  The
10 # parser has been moved into an awk "library" file: udbParseLib.awk.
11 # this parser can and should be used by all scripts wishing to parse
12 # the ".udb" data bases. 
13 #
14 # ----------------------------
15 # new features:
16 #
17 #  -toDB                to convert to old mksubmit-style ".db" format
18 #  -toLst               to convert to old domain-port-style ".lst" files
19 #  -toCustom <prt>      to specify a print routine for a custom format
20 #  -custom <awklib>     to specify a library containing custom print routines.
21 #  -udbParseLib <awkLib> to specify a library containing an alternate parser. 
22 #  -mailTo  <user>      to specify an administrator who will receive mail
23 #                       concerning parsing errors. 
24 #
25 #  -Database            is now obsolete (although it will still work)
26 #  <udbfile> ... ...    The script can now take multiple udb files (Only the
27 #                       defaults specified in the first udb file will apply).
28 #  
29 # ----------------------------
30 #
31 #     This script converts a universal database to an 
32 # HP OSF style System Definition Database (SDD) file,
33 # or a set of args suitable to supply to the Chelmsford deliver
34 # tool, or a set of commands to build a delivery tree.
35 # For more details, please refer to the "X11 and VUE for HP_OSF/1.0"
36 # document.
37 #
38 #  This script was leveraged (read hacked extensively) from
39 #  the "udbToDb" script by Jim Andreas.  Ron Voll authored the
40 #  original "udbToDb" script.
41 #
42 # -------------------------------------------------------------
43
44 # usage:  databaseToSDD.ksh Option udbFile
45 #
46 # where Option is one of the following:
47 #
48 # -toDB              convert a .udb to ".db" format
49 # -toLst             convert a .udb to ".lst" format
50 # -toSDD             convert a .udb to SDD format
51 # -toDeliverArgs     convert a .udb to args that the deliver tool likes
52 # -toReleaseTree     convert a .udb to a script to build a delivery tree
53 # -toCheckBuild      convert a .udb to a script to check the items in a build
54 # -toFileList        convert a .udb to a list of files for carbon units
55 # -Machine           specifies 3,7,8 for hp-ux releases
56 # -ReleaseStream     {hp-ux, osf, whatever} 
57 # -NoDefaults        do not convert any records labeled default
58 # -toTargetList      convert a .udb to a list of target files in product tree
59 #                      the leading path is stripped and just the target
60 #                      file is left - for easy diffing with some other
61 #                      version of a release
62 # -custom <awkFile>             Supply a custom "awk" print library
63 # -toCustom <prt routine>
64 # -udbParseLib <awkFile>        Supply an alternate  "awk" parser library
65
66
67 # -------------------------------------------------------------
68 #  ConvertRoutine
69
70 #   This ksh function invokes awk to do all of the dirty
71 # work.  The DoAction variable is consulted only in the
72 # final stages of printing the desired info after the
73 # .udb "phrases" are parsed.
74 #
75 # -------------------------------------------------------------
76 ConvertRoutine()
77 {
78
79
80 #
81 # set what we are going to do
82 #
83     typeset DoAction=$1
84
85 #
86 # set the "release stream" or token in a block in the .udb
87 # file for which we will scan.
88 #
89     typeset BlockToken=$2
90
91
92 # and for HP-UX releases, the particular machine 68k/Snake/S800 3/7/8
93 #
94     typeset machine=$3
95
96 # set flag if default blocks are to be processed
97 #
98     typeset UseDefaultBlocks=$4
99
100     shift
101     shift
102     shift
103     shift
104
105     AWK=/usr/bin/awk
106     if [ -x /usr/bin/nawk ]; then
107         AWK=/usr/bin/nawk
108     fi
109
110 #
111 # Custom print routines may use the following parser variables:
112 #       defOrder[]      --- An array containing the names of the fields in
113 #                           a data base record (in the correct order).
114 #       NumEntries      --- One MORE than the number of entries in the
115 #                           "defOrder" array.  This is the number of fields
116 #                           in a data base record.
117 #       rec[]           --- An associative array indexed by data base record
118 #                           field name containing the value of the field.
119 #
120 # Assign custom print routines to be used for output.  The default is to
121 # use the "printDb" function associated with the library.
122 #
123
124     typeset     PRT=printDb
125     case "$DoAction" in
126     toDB)
127         PRT=printDb
128         ;;
129     toLst)
130         PRT=printLst
131         ;;
132     toFileList|toTargetList|toCheckBuild|toReleaseTree|toDeliverArgs|toSDD)
133         PRT=printGather;
134         ;;
135     toCustom) 
136         CUSTOM_PRINT="-f $CUSTOM_PRINT_LIB"
137         PRT=$CUS_PRT
138         ;;
139     *)  # Unknown Action
140         echo "$0: Unknown Action>> \"$doAction\""
141         exit 1;
142         ;;
143     esac
144
145    cat > $TMPFILE <<EOF
146 #
147 # The function name "PRTREC" is used by the parsing routines
148 # to do the output. By providing a custom output function you
149 # can print the database any way you want.  The default is to
150 # use the "printRecord" function built defined in the awk file
151 # containing the awk parser.
152 #
153 function PRTREC(rec) {
154         $PRT(rec)
155 }
156 BEGIN {
157         parseUdb()
158 }
159 {
160         print "Getting New Line AWK style -- Problem?"          
161         exit 1
162 }
163 EOF
164
165    #
166    # Create a single awk file for use with the "-f" parameter.
167    # IBM's awk only allows one "-f"
168    #
169     cat "$UDB_PARSE_LIB" >> $TMPFILE
170     [ -z "$CUSTOM_PRINT" ]  || cat "$CUSTOM_PRINT_LIB" >> $TMPFILE
171
172     $AWK -v mailTo="$Administrator" \
173          -v action="$DoAction" \
174          -v BlockToken="$BlockToken" \
175          -v Machine="$machine" \
176          -v UseDefaultBlocks="$UseDefaultBlocks" \
177          -v DeBugFile="$DEBUGFILE" \
178          -v DeBug="$DEBUGLEVEL" \
179          -f $TMPFILE $*
180
181
182 #
183 # Removed from parameter list because IBM's awk only allows one "-f"
184 #        $CUSTOM_PRINT \
185 #        -f "$UDB_PARSE_LIB" \
186 #
187
188      rm $TMPFILE
189 }
190
191 #
192 #  print a handy usage message to stderr (file descriptor 2 )
193 #
194 #
195 usage()
196 {
197     exec >&2
198
199     echo "$ScriptName: usage:" 
200     echo ""
201     echo "  $ScriptName [Options] <UdbFile> ..."
202     echo ""
203     echo "     -toDB              convert a .udb to \".db\" format"
204     echo "     -toLst              convert a .udb to \".lst\" format"
205     echo "     -toSDD             convert a .udb to SDD format"
206     echo "     -toDeliverArgs     convert a .udb to args that the deliver tool likes"
207     echo "     -toReleaseTree     convert a .udb to a script to build a delivery tree"
208     echo "     -toCheckBuild      convert a .udb to a script to check a build"
209     echo "     -toFileList        convert a .udb to a list of files"
210     echo "     -toTargetList      convert a .udb to a list of product files"
211     echo "     -ReleaseStream     {hp-ux, osf, whatever}"
212     echo "     -Machine           specifies machine 3,7,8 for hp-ux"
213     echo "     -NoDefaults        do not convert any records labeled \"default\""
214     echo "     -Database path     (obsolete) specifies full path to the .udb file to convert"
215     echo "   -mailTo  <user>      Specify a user to receive mail on errors."
216     echo "   -toCustom  <prt>     Specify the name of a custom print routine."  
217     echo "   -custom <awkFile>    Supply a custom "awk" print library."
218     echo " -udbParseLib <awkFile> Supply an alternate 'awk' parser library"
219     exit 1
220 }
221
222
223 # OK, here is where we really start execution.
224 #   Check that the first argument defines what this script is
225 #  supposed to do:
226
227 #    Obscurity footprint-in-the-sand:  "${1##*/}" is equivalent
228 #      to basename(1)
229 #
230 ScriptName=${0##*/}
231
232 # -toSDD             convert a .udb to SDD format
233 # -toDeliverArgs     convert a .udb to args that the deliver tool likes
234 # -toReleaseTree     convert a .udb to a script to build a delivery tree
235 # -toCheckBuild      convert a .udb to a script to check the items in a build
236
237 if [ $# -le 3 ]; then
238     usage $0
239 fi
240
241 typeset TakeDefaultBlocks="Y"
242 typeset Administrator=""
243 #typeset DBTOOLSRC=/x/toolsrc/dbTools
244 typeset DBTOOLSRC=`dirname $0`
245 typeset UDB_PARSE_LIB="$DBTOOLSRC/udbParseLib.awk"
246 typeset CUSTOM_PRINT_LIB=""
247 typeset DEBUGFILE="/dev/tty"
248 typeset DEBUGLEVEL=0
249 typeset TMPFILE=`mktemp /tmp/awkXXXXXXXXXXXXXXXXXXXXX`
250
251 if [ $# -gt 2 ]; then 
252     while [ $# -gt 0 ]
253     do
254         case $1 in 
255         -NoDefaults)
256             TakeDefaultBlocks=N
257             shift
258             continue
259             ;;
260         -toDB)
261             Action=toDB
262             shift;
263             continue;
264             ;;
265         -toLst)
266             Action=toLst
267             shift;
268             continue;
269             ;;
270         -toSDD)
271             Action=toSDD
272             shift
273             continue
274             ;;
275         -toDeliverArgs)
276             Action=toDeliverArgs
277             shift
278             continue
279             ;;
280         -toReleaseTree)
281             Action=toReleaseTree
282             shift
283             continue
284             ;;
285         -toCheckBuild)
286             Action=toCheckBuild
287             shift
288             continue
289             ;;
290         -toTargetList)
291             Action=toTargetList
292             shift
293             continue
294             ;;
295         -toFileList)
296             Action=toFileList
297             shift
298             continue
299             ;;
300         -Machine)
301             if [ "x$2" = "x" ]; then
302                 usage
303             fi
304             Machine=$2
305             shift
306             shift
307             continue
308             ;;
309         -ReleaseStream)
310             if [ "x$2" = "x" ]; then
311                 usage
312             fi
313             ReleaseStream=$2
314             shift
315             shift
316             continue
317             ;;
318         -Database)
319             if [ "x$2" = "x" ]; then
320                 usage
321             fi
322             if [ ! -r "$2" ]; then 
323                 usage
324             fi
325             Database="$Database $2"
326             shift
327             shift
328             continue
329             ;;
330         -udbParseLib) # specify alternate "awk" parser location
331             if [ "x$2" = "x" ]; then
332                 usage
333             fi
334             if [ ! -r "$2" ]; then 
335                 usage
336             fi
337             UDB_PARSE_LIB=$2
338             shift
339             shift
340             continue
341             ;;
342         -toCustom) # specify custom "awk" print routines
343             if [ "x$2" = "x" ]; then
344                 usage
345             fi
346             Action=toCustom
347             CUS_PRT=$2
348             shift
349             shift
350             continue
351             ;;
352         -custom) # specify custom "awk" print routines
353             if [ "x$2" = "x" ]; then
354                 usage
355             fi
356             if [ ! -r "$2" ]; then 
357                 usage
358             fi
359             CUSTOM_PRINT_LIB=$2
360             shift
361             shift
362             continue
363             ;;
364         -mailTo) # specify an administrator who receives mail about errors.
365             if [ "x$2" = "x" ]; then
366                 usage
367             fi
368             Administrator=$2
369             shift
370             shift
371             continue
372             ;;
373         -DeBugFile) # specify a debug file and debug level for parser debug info
374             if [ "x$2" = "x" ]; then
375                 usage
376             fi
377             if [ "x$3" = "x" ]; then
378                 usage
379             fi
380             DEBUGFILE=$2
381             shift 2
382             continue
383             ;;
384         -DeBugLevel) # specify a debug level for parser debug info
385             if [ "x$2" = "x" ]; then
386                 usage
387             fi
388             if [ "x$3" = "x" ]; then
389                 usage
390             fi
391             DEBUGLEVEL=$2
392             shift 2
393             continue
394             ;;
395         -*)
396             echo "unknown option: $1"
397             echo ""
398             usage
399             exit 1;
400             ;;
401         *) if [ ! -r $1 ]; then
402                 usage
403            fi
404            Database="$Database $1"
405            shift;
406            ;;
407         esac
408     done
409 fi
410
411 if [ "$Action" = "toCustom" ]; then
412         if [ -z "$CUSTOM_PRINT_LIB" ]; then
413                 echo "You Must specify an awk file containing the custom print routine \"$CUS_PRT\""
414                 exit 1;
415         fi
416 fi
417
418
419 if [ "$Machine" = "" ]; then
420     Machine=NA
421 elif [ "$Machine" = "300" ]; then
422     Machine="3"
423 elif [ "$Machine" = "700" ]; then
424     Machine="7"
425 elif [ "$Machine" = "800" ]; then
426     Machine="8"
427 fi
428 if [ "$ReleaseStream" = "" ]; then
429     echo "$ScriptName: need to specify a -ReleaseStream" >&2
430     exit 1
431 fi
432 if [ "$Database" = "" ]; then
433     echo "$ScriptName: need to specify a -Database" >&2
434     exit 1
435 fi
436
437
438 ConvertRoutine $Action $ReleaseStream $Machine $TakeDefaultBlocks $Database