Provide support for installation under OpenBSD. Don't override PATH in the install...
[oweals/cde.git] / cde / admin / IntegTools / dbTools / mkProd
1 #! /bin/ksh 
2
3 export SYSTYPE=bsd4.3
4
5 # This script is aimed at producing Apollo product tree but
6 # it should work as well on hp-ux systems.  There is at least
7 # one special rule "-n" which should not be generated on hp-ux systems
8 # where it is not necessary to run nroff on the man page sources.
9
10
11 # Put "/etc" in the path
12 # because this is where "chown" appears on bsd4.3 systems (i.e. apollo).
13 # Put "/usr/apollo/bin" on the path to access "chacl" on apollo systems.
14
15 export PATH=":/etc:$PATH:/usr/apollo/bin:"
16 typeset STRIP=""
17
18
19 # this alias makes for more "beautiful" output.
20 alias echo=print
21
22 #
23 # Shell script to read a file full of destination/perm/source triples and
24 # create data base entries in a format suitable for 8.0 release.  All other
25 # information defaults to the default values for now -- this should be correct
26 # in most cases but may require editing of the result for those cases
27 # needing special handling.
28 #
29 # Lst file syntax:
30 # [<install-flags>]  <destination_path> <perm> <src> [ <type> <owner> <group> <status> <processor> <resp_proj> ]
31 #
32 # <install-rule>       ==>  [optional] 2 character field encoding special actions to be invoked when
33 #                           creating the product tree.  In the absence of this field the file is subject
34 #                           to defaults based on its name,destination directory and permissions.
35 #                            valid values: 
36 #                               --      Do nothing special
37 #                               c-      compress the file
38 #                               -n      run it through an nroff pipeline
39 #                               -s      strip the file (should be a binary executable)
40 #
41 # <destination_path>    ===> full path name on target system
42 # <perm>                ===> numeric permission (with leading 0)
43 # <src>                 ===> build tree path name (relative to build tree root)
44 #                            | full path name of link source
45 #                            | "-" to ignore this field (source for directory not required)
46 # <type>                ===> "file" | "empty_dir" | "directory" | "hard_link" | NULL
47 # <owner>               ===> file owner name -- defaults to bin
48 # <group>               ===> file group ownership --- defaults to bin
49 # <status>              ===> "di----" default
50 # <processor>           ===> valid processor "3-" | "-8" | "38" -- defaults to "38"
51 # <resp_proj>           ===> responsible project --- defaults to "xwindows"
52 #
53
54 Usage="Usage: $0 [-f filesetName] -D <desttree> -S <srctree> <file> ...\n
55 \tfilesetName \t==>   (optional) used to create subdirectory in desttree\n
56 \tdesttree \t==>   destination directory\n
57 \tsrctree \t==>   usually a build tree containing the source\n
58 \tfile \t\t==>   data base file in \".lst\" format"
59
60 FILE='file'
61 STATUS='di----'
62 PROCESSOR='378'
63 DESTTREE='.'
64
65 if [ -f /com/ctnode ]; then
66         # Apollo systems
67         OWNER='root'
68         GROUP='staff'
69         # set flag used to test for soft links
70         LNKTST="L"
71 else
72         # hp-ux systems (and all other systems)
73         OWNER='bin'
74         GROUP='bin'
75         # set flag used to test for soft links
76         LNKTST="h"
77 fi
78
79 BUILD_TARGET='/'
80 INSTALL_RULE='--'
81 INSTALL_FLAGS=''
82 ORDER='0'
83 RESP_PROJ='xwindows'
84
85 # Set which system we are on
86 BUILDSYSTEM=$(uname)
87
88 # Set system
89 if [ $BUILDSYSTEM = "AIX" ];
90 then
91         PLATFORM=aix
92 elif [ $BUILDSYSTEM = "SunOS" ];
93 then
94         PLATFORM=sun
95 elif [ $BUILDSYSTEM = "UNIX_SV" ];
96 then
97         PLATFORM=usl
98 elif [ $BUILDSYSTEM = "UNIX_System_V" ];
99 then
100         PLATFORM=uxp
101 elif [ $BUILDSYSTEM = "OSF1" ];
102 then
103         PLATFORM=dec
104 else                                                # Build system = HP
105         PLATFORM=hp-ux
106 fi
107
108 if [ $PLATFORM = "aix" ];
109 then
110         USER=`/bin/whoami`
111 elif [ $PLATFORM = "sun" -o $PLATFORM = "usl" -o $PLATFORM = "uxp" ];
112 then
113         USER=`/usr/ucb/whoami`
114 else
115         USER=`/usr/bin/whoami`
116 fi
117
118 awkit() {
119         awk  '
120         BEGIN {
121                 print "#" 
122                 print "# TYPE RULE SRC DEST MODE OWNER GROUP LINKSRC"
123                 print ""
124         }
125
126         /^[     ]*#/ { next }
127         {
128                 # initialize install_rule to NULL
129                 install_rule=""
130
131                 # skip blank lines
132                 if ( NF == 0 )
133                         next
134                 # do a little syntax checking
135                 
136                 if ( $1 !~ /\// ) {
137                         # check for special install_rule values
138                         if ( $1 ~ /[-c][-ns]/ ) {
139                                 # set the special install rule
140                                 install_rule=$1
141
142                                 # shift the rest of the fields left
143                                 for ( i = 1; i < NF; i++) {
144                                         $i = $(i+1)
145                                 }
146                                 NF=NF-1
147                         }
148                         else {
149                                 print "Syntax Error (line " NR ") destination path: " $1
150                                 print "text: "  $0
151                                 next
152                         }
153                 }
154         
155                 if ( NF > 9 ) {
156                         print "Syntax Error (line " NR ") number of fields"
157                         print "text: "  $0
158                         exit
159                 }
160                 
161                 if ( $2 !~ /0[0-7][0-7]*/ ) {
162                         print "Syntax Error (line " NR ") perm  : " $2 
163                         print "text: "  $0
164                         next
165                 }
166 #               if ( $4 ~ /link/ && $3 !~ /^\// && $3 !~ /`node_data/) {
167 # changed to allow local link (using ./file syntax) -- Should this be
168 # domain only?
169 #
170                 if ( $4 ~ /link/ && $3 !~ /^\// && $3 !~ /`node_data/ && \
171                         $3 !~ /^\.\// && $3 !~ /^\.\.\// ) {
172                         print "Syntax Error (line " NR ") source: " $3
173                         print "text: "  $0
174                         next
175                 }
176
177 # Check for special install rules
178 # man pages are compressed use rule "c-"
179 # font files are compressed use rule "c-"
180 # directories have no source i.e. src = "-"
181
182                 src = $3;
183                 link_src = "";
184                 type = "'$FILE'";
185                 owner = "'$OWNER'";
186                 group = "'$GROUP'";
187                 status = "'$STATUS'";
188                 processor = "'$PROCESSOR'";
189                 resp_proj = "'$RESP_PROJ'";
190                 if ( $4 != "" )
191                 {
192                         type = $4;
193                         if ( $4 ~ /link/ ) {
194                                 src = "";
195                                 if ( $3 ~ /^\// || $3 ~ /`node_data/ || \
196                                         $3 ~ /^\.\// || $3 ~ /^\.\.\// )
197                                         link_src = $3;
198                                 else {
199                                         print "Syntax Error (line " NR ") link_src: " $3
200                                         print "text: "  $0
201                                         next;
202                                 };
203                         }
204                         else if ( $4 == "directory" )
205                                 if ( $3 != "-" ) 
206                                 {
207                                         print "Syntax Error (line " NR ") directory: " $3;
208                                         print "text: "  $0;
209                                         next;
210                                 }
211                                 else
212                                         src="";
213                         else if ( $4 == "empty_dir" )
214                         {
215                                 resp_proj = "common";
216                                 status = "------";
217                                 if ( $3 != "-" )
218                                 {
219                                         print "Syntax Error (line " NR ") empty_dir: " $3;
220                                         print "text: "  $0;
221                                         next;
222                                 }
223                                 else
224                                         src="";
225                         }
226                         else if ( $4 == "file" && $3 == "-" )
227                                 if ( $7 == "---cu-")
228                                         src = "";
229                 }
230                 #
231                 # If owner an/or group are specified all previous fields (including
232                 # type) must be specified.
233                 #
234                 if ( $5 != "" )
235                         owner = $5;
236
237                 if ( $6 != "" )
238                         group = $6;
239
240                 if ( $7 != "" )
241                         status = $7
242                         
243                 if ( $8 != "" )
244                         processor = $8;
245
246                 if ($9 != "" )
247                         resp_proj= $9;
248
249                 if ( install_rule != "" ) {
250                         ; # install rule has been explicitly set -- do nothing
251                 }
252                 else if ( $1 ~ /\/usr\/man\/man.*\.Z\// )
253                 {
254                         install_rule = "c-";
255                 }
256                 else if ( $1 ~ /\/usr\/X11\/man\/cat[1-9]\/.*[1-9]/ )
257                 {
258                         install_rule = "-n";
259                 }
260                 else if ( $1 ~ /\/bsd4.3\/usr\/man\/cat[1-9]\/.*[1-9]/ )
261                 {
262                         install_rule = "-n";
263                 }
264                 else if ( $1 ~ /\/sys5.3\/usr\/catman\/.*man[1-9]\/.*[1-9]/ )
265                 {
266                         install_rule = "-n";
267                 }
268                 else if ( $1 ~ /.*\/fonts\/.*.scf$/ && $3 ~ /.*.snf$/ )
269                 {
270                         install_rule = "c-";
271                 }
272                 else if ( $1 ~ /.*\/fonts\/.*.snf\.Z$/ && $3 ~ /.*.snf$/ )
273                 {
274                         install_rule = "c-";
275                 }
276                 else if ( $1 ~ /^\/integration\// && $3 ~ /^doc\/upinfo\// )
277                 {
278                         install_rule = "";
279                 }
280                 else if ( $2 ~ /[1357]/ && $4 == "file" )
281                 {       # executable files may be stripped
282                         install_rule = "-s";
283                 }
284                 else
285                         install_rule = "'$INSTALL_RULE'";
286                         
287                         
288 #               print "install_target           : " $1
289 #               print "fileset                  : " "'$FILESET'"
290 #               print "type                     : " type
291 #               print "status                   : " status
292 #               print "processor                : " processor
293 #               print "mode                     : " $2
294 #               print "owner                    : " owner
295 #               print "group                    : " group
296 #               print "link_source              : " link_src
297 #               print "build_target             : " "'$BUILD_TARGET'"
298 #               print "install_rule_name        : " install_rule
299 #               print "install_flags            : " "'$INSTALL_FLAGS'"
300 #               print "a_out_location           : " src
301 #               print "order                    : " "'$ORDER'"
302 #               print "responsible_project      : " resp_proj
303 #               print "#"
304
305                 if ( type ~ /.*link/ )
306                         src= link_src
307                 else if ( type ~ /.*directory/ )
308                         src="-"
309
310                 print type, install_rule, src, $1, $2, owner ,group
311         }
312
313         '  $1
314 }
315
316 #
317 # createPath
318 #       function to create a directory path.  (mkdir -p replacement on bsd4.3)
319 #
320 createPath()
321 {
322         if [ $# -ne 1 ]; then
323                 echo "ERROR: \"$0\" invalid argument count" >&2
324                 echo "   ARGS=$@"
325                 return 1
326         fi
327         
328         # Since we are SYSTYPE bsd4.3 "-x" won't work.
329         if [ -r /sys5.3/bin/mkdir ]; then
330                 /sys5.3/bin/mkdir -p $1
331                 return
332         fi
333         if hp9000s300 > /dev/null 2>&1 || hp9000s800 > /dev/null 2>&1; then
334                 mkdir -p $1
335                 return
336         fi 
337         
338         #
339         # We can't use mkdir -p so do the hard way.
340         # starting at the root check/create all the directories
341         # in the given path
342         #
343
344         typeset xdirs=""
345         typeset nextdir=""
346         typeset fulldirs
347
348         # provide extra slash for apollo systems (//hp* syntax)?
349
350         if [ "$1" = "${1##/}" ]; then
351                 # not a full path name so attach path to current directory
352                 fulldirs=`/bin/pwd`/$1
353                 xdirs="/"    # added to support apollo "//host" syntax
354         elif [ "$1" != "${1##//}" ]; then
355                 # using "//host" syntax
356                 fulldirs=$1
357                 xdirs="/"
358         else
359                 fulldirs=$1
360                 xdirs=""
361         fi
362
363         echo creating path: $1 
364
365         while nextdir=`expr /$fulldirs/ : '/*\([^/]*\)/.*'`; do
366                 xdirs=$xdirs/$nextdir
367                 if [ ! -d $xdirs ]; then
368                         mkdir $xdirs
369                 fi
370
371                 fulldirs=`expr $fulldirs : '/*[^/]*/\(.*\)'`
372         done
373         return 0        
374 }
375
376 # doit
377 #       function to put the source file into the product tree.  This function 
378 #       reads its stdin for file movement rules.
379 #
380 doit()
381 {
382         if [ $# -ne 0 ]; then
383                 echo "ERROR: \"$0\"  incorrect arg count" >&2
384                 echo "ARGS: $@" >&2
385         fi
386         
387         typeset TYPE
388         typeset RULE
389         typeset SRC
390         typeset DEST
391         typeset MODE
392         typeset OWNER
393         typeset GROUP
394         
395         while read TYPE RULE SRC DEST MODE OWNER GROUP; do
396                 # ignore blank lines
397                 if [ -z "$TYPE" ]; then
398                         continue;
399                 fi
400                 # ignore comment lines (match longest string starting with '#')
401                 if [ "${TYPE###}" != "$TYPE" ]; then
402                         continue
403                 fi
404
405                 DEST=$DESTTREE/$FILESET${DEST}
406                 CMD=""
407
408                 case $TYPE in
409                 directory)
410                         if [ ! -d "$DEST" ]; then
411                                 CMD="createPath $DEST"
412                         fi
413                         ;;
414                 file)   
415                         SRC=$SRCTREE/$SRC
416                         if [ "$CHECK_BEFORE" = "yes" ]
417                         then
418                             if [ ! -f $SRC ]
419                             then
420                                 echo "Missing: $SRC"
421                                 echo "Missing: $SRC" >>/tmp/mkprod.missing
422                                 continue
423                             fi
424                         fi
425                         if [ ! -f $SRC ]; then
426                                 echo "ERROR: Cannot read \"$SRC\"." >&2
427                                 echo "  LINE=$TYPE $RULE $SRC $DEST $MODE $OWNER $GROUP " >&2
428                                 continue
429                         fi
430                         case $RULE in
431                         c-) # compress rule
432                                 CMD="compress -c $SRC > $DEST"
433                                 ;;
434                         -n) # nroff rule (for apollo man pages)
435                                 if egrep -c '^\.TH[     ]|^\.ds[        ]' $SRC > /dev/null ; then
436                                         CMD="tbl $SRC|eqn|nroff -man|col -b> $DEST"
437                                 else
438                                         CMD="cp $SRC $DEST"
439                                 fi
440                                 ;;
441                         -s) # strip rule 
442                                         # Domain Only
443 #                               if [ "$(find $SRC -typeuid coff -print)" =  "$SRC" ]; then 
444                                         # hp-ux
445                                 if [ -z "$STRIP" ]; then
446                                         CMD="cp $SRC $DEST"
447                                 else if file $SRC | grep -q "not stripped"; then 
448                                         CMD="cp $SRC $DEST; strip $DEST"
449                                 else
450                                                 CMD="cp $SRC $DEST"
451                                 fi
452                                 fi
453                                 ;;
454                         --) # default (no special processing)
455                                 CMD="cp $SRC $DEST"
456                                 ;;
457                         *)  # huh?
458                                 echo "ERROR: Unrecognized rule: \"$RULE\"" >&2
459                                 echo "  LINE=$TYPE $RULE $SRC $DEST $MODE $OWNER $GROUP " >&2
460                                 continue;
461                                 ;;
462                         esac
463                         ;;
464                 sym_link)
465                         if [ -z "$SRC" ]; then
466                                 echo "ERROR: No link source for \"$DEST\"" >&2
467                                 echo "  LINE=$TYPE $RULE $SRC $DEST $MODE $OWNER $GROUP "  >&2
468                                 continue;
469                         fi
470                         if [ "${SRC#\`node_data}" != "$SRC" ]; then
471                                 # apollo-style special link
472                                 echo "WARNING: encountered special link: $SRC" >&2
473                                 # try to make link to this SRC by adding a
474                                 # backslash to protect the tic mark.
475                                 SRC=\\$SRC
476                         elif [ "${SRC#/}" = "$SRC" ]; then
477 #                               echo "ERROR: Relative link \"$SRC\"" >&2
478                         #       echo "WARNING: Relative link \"$SRC\"" >&2
479                         #       echo "  LINE=$TYPE $RULE $SRC $DEST $MODE $OWNER $GROUP "  >&2
480                                 # Protect the potential variant link variable
481                                 # (eg lnk_$(LNK))
482                                 SRC=\'$SRC\'
483 #                               continue;
484                         fi
485                         CMD="ln -s $SRC $DEST"
486                         # Remove existing link if one already exists
487                         [ -${LNKTST} "$DEST" ] && rm $DEST
488                         ;;
489                 hard_link)
490                         # Hard links are assumed to be made only to other
491                         # files in the submittal -- not to existing system
492                         # files.
493                         SRC="$DESTTREE/$FILESET${SRC}"
494                         if [ -z "$SRC" ]; then
495                                 echo "ERROR: No link source for \"$DEST\"" >&2
496                                 echo "  LINE=$TYPE $RULE $SRC $DEST $MODE $OWNER $GROUP"  >&2
497                                 continue;
498                         fi
499                         CMD="ln $SRC $DEST"
500                         ;;
501                 *)      echo "ERROR: Unrecognized type: \"$TYPE\"" >&2
502                         echo "  LINE=$TYPE $RULE $SRC $DEST $MODE $OWNER $GROUP" >&2
503                         continue;
504                         ;;
505                 esac
506
507                 # createPath issues its own error messages
508                 if [ ! -d ${DEST%/*} ]; then
509                         createPath ${DEST%/*} || continue
510                 fi
511                 [ -z "$CMD" ] && continue
512                 eval "$CMD" || {
513                         echo "ERROR: \"$CMD\" failed" >&2
514                         echo "  LINE=$TYPE $RULE $SRC $DEST $MODE $OWNER $GROUP" >&2;
515                         continue;
516                 }
517                 # set permissions for non-links
518                 if [ "${TYPE%link}" = "$TYPE" ]; then
519                         if [ "$USER" = "root" ]; then
520                                 chmod $MODE $DEST || 
521                                         echo "ERROR: \"chmod $MODE $DEST\" failed" >&2
522                                 chgrp $GROUP $DEST || 
523                                         echo "ERROR: \"chgrp $GROUP $DEST\" failed" >&2
524                                 chown $OWNER $DEST || 
525                                         echo "ERROR: \"chown $OWNER $DEST\" failed" >&2
526                         fi
527                 fi
528
529                 echo $TYPE $RULE $SRC $DEST $MODE $OWNER $GROUP
530         done
531
532 }
533
534 ###################################################
535 #       MAIN BODY
536 ###################################################
537
538 #
539 # Check Path for chmod, chown, and chgrp
540 #
541 whence chmod > /dev/null || {
542         echo "ERROR: \"chmod\" not in PATH." >&2
543         exit 1
544 }
545 whence chown > /dev/null || {
546         echo "ERROR: \"chown\" not in PATH." >&2
547         exit 1
548 }
549 whence chgrp > /dev/null || {
550         echo "ERROR: \"chgrp\" not in PATH." >&2
551         exit 1
552 }
553
554 CHECK_BEFORE="no"
555
556 while [ $# -ne 0 ]  ; do
557         case $1 in
558         -f*)
559                 FILESET=${1#-f}
560                 if [ -z "$FILESET" ]; then
561                         if [ $# -ge 1 ]; then
562                                 shift;
563                                 FILESET=$1
564                         else
565                                 echo $Usage
566                                 exit 1;
567                         fi
568                 fi;;
569         -S*)    SRCTREE=${1#-S}
570                 if [ -z "$SRCTREE" ]; then
571                         if [ $# -ge 1 ]; then
572                                 shift;
573                                 SRCTREE=$1
574                         else
575                                 echo $Usage
576                                 exit 1;
577                         fi
578                 fi
579                 ;;
580         -D*)    DESTTREE=${1#-D}
581                 if [ -z "$DESTTREE" ]; then
582                         if [ $# -ge 1 ]; then
583                                 shift;
584                                 DESTTREE=$1
585                         else
586                                 echo $Usage
587                                 exit 1;
588                         fi
589                 fi
590                 ;;
591
592         -P*)    CHECK_BEFORE="yes"
593                 ;;
594
595         *)      files="$files $1";;
596         esac
597         if [ $# -ge 1 ]; then
598                 shift;
599         fi
600 done
601
602 if [ -z "$files" -o -z "$SRCTREE" ]; then
603         echo $Usage >&2
604         exit 1;
605 fi
606
607 if [ ! -d "$SRCTREE" ]; then
608         echo "ERROR: Cannot access \"$SRCTREE\"." >&2
609         exit 1
610 fi
611 if [ ! -d "$DESTTREE" ]; then
612         echo "ERROR: Cannot access \"$DESTTREE\"." >&2
613         exit 1
614 fi
615
616 umask 022
617 if [ -f /com/ctnode ]; then
618         #
619         # Make sure berkely style permissions are applied to the product tree
620         # This should cause directories to be created with permissions based
621         # on the umask (which is otherwise ignored on "open security model" 
622         # file systems).
623         #
624         chacl -RB $DESTTREE
625 fi
626
627 # Debug Stmts
628 echo FILESET=$FILESET
629 echo files=$files
630 echo SRCTREE=$SRCTREE
631 echo DESTTREE=$DESTTREE
632 echo "#-------------- $(date) --------------"
633 echo ""
634
635 TOOL_DIR=$SRCTREE/admin/IntegTools/dbTools
636
637 #
638 # Avoid making duplicate entries
639 # make sure links are referred to AFTER the files to which they point.
640 #
641 if [ $PLATFORM = "uxp" ]; then
642   $TOOL_DIR/uncomment $files | sort | uniq | $TOOL_DIR/linksLast - > /tmp/awk.in
643   cat /tmp/awk.in | awkit > /tmp/awk.out
644   cat /tmp/awk.out | doit
645 else
646   $TOOL_DIR/uncomment $files | sort | uniq | $TOOL_DIR/linksLast - | awkit | doit
647 fi
648
649 echo ""
650 echo "#-------------- $(date) --------------"