fontaliases: add more robust font.aliases file.
[oweals/cde.git] / cde / programs / dtprintegrate / dtprintegrate.src
1 XCOMM!/bin/ksh
2 XCOMM $XConsortium: dtprintegrate.src /main/5 1996/04/23 12:02:04 drk $
3 XCOMM ####################################################################
4 XCOMM ##  File:              dtprintegrate
5 XCOMM ##
6 XCOMM ##  Default Location:  /usr/dt/bin/dtprintegrate
7 XCOMM ##
8 XCOMM ##  Purpose:           Set up the desktop configuration files for 
9 XCOMM ##                     the desktop components.
10 XCOMM ##
11 XCOMM ##  Description:       This script is invoked as a means to create the
12 XCOMM ##                     desktop configuration files for newly installed
13 XCOMM ##                     printers on a given host.
14 XCOMM ##
15 XCOMM ##  Invoked by:        System administrators, either by hand or by means
16 XCOMM ##                     of the printer installation script.
17 XCOMM ##
18 XCOMM ##  Product:           @(#)Common Desktop Environment 1.0
19 XCOMM ##          
20 XCOMM ##                     (c) Copyright 1993, 1994 Hewlett-Packard Company
21 XCOMM ##                     (c) Copyright 1993, 1994 International Business 
22 XCOMM ##                            Machines Corp.
23 XCOMM ##                     (c) Copyright 1993, 1994 Sun Microsystems, Inc.
24 XCOMM ##
25 XCOMM ##
26 XCOMM ##  Note:              Please do not modify this file.
27 XCOMM ##                     Later product updates will overwrite this file.
28 XCOMM ##
29 XCOMM ####################################################################
30 #define HASH #
31
32 XCOMM #################################################################
33 XCOMM ##  Internal Globals
34 XCOMM ##
35 XCOMM ##  Set default values.
36 XCOMM ##
37 COMMAND_NAME=dtprintegrate
38
39 ACTION_STUB_FILE=/usr/dt/appconfig/appmanager/C/Desktop_Apps/Dtcalc
40
41 base_icon_name="Fpprnt"
42
43 XCOMM
44 XCOMM Return codes
45 XCOMM
46 SUCCESS=0
47 USAGE_ERR=1
48 CREATE_ERR=2
49 NO_WRITE_ERR=3
50
51 XCOMM #################################################################
52 XCOMM ##  Initialize()
53 XCOMM ##
54 XCOMM ##         Do upfront processing.
55 XCOMM ##
56 XCOMM #################################################################
57 Initialize()
58 {
59     if (( ${#LANG} ))
60     then
61         if [[ $LANG != C ]]
62         then
63             non_default_lang=1
64         fi
65     fi
66
67     HASH
68     HASH Location for Print action files...
69     HASH
70     DEFAULT_FOLDER=/etc/dt/appconfig/appmanager/C/Printers
71     DTPRINTERFOLDER=${DTPRINTERFOLDER:-$DEFAULT_FOLDER}
72  
73     if (( ${#DTUSERPRINTERFOLDER} ))
74     then
75             DTPRINTERFOLDER="$DTUSERPRINTERFOLDER"
76     fi
77
78     if [ ! -d $DTPRINTERFOLDER ]
79     then
80         mkdir -p $DTPRINTERFOLDER > /dev/null  2>/dev/null
81     fi
82
83     HASH
84     HASH Location for Print definition (*.dt) files...
85     HASH
86     DEFAULT_FOLDER=/etc/dt/appconfig/types/C
87     DTPRINTACTIONFOLDER=${DTPRINTACTIONFOLDER:-$DEFAULT_FOLDER}
88
89     if [ ! -d $DTPRINTACTIONFOLDER ]
90     then
91         mkdir -p $DTPRINTACTIONFOLDER > /dev/null  2>/dev/null
92     fi
93
94     HASH
95     HASH Search path for printer icons...
96     HASH
97     DEFAULT_PATH=.,$HOME/.dt/icons,/usr/dt/appconfig/icons/C
98     DTPRINTICONPATH=${DTPRINTICONPATH:-$DEFAULT_PATH}
99
100     HASH
101     HASH Location for printer icons...
102     HASH
103     DEFAULT_FOLDER=/etc/dt/appconfig/icons/C
104     DTPRINTERICONFOLDER=${DTPRINTERICONFOLDER:-$DEFAULT_FOLDER}
105
106     HASH
107     HASH Other...
108     HASH
109     database_file=${printer_name}.dt
110
111     databasefile_path=$DTPRINTACTIONFOLDER/$database_file
112
113     action_file=${printer_name}_Print
114
115     action_path=$DTPRINTERFOLDER/$action_file
116 }
117
118 XCOMM #################################################################
119 XCOMM ##  CheckOptions()
120 XCOMM ##
121 XCOMM ##         Check the options supplied in the command line 
122 XCOMM ##            interface
123 XCOMM ##
124 XCOMM #################################################################
125 CheckOptions()
126 {
127         if (( $printer_flag == "0" ))
128         HASH
129         HASH  We require a printer specification
130         HASH
131         then
132             PrintUsage
133             Exit $USAGE_ERR
134         fi
135 }
136
137 XCOMM #################################################################
138 XCOMM ##  AddHelpFileContents
139 XCOMM ##
140 XCOMM ##         Add the contents of the help file to $1
141 XCOMM ##
142 XCOMM #################################################################
143 AddHelpFileContents()
144 {
145     if [[ ! -r $help_file || ! -s $help_file ]]
146     then
147             print ""
148             print "     Sorry--Unable to read the help file: "
149             print "             \"$help_file\"."
150             print ""
151             failure_flag=1
152             if (( $verbose_flag ))
153             then
154                 PrintEndLog
155             fi
156             Exit $CREATE_ERR
157     fi
158     echo "      DESCRIPTION             \\" >> $databasefile_path
159
160     exec 8< $help_file
161
162     while read -u8 this_line
163     do
164         print $this_line " \\" >> $databasefile_path 
165     done
166     print "**" >> $databasefile_path 
167
168     exec 8<&-
169 }
170
171 XCOMM #################################################################
172 XCOMM ##  MakeDatabaseFile()
173 XCOMM ##
174 XCOMM ##         Make the *.dt file for the printer
175 XCOMM ##
176 XCOMM #################################################################
177 MakeDatabaseFile()
178 {
179
180         touch $databasefile_path > /dev/null 2>/dev/null
181
182         chmod +w $databasefile_path > /dev/null 2>/dev/null
183
184         if [[ ! -f $databasefile_path  || ! -w $databasefile_path ]]
185         then
186                 failure_flag=1
187                 PrintCreateError "$DTPRINTACTIONFOLDER" "$database_file"
188                 if (( $verbose_flag ))
189                 then
190                     PrintEndLog
191                 fi
192                 Exit $CREATE_ERR
193         fi
194
195         HASH
196         HASH First, write the preamble for this *.dt file
197         HASH
198         (
199         echo "################################################################"
200         echo "# "
201         echo "#   Actions and Datatypes for Printer \"$printer_name\""
202         echo "# "
203         echo "#   Common Desktop Environment 1.0"
204         echo "# "
205         echo "#   This file created by the \"dtprintegrate\" utility."
206         echo "# "
207         echo "#   Date of integration: `date`. "
208         echo "# "
209         echo "################################################################"
210         echo " "
211         ) > $databasefile_path
212
213         HASH
214         HASH Now, create the Print action for the printer
215         HASH
216
217         (
218         echo "ACTION ${printer_name}_Print"
219         echo "{"
220         echo "        ARG_TYPE                *"
221         echo "        LABEL                   $printer_name"
222         echo "        ICON                    ${base_icon_name}"
223         echo "        TYPE                    COMMAND"
224         echo "        WINDOW_TYPE             NO_STDIO"
225         if (( destination_flag ))
226         then
227             echo "        EXEC_STRING             env LPDEST=$destination \\"
228         else
229             echo "        EXEC_STRING             env LPDEST=$printer_name \\"
230         fi
231
232         echo "                          /usr/dt/bin/dtaction Print %(File)Arg_1%"
233         ) >> $databasefile_path
234
235         if (( $help_flag ))
236         then
237             echo "      DESCRIPTION             $help_text" >> $databasefile_path
238         elif (( $helpfile_flag ))
239         then
240             AddHelpFileContents $databasefile_path
241         fi
242
243         echo "}" >> $databasefile_path
244
245         HASH
246         HASH Next, create the print manager action for the printer
247         HASH
248         (
249         echo ""
250         echo "ACTION ${printer_name}_Print"
251         echo "{"
252         echo "  ARG_COUNT               0"
253         echo "  TYPE                    COMMAND"
254         echo "  WINDOW_TYPE             NO_STDIO"
255         if (( destination_flag ))
256         then
257             echo "      EXEC_STRING             env LPDEST=$destination \\"
258         else
259             echo "      EXEC_STRING             env LPDEST=$printer_name \\"
260         fi
261         echo "                          /usr/dt/bin/dtaction Dtqueueinfo"
262         echo "}"
263         ) >> $databasefile_path
264 }
265
266 XCOMM #################################################################
267 XCOMM ##
268 XCOMM ##  TraversePath()
269 XCOMM ##
270 XCOMM ##        Parse a given search path, using comma (,) and colon (:) as
271 XCOMM ##     delimiters.  Pass each path element to another
272 XCOMM ##     function.
273 XCOMM ##
274 XCOMM #################################################################
275 TraversePath ()
276 {
277   gotahost=0
278   typeset -i path=0 
279   IFSsave=$IFS
280   search_path=$1
281   dir_function=$2
282
283   if [[ -n "$search_path" && -n "$dir_function" ]]; then
284          HASH
285          HASH  look for colon and comma delimiters
286          HASH
287          IFS=':,'
288          set -A dir_array $search_path
289          while ((path<=${#dir_array[*]}-1)) ;do
290                  $dir_function ${dir_array[$path]}
291                  path=path+1
292          done
293   else return
294   fi
295   IFS=$IFSsave
296 }
297
298 XCOMM #################################################################
299 XCOMM ##
300 XCOMM ##  GetIconBaseName()
301 XCOMM ##
302 XCOMM ##        Given a file name of the form "base.l.bm" where size
303 XCOMM ##     can be ".l", ".m", ".s.", or ".t" and visual type can be 
304 XCOMM ##     ".bm" or ".pm", set $base_icon_name to just the base.
305 XCOMM ##
306 XCOMM #################################################################
307 GetIconBaseName()
308 {
309   base_icon_name=`basename $1 .bm`
310   base_icon_name=`basename $base_icon_name .pm`
311   base_icon_name=`basename $base_icon_name .l`
312   base_icon_name=`basename $base_icon_name .m`
313   base_icon_name=`basename $base_icon_name .s`
314   base_icon_name=`basename $base_icon_name .t`
315 }
316
317 XCOMM #################################################################
318 XCOMM ##
319 XCOMM ##  DoTheActualIconCopy()
320 XCOMM ##
321 XCOMM ##     Once we've determined the base name, and we've got the
322 XCOMM ##     source icon directory ($1), then copy all the icons
323 XCOMM ##     with the same base name into the destination $DTPRINTERICONFOLDER.
324 XCOMM ##
325 XCOMM #################################################################
326 DoTheActualIconCopy()
327 {
328   for i in $1/${base_icon_name}.*
329   do
330      if [ -f $i ]
331      then
332          simple_icon_name=${i##*/}
333
334          if [ -f $DTPRINTERICONFOLDER/$simple_icon_name ]
335          then
336              mv -f $DTPRINTERICONFOLDER/$simple_icon_name $DTPRINTERICONFOLDER/#$simple_icon_name
337          fi
338
339          cp $i $DTPRINTERICONFOLDER
340
341          if (( $verbose_flag ))
342             then
343                 print " Copied icon file $i "
344                 print " to $DTPRINTERICONFOLDER."
345             fi
346      fi
347   done
348 }
349
350 XCOMM #################################################################
351 XCOMM ##  CopyIconFiles()
352 XCOMM ##
353 XCOMM ##         Given $icon_name, get its base name, ensure that
354 XCOMM ##         the destination icon folder exists, and march down
355 XCOMM ##         the icon path, looking for matching icons, and copying
356 XCOMM ##         them to the destination folder.
357 XCOMM ##
358 XCOMM #################################################################
359 CopyIconFiles()
360 {
361
362 GetIconBaseName $icon_name
363
364 touch $DTPRINTERICONFOLDER/$icon_name.install > /dev/null 2>/dev/null
365
366 if [[ ! -f $DTPRINTERICONFOLDER/$icon_name.install ]]
367 then
368         failure_flag=1
369         PrintCreateError "$DTPRINTERICONFOLDER" "$DTPRINTERICONFOLDER/$icon_name.*"
370         if (( $verbose_flag ))
371         then
372             PrintEndLog
373         fi
374         Exit $CREATE_ERR
375 fi
376
377 rm -f $DTPRINTERICONFOLDER/$icon_name.install 
378
379 TraversePath "$DTPRINTICONPATH" DoTheActualIconCopy
380
381 XCOMM ##  install default icon if none found above. ###
382
383 }
384
385 XCOMM #################################################################
386 XCOMM ##  MakeActionFile()
387 XCOMM ##
388 XCOMM ##         Make the Action file for the printer in the
389 XCOMM ##         $DTPRINTERFOLDER
390 XCOMM ##
391 XCOMM #################################################################
392 MakeActionFile()
393 {
394
395         touch $action_path > /dev/null 2>/dev/null
396
397         chmod +x $action_path > /dev/null 2>/dev/null
398
399         if [[ ! -f $action_path  || ! -x $action_path ]]
400         then
401                 failure_flag=1
402                 PrintCreateError "$DTPRINTERFOLDER" "$action_file"
403                 if (( $verbose_flag ))
404                 then
405                     PrintEndLog
406                 fi
407                 Exit $CREATE_ERR
408         fi
409
410         if [ -x $ACTION_STUB_FILE ]
411         then
412
413                 cp $ACTION_STUB_FILE "$action_path"
414         else
415                 failure_flag=1
416                 PrintCreateError "$DTPRINTERFOLDER" "$action_file"
417                 if (( $verbose_flag ))
418                 then
419                     PrintEndLog
420                 fi
421                 Exit $CREATE_ERR
422         fi
423 }
424
425 XCOMM #################################################################
426 XCOMM ##  PrintStartLog()
427 XCOMM ##
428 XCOMM ##         Print the start of the log
429 XCOMM ##
430 XCOMM #################################################################
431 PrintStartLog() {
432     print "$COMMAND_NAME..."
433     print ""
434 }
435
436 XCOMM #################################################################
437 XCOMM ##  PrintEndLog()
438 XCOMM ##
439 XCOMM ##         Print the values of the variables.
440 XCOMM ##
441 XCOMM #################################################################
442 PrintEndLog() {
443     print "     DTPRINTACTIONFOLDER  ==  $DTPRINTACTIONFOLDER"
444     print ""
445     print "     DTPRINTERFOLDER ==       $DTPRINTERFOLDER"
446     print ""
447     if (( $failure_flag == 0 )) && (( $unintegrate_flag == 0))
448     then
449         print " Created one database file,"
450         print "     \"$databasefile_path\","
451         print " and one action file, "
452         print "     \"$action_path\"."
453         print ""
454     fi
455     if (( $failure_flag ==0 ))
456     then
457         print "...successfully completed."
458     else
459         print "...completed UNsuccesfully."
460     fi
461     print ""
462 }
463
464 XCOMM #################################################################
465 XCOMM ##  PrintCreateError()
466 XCOMM ##
467 XCOMM ##        Print an error message
468 XCOMM ##
469 XCOMM #################################################################
470 PrintCreateError() {
471     print ""
472     print "     Sorry--Unable to create the file \"$2\" under subdirectory"
473     print "     \"$1\"."
474     print ""
475     failure_flag=1
476 }
477
478 XCOMM #################################################################
479 XCOMM ##  PrintUsage()
480 XCOMM ##
481 XCOMM ##        Print a usage message.
482 XCOMM ##
483 XCOMM #################################################################
484 PrintUsage() {
485     print ""
486     print "Usage: $COMMAND_NAME [-d <destination>] [-i <icon_name>]"
487     print "                     [-h <help_text> | -f <help_file>]"
488     print "                     [-r] [-u] [-v] printer_name"
489     print "       where:"
490     print "       <destination>  specifies print destination known to the print"
491     print "       <icon_name>    specifies the icon to use to represent the printer"
492     print "       <help_text>    specifies the help text for the printer"
493     print "       <help_file>    specifies the help file for the printer"
494     print "       -r             forces reloading of the action database"
495     print "       -u             unintegrates the printer"
496     print "       -v             directs verbose messages to standard out"
497     print "       printer_name   specifies the printer name"
498     print ""
499     print "       All parameters except \"<printer_name>\" are optional."
500     print ""
501 }
502
503 XCOMM #################################################################
504 XCOMM ##  ReloadActions()
505 XCOMM ##
506 XCOMM ##
507 XCOMM #################################################################
508 ReloadActions() {
509         HASH
510         HASH Shorten forms like "host:0.0" to "host"
511         HASH
512         display_host=${DISPLAY%:*}
513         display_host=${display_host%%.*}
514
515         HASH
516         HASH Shorten forms like "host.dom.com" to "host"
517         HASH
518         session_host=`uname -n`
519         session_host=${session_host%%.*}
520
521         if (( reloadactions_flag ))
522         then
523         HASH
524         HASH  User wants to force reloading actions
525         HASH
526                 if [[ -x /usr/dt/bin/dtaction  &&  -n "$DISPLAY" ]]
527                 then
528                         /usr/dt/bin/dtaction ReloadActions
529                 fi
530         elif [[ "$display_host" = "$session_host" ]]
531         HASH
532         HASH  Our session server is running on the desktop
533         HASH
534         then
535                 if [ -x /usr/dt/bin/dtaction ]
536                 then
537                         /usr/dt/bin/dtaction ReloadActions
538                 fi
539         elif [[ -z "$display_host" && -n "$DISPLAY" ]]
540         then
541         HASH
542         HASH  Covers the case in which the simple value ":0.0" is our $DISPLAY
543         HASH
544                 if [ -x /usr/dt/bin/dtaction ]
545                 then
546                         /usr/dt/bin/dtaction ReloadActions
547                 fi
548         fi
549 }
550
551 XCOMM #################################################################
552 XCOMM ##  UnintegratePrinter()
553 XCOMM ##
554 XCOMM ##
555 XCOMM #################################################################
556 UnintegratePrinter() {
557
558     if [[ -f $action_path ]]
559     then
560         rm -fr $action_path
561         if (( verbose_flag ))
562         then
563             print "     Removed one action file: "
564             print "         \"$action_path\"."
565         fi
566     fi
567
568     if [[ -f $databasefile_path ]]
569     then
570         rm -fr $databasefile_path
571         if (( verbose_flag ))
572         then
573             print "     Removed one database file:"
574             print "         \"$databasefile_path\"."
575             print ""
576         fi
577     fi
578
579 }
580
581 XCOMM #################################################################
582 XCOMM ##  Exit()
583 XCOMM ##
584 XCOMM ##
585 XCOMM #################################################################
586 Exit() {
587         exit $1
588 }
589
590 XCOMM #################################################################
591 XCOMM ##  Main()
592 XCOMM ##
593 XCOMM ##         Set up and call the routines
594 XCOMM ##
595 XCOMM #################################################################
596
597 printer_name=""
598 printer_flag=0
599 help_flag=0
600 helpfile_flag=0
601 icon_flag=0
602 language_flag=0
603 verbose_flag=0
604 unintegrate_flag=0
605 failure_flag=0
606 destination_flag=0
607 destination=""
608 reloadactions_flag=0
609
610 while getopts d:f:h:i:ruv argument
611 do
612     case $argument in
613             d)  destination=$OPTARG
614                 destination_flag=1
615                 HASH print the destination name is $destination.
616                 ;;
617             f)  help_file=$OPTARG
618                 helpfile_flag=1
619                 ;;
620             h)  help_text=$OPTARG
621                 help_flag=1
622                 ;;
623             i)  icon_name=$OPTARG
624                 icon_flag=1
625                 ;;
626             r)  reloadactions_flag=1
627                 ;;
628             u)  unintegrate_flag=1
629                 ;;
630             v)  verbose_flag=1
631                 ;;
632             \?) 
633                 PrintUsage
634                 Exit 1
635                 ;;
636     esac
637 done
638
639 ((shift_positions = OPTIND - 1))
640
641 if (( $shift_positions < $# ))
642 then
643 XCOMM
644 XCOMM We have at least one remaining non-switch command line argument
645 XCOMM
646         shift $shift_positions
647         HASH
648         HASH We assume that any remaining arguments constitute the 
649         HASH filename.
650         HASH At some later point, may want to parse a list of filenames.
651         HASH
652         printer_name=$*
653         printer_flag=1
654 fi
655
656 XCOMM
657 XCOMM  Time to get to work.
658 XCOMM
659 Initialize
660
661 if (( $verbose_flag ))
662 then
663         PrintStartLog
664 fi
665
666 CheckOptions
667
668 if (( $unintegrate_flag ))
669 then
670
671     UnintegratePrinter
672
673 else
674
675     if (( $icon_flag ))
676     then
677         CopyIconFiles
678         HASH Doing so will set the $base_icon_name for inclusion in the
679         HASH filetypes action definition
680     fi
681
682     MakeDatabaseFile
683
684     MakeActionFile
685
686     ReloadActions
687
688 fi
689
690 if (( $verbose_flag ))
691 then
692         PrintEndLog
693 fi
694
695 Exit 0
696
697 XCOMM #########################         eof       ##############################