dtappintegrate.src and dtopen.src: fix shellcheck warnings
[oweals/cde.git] / cde / programs / dtappintegrate / dtappintegrate.src
1 XCOMM!KORNSHELL
2 XCOMM $XConsortium: dtappintegrate.src /main/4 1996/04/21 19:27:37 drk $
3 #define COMMENT_STAR *
4 XCOMM ###################################################################
5 XCOMM                                                                   #
6 XCOMM  dtappintegrate                                                   #
7 XCOMM                                                                   #
8 XCOMM (c) Copyright 1996 Digital Equipment Corporation.                 #
9 XCOMM (c) Copyright 1993,1994,1996 Hewlett-Packard Company.             #
10 XCOMM (c) Copyright 1993,1994,1996 International Business Machines Corp.#
11 XCOMM (c) Copyright 1993,1994,1996 Sun Microsystems, Inc.               #
12 XCOMM (c) Copyright 1996 Novell, Inc.                                   #
13 XCOMM (c) Copyright 1996 FUJITSU LIMITED.                               #
14 XCOMM (c) Copyright 1996 Hitachi.                                       #
15 XCOMM (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of  #
16 XCOMM     Novell, Inc.                                                  #
17 XCOMM                                                                   #
18 XCOMM  This is the dt  application integration script to assist         #
19 XCOMM  in integrating applications into COSE Desktop Environment.       #
20 XCOMM                                                                   #
21 XCOMM  syntax: dtappintegrate -s <source> [-t <target>] [-l <lang>] [-u]#
22 XCOMM  where                                                            #
23 XCOMM        -s           indicates application's root location.        #
24 XCOMM        source       the path name of the application's root.      #
25 XCOMM        -t           indicates a new location for the              #
26 XCOMM                       application's group files.                  #
27 XCOMM        target       the path name of the target location.         #
28 XCOMM        -l           indicates the language for this application.  #
29 XCOMM        language     the language name as used by $LANG            #
30 XCOMM                       environment variable.                       #
31 XCOMM        -u           indicates to unintegrate the application.     #
32 XCOMM                                                                   #
33 XCOMM                                                                   #
34 XCOMM ###################################################################
35
36 XCOMM ------------------------------------------------------------------#
37 XCOMM  ShowSyntax                                                       #
38 XCOMM                                                                   #
39 XCOMM       This routine is used to echo the command line syntax.       #
40 XCOMM                                                                   #
41 XCOMM       input:                                                      #
42 XCOMM         none                                                      #
43 XCOMM                                                                   #
44 XCOMM       return codes:                                               #
45 XCOMM         0   - success                                             #
46 XCOMM                                                                   #
47 XCOMM ------------------------------------------------------------------#
48 function ShowSyntax
49 {
50   echo "Usage: $SCRIPT_NAME -s <source> [-t <target>] [-l <language>] [-u]" | tee -a "$LOGFILE"
51   return 0
52 }
53 XCOMM ------------------------------------------------------------------#
54 XCOMM  GetAbsolutePath                                                  #
55 XCOMM                                                                   #
56 XCOMM       This routine is to resolve a path to its actual path,       #
57 XCOMM       following links, etc.                                       #
58 XCOMM                                                                   #
59 XCOMM       input:                                                      #
60 XCOMM         $1 = path                                                 #
61 XCOMM                                                                   #
62 XCOMM       output:                                                     #
63 XCOMM         absolute path                                             #
64 XCOMM                                                                   #
65 XCOMM ------------------------------------------------------------------#
66 function GetAbsolutePath
67 {
68   if [ "/" = "$1" ]; then
69      echo "$2"
70   elif [ -L "$1" ]; then
71      GetAbsolutePath "$(find . -name "$1" | awk '{print $NF}')" "$2"
72   else
73   {
74      if [ "." = "$1" ] || [ ".." = "$1" ]; then
75         GetAbsolutePath / /"$(basename "$1")""$2"
76      else
77         GetAbsolutePath "$(dirname "$1")" /"$(basename "$1")""$2"
78      fi
79   }
80   fi
81 }
82 XCOMM ------------------------------------------------------------------#
83 XCOMM  GetRelativePath                                                  #
84 XCOMM                                                                   #
85 XCOMM       This routine is used to determine the relative path of      #
86 XCOMM       of the source path from the target path.                    #
87 XCOMM                                                                   #
88 XCOMM       input:                                                      #
89 XCOMM         $1 = absolute source path                                 #
90 XCOMM         $2 = absolute target path                                 #
91 XCOMM                                                                   #
92 XCOMM       return codes:                                               #
93 XCOMM         0   - success                                             #
94 XCOMM         1   - error                                               #
95 XCOMM                                                                   #
96 XCOMM ------------------------------------------------------------------#
97 function GetRelativePath
98 {
99   $AWK 'BEGIN {
100           src = ARGV[1]
101           dest = ARGV[2]
102           a = split(src, A, "/");
103           b = split(dest, B, "/");
104
105           s = 0;
106            for (i = 2; i < a && i < b; i++) {
107              if (match(A[i],B[i])) {
108                 ++s;
109              } else {
110                 break;
111              }
112            }
113           for (i = 0 ; i <= (a - s - 2); i++) {
114              printf("../")
115           }
116           for (i = 2 + s; i <= b; i++) {
117              printf("%s%s", B[i], (i < b) ? "/":"\n");
118           }
119   }' "$2" "$1"
120 }
121 XCOMM ------------------------------------------------------------------#
122 XCOMM  LinkCfgs                                                         #
123 XCOMM                                                                   #
124 XCOMM       This routine creates the actual links from the application's#
125 XCOMM       root config files to the target location.                   #
126 XCOMM                                                                   #
127 XCOMM       input:                                                      #
128 XCOMM                                                                   #
129 XCOMM       return codes:                                               #
130 XCOMM         n   - number of files integrated or unintegrated          #
131 XCOMM                                                                   #
132 XCOMM ------------------------------------------------------------------#
133 function LinkCfgs
134 {
135   typeset source=$1 target=$2 torf=$3 spath="" tpath="" cfgfile="" rpath=""
136   typeset pattern="" files=""
137
138   shift;shift;shift
139   if [[ -L $source || -L $(dirname "$source") ]]
140     then
141     spath=$(GetAbsolutePath "$source")
142   else
143     spath=$source
144   fi
145   if [[ -L $target || -L $(dirname "$target") ]]
146     then
147     tpath=$(GetAbsolutePath "$target")
148   else
149     tpath=$target
150   fi
151   rpath=""
152   for pattern in "$@"
153   do
154     if [[ $pattern = "(*)" ]]
155       then
156       files=$(ls -d "$source"/COMMENT_STAR 2>/dev/null)
157     else
158       files=$(ls -d "$source"/"$pattern" 2>/dev/null)
159     fi
160     if $files
161       then
162       count=$(echo "$files" | wc -w)
163       for cfgfile in $files
164       do
165         basecfg=$(basename "$cfgfile")
166         if [[ $torf = TRUE ]]
167           then
168           if [[ $rpath = "" ]]
169             then
170             rpath=$(GetRelativePath "$spath" "$tpath")
171           fi
172           rm -f "$tpath"/"$basecfg"
173           echo "ln -sf $rpath/$basecfg $tpath/$basecfg" >> "$LOGFILE"
174           ln -sf "$rpath/$basecfg" "$tpath/$basecfg" >> "$LOGFILE" 2>&1
175         else
176           if rm "$tpath"/"$basecfg" >/dev/null 2>&1
177             then
178             echo "rm $tpath/$basecfg" >> "$LOGFILE"
179           fi
180         fi
181       done
182     fi
183   done
184   return "$count"
185 }
186
187 XCOMM ------------------------------------------------------------------#
188 XCOMM  IntegrateUnintegrate                                             #
189 XCOMM                                                                   #
190 XCOMM       This routine integrates the files into the cose desktop     #
191 XCOMM       environment or unintegrates them depending on the boolean   #
192 XCOMM       input parameter.                                            #
193 XCOMM                                                                   #
194 XCOMM       input:                                                      #
195 XCOMM         $1 = Integrate or not. TRUE=integrate. FALSE=unintegrate. #
196 XCOMM                                                                   #
197 XCOMM       return codes:                                               #
198 XCOMM         0   - work was done                                       #
199 XCOMM         1   - no work done                                        #
200 XCOMM                                                                   #
201 XCOMM ------------------------------------------------------------------#
202 function IntegrateUnintegrate
203 {
204   typeset torf=$1 srcs="" trgs="" fpats="" tpath="" spath="" rpath="" languages="" lang=""
205   typeset srcabs="" trgabs=""
206   integer i=0 icons=0 types=1 help=2 appmgr=3
207
208   srcabs=$(GetAbsolutePath "$APP_ROOT")
209   trgabs=$(GetAbsolutePath "$APP_TARGET")
210
211   srcs[0]=$srcabs$ICONS$APP_LANG
212   srcs[1]=$srcabs$TYPES$APP_LANG
213   srcs[2]=$srcabs$HELP$APP_LANG
214   srcs[3]=$srcabs$APPMANAGER$APP_LANG
215
216   trgs[0]=$trgabs$ICONS$APP_LANG
217   trgs[1]=$trgabs$TYPES$APP_LANG
218   trgs[2]=$trgabs$HELP$APP_LANG
219   trgs[3]=$trgabs$APPMANAGER$APP_LANG
220
221   fpats[0]="$PIXMAP_FILES $BITMAP_FILES"
222   fpats[1]="$ACTIONDB_FILES"
223   fpats[2]="$HELPVOLUME_FILES_OLD $HELPVOLUME_FILES_NEW $HELPFAMILY_FILES"
224   fpats[3]="$APPMAN_FILES"
225
226   rc=1
227   while (( i < 4 ))
228   do
229     if [[ $APP_LANG = "" ]]
230       then
231       if languages=$(ls -d "${srcs[i]}"/COMMENT_STAR 2>/dev/null)
232         then
233         for lang in $languages
234         do
235           baselang=$(basename "$lang")
236           if [[ -d $lang ]]
237             then
238             if [[ $torf = TRUE ]]
239               then
240               if [[ ! -d ${trgs[i]}/$baselang ]]
241                 then
242                 mkdir -p "${trgs[i]}"/"$baselang"
243               fi
244             fi
245             if ! LinkCfgs "${srcs[i]}/$baselang" "${trgs[i]}/$baselang" "$torf" "${fpats[i]}"
246               then
247               rc=0
248             fi
249           fi
250         done
251       fi
252     else
253       if ! LinkCfgs "${srcs[i]}" "${trgs[i]}" "$torf" "${fpats[i]}"
254         then
255         rc=0
256       fi
257     fi
258     i=$((i+1))
259   done
260   return $rc
261 }
262 XCOMM ------------------------------------------------------------------#
263 XCOMM  ExitOut                                                          #
264 XCOMM                                                                   #
265 XCOMM       Exit the program.                                           #
266 XCOMM                                                                   #
267 XCOMM       input:                                                      #
268 XCOMM         $1 = return code                                          #
269 XCOMM                                                                   #
270 XCOMM ------------------------------------------------------------------#
271 function ExitOut
272 {
273   typeset retcode=$1
274   echo "<<<<<<< END  OF  APPLICATION INTEGRATION >>>>>>>" >> "$LOGFILE"
275
276   echo "See $LOGFILE file for more information"
277   exit "$retcode"
278 }
279 XCOMM  ----<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>-----
280 XCOMM  ----<<<<<<<<<<<.-------------------------.>>>>>>>>>>>-----
281 XCOMM  ----<<<<<<<<<<<|                         |>>>>>>>>>>>-----
282 XCOMM  ----<<<<<<<<<<<|  START OF MAIN ROUTINE  |>>>>>>>>>>>>-----
283 XCOMM  ----<<<<<<<<<<<|_________________________|>>>>>>>>>>>-----
284 XCOMM  ----<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>-----
285
286 XCOMM -------------------------------------------------------------------
287 XCOMM  Initialize variables
288 XCOMM -------------------------------------------------------------------
289 sFLAG=0
290 tFLAG=0
291 uFLAG=0
292
293 TYPES=/types
294 APPMANAGER=/appmanager
295 ICONS=/icons
296 HELP=/help
297 APPCONFIG=/dt/appconfig
298 CONFIG_TOP=CDE_CONFIGURATION_TOP
299 DT=$(basename $CONFIG_TOP)
300 APP_TARGET=${CONFIG_TOP%/$DT}$APPCONFIG
301
302 PIXMAP_FILES="*.pm"
303 BITMAP_FILES="*.bm"
304 HELPVOLUME_FILES_OLD="*.hv"
305 HELPVOLUME_FILES_NEW="*.sdl"
306 HELPFAMILY_FILES="*.hf"
307 ACTIONDB_FILES="*.dt"
308 APPMAN_FILES="(*)"
309
310 ID=$(id)
311 LOGFILE=$(mktemp /tmp/dtappint.logXXXXXXXXXXXXXXXX)
312 PATH=CDE_INSTALLATION_TOP/bin:/usr/bin:/bin
313
314 XCOMM -------------------------------------------------------------------
315 XCOMM  Save application's name in variable.
316 XCOMM -------------------------------------------------------------------
317 SCRIPT_NAME=$0
318
319 XCOMM -------------------------------------------------------------------
320 XCOMM  Check if root user.  Exit if not.
321 XCOMM -------------------------------------------------------------------
322 ID=${ID##*uid=}
323 ID=${ID#*\(}
324 ID=${ID%%\)*}
325 if [[ $ID != root ]]
326    then
327    echo "Error: Must be root user to run $0!" >&2
328    exit 3
329 fi
330
331 XCOMM -------------------------------------------------------------------
332 XCOMM  Put prolog into log file.
333 XCOMM -------------------------------------------------------------------
334 echo "<<<<<<< START OF APPLICATION INTEGRATION >>>>>>>" > "$LOGFILE"
335
336 XCOMM -------------------------------------------------------------------
337 XCOMM  Put the date of application integration into the log file.
338 XCOMM -------------------------------------------------------------------
339 date >> "$LOGFILE"
340
341 XCOMM -------------------------------------------------------------------
342 XCOMM  Put the command line into the log file.
343 XCOMM -------------------------------------------------------------------
344 echo "$SCRIPT_NAME $*" >> "$LOGFILE"
345
346 XCOMM -------------------------------------------------------------------
347 XCOMM  Check if there are no command line arguments.
348 XCOMM  If none, then display the command syntax.
349 XCOMM -------------------------------------------------------------------
350 if [[ $# = 0 ]]
351   then
352   ShowSyntax
353   ExitOut 0
354 fi
355
356 XCOMM -------------------------------------------------------------------
357 XCOMM  Parse the command line into flags and variables.
358 XCOMM -------------------------------------------------------------------
359 while getopts s:t:l:u  flag
360 do
361   case $flag in
362      s)   sFLAG=1
363           APP_ROOT="$OPTARG";;
364      t)   tFLAG=1
365           APP_TARGET="$OPTARG";;
366      l)   APP_LANG="$OPTARG";;
367      u)   uFLAG=1;;
368      ?)   echo " "
369           ShowSyntax
370           ExitOut 2;;
371   esac
372 done
373
374 XCOMM -------------------------------------------------------------------
375 XCOMM  Check if application's root was specified and is valid.
376 XCOMM -------------------------------------------------------------------
377 if [[ $sFLAG = 0 ]]
378   then
379   echo "Error: Did not specify -s option!" >&2
380   ExitOut 4
381 else
382   if [[ ! -d $APP_ROOT ]]
383     then
384     APP_PATH=$APP_ROOT
385     echo "Error: $APP_PATH is not a directory!" >&2
386     ExitOut 4
387   fi
388   if [[ ! -r $APP_ROOT ]] || [[ ! -x $APP_ROOT ]]
389     then
390     APP_PATH=$APP_ROOT
391     echo "Error: Can not read $APP_PATH directory!" >&2
392     ExitOut 4
393   fi
394 fi
395
396 if [[ ${APP_ROOT%%/COMMENT_STAR} != "" ]]
397   then
398   APP_ROOT=$(pwd)/$APP_ROOT
399 fi
400
401 XCOMM -------------------------------------------------------------------
402 XCOMM  If target is specified, do some sanity checks on this path.
403 XCOMM -------------------------------------------------------------------
404 if [[ $tFLAG = 1 ]]
405   then
406   if [[ ! -d $APP_TARGET ]]
407     then
408     APP_PATH=$APP_TARGET
409     echo "Error: $APP_PATH is not a directory!" >&2
410     ExitOut 4
411   fi
412   if [[ ! -r $APP_TARGET ]] || [[ ! -x $APP_TARGET ]]
413     then
414     APP_PATH=$APP_TARGET
415     echo "Error: Can not read $APP_PATH directory!" >&2
416     ExitOut 4
417   fi
418
419   if [[ ${APP_TARGET%%/COMMENT_STAR} != "" ]]
420     then
421     APP_TARGET=$(pwd)/$APP_TARGET
422   fi
423 fi
424
425 XCOMM -------------------------------------------------------------------
426 XCOMM  Set up variables.
427 XCOMM -------------------------------------------------------------------
428 APP_ROOT=$APP_ROOT$APPCONFIG
429
430 if [[ $APP_LANG != "" ]]
431   then
432   APP_LANG=/$APP_LANG
433 fi
434
435 XCOMM -------------------------------------------------------------------
436 XCOMM  Unintegrate the application by un-doing the integration steps.
437 XCOMM -------------------------------------------------------------------
438 if [[ $uFLAG = 1 ]]
439   then
440   if IntegrateUnintegrate FALSE
441     then
442     echo "Unintegration Complete"
443   else
444     echo "No files to unintegrate"
445   fi
446   ExitOut 0
447 fi
448
449 XCOMM -------------------------------------------------------------------
450 XCOMM  See if these directories exist.  If they don't, then create them.
451 XCOMM -------------------------------------------------------------------
452 for i in $APP_TARGET$ICONS$APP_LANG $APP_TARGET$TYPES$APP_LANG $APP_TARGET$APPMANAGER$APP_LANG $APP_TARGET$HELP$APP_LANG
453  do
454   if [[ ! -d $i ]]
455     then
456     mkdir -p "$i"
457   fi
458  done
459
460 XCOMM -------------------------------------------------------------------
461 XCOMM  Determine which awk to use.
462 XCOMM -------------------------------------------------------------------
463 if ! type nawk > /dev/null 2>&1
464   then
465   AWK="awk"
466 else
467   AWK="nawk"
468 fi
469
470 XCOMM -------------------------------------------------------------------
471 XCOMM  Integrate the application.
472 XCOMM -------------------------------------------------------------------
473 if IntegrateUnintegrate TRUE
474   then
475   echo "Integration Complete"
476 else
477   echo "No files to integrate"
478 fi
479
480 XCOMM -------------------------------------------------------------------
481 XCOMM  Exit
482 XCOMM -------------------------------------------------------------------
483 ExitOut 0