SECURITY fix for dtappintegrate: Use mktemp(1) to generate a template.
[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 `ls -l $1 | awk '{print $NF}'` $2
72   else
73   {
74      if [ "." = "$1" -o ".." = "$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) ]] then
140     spath=$(GetAbsolutePath $source)
141   else
142     spath=$source
143   fi
144   if [[ -L $target || -L $(dirname $target) ]] then
145     tpath=$(GetAbsolutePath $target)
146   else
147     tpath=$target
148   fi
149   rpath=""
150   for pattern in "$@"
151   do
152     if [[ $pattern = "(*)" ]] then
153       files=$(ls -d $source/COMMENT_STAR 2>/dev/null)
154     else
155       files=$(ls -d $source/$pattern 2>/dev/null)
156     fi
157     if [[ $? = 0 ]] then
158       count=$(echo $files | wc -w)
159       for cfgfile in $files
160       do
161         basecfg=$(basename $cfgfile)
162         if [[ $torf = TRUE ]] then
163           if [[ $rpath = "" ]] then
164             rpath=$(GetRelativePath $spath $tpath)
165           fi
166           rm -f $tpath/$basecfg
167           echo "ln -sf $rpath/$basecfg $tpath/$basecfg" >> $LOGFILE
168           ln -sf $rpath/$basecfg $tpath/$basecfg >> $LOGFILE 2>&1
169         else
170           rm $tpath/$basecfg >/dev/null 2>&1
171           if [[ $? = 0 ]] then
172             echo "rm $tpath/$basecfg" >> $LOGFILE
173           fi
174         fi
175       done
176     fi
177   done
178   return $count
179 }
180
181 XCOMM ------------------------------------------------------------------#
182 XCOMM  IntegrateUnintegrate                                             #
183 XCOMM                                                                   #
184 XCOMM       This routine integrates the files into the cose desktop     #
185 XCOMM       environment or unintegrates them depending on the boolean   #
186 XCOMM       input parameter.                                            #
187 XCOMM                                                                   #
188 XCOMM       input:                                                      #
189 XCOMM         $1 = Integrate or not. TRUE=integrate. FALSE=unintegrate. #
190 XCOMM                                                                   #
191 XCOMM       return codes:                                               #
192 XCOMM         0   - work was done                                       #
193 XCOMM         1   - no work done                                        #
194 XCOMM                                                                   #
195 XCOMM ------------------------------------------------------------------#
196 function IntegrateUnintegrate
197 {
198   typeset torf=$1 srcs="" trgs="" fpats="" langs="" tpath="" spath="" rpath="" k="" languages="" lang=""
199   typeset cfgs="" srcabs="" trgabs=""
200   integer i=0 icons=0 types=1 help=2 appmgr=3
201
202   srcabs=$(GetAbsolutePath $APP_ROOT)
203   trgabs=$(GetAbsolutePath $APP_TARGET)
204
205   srcs[0]=$srcabs$ICONS$APP_LANG
206   srcs[1]=$srcabs$TYPES$APP_LANG
207   srcs[2]=$srcabs$HELP$APP_LANG
208   srcs[3]=$srcabs$APPMANAGER$APP_LANG
209
210   trgs[0]=$trgabs$ICONS$APP_LANG
211   trgs[1]=$trgabs$TYPES$APP_LANG
212   trgs[2]=$trgabs$HELP$APP_LANG
213   trgs[3]=$trgabs$APPMANAGER$APP_LANG
214
215   fpats[0]="$PIXMAP_FILES $BITMAP_FILES"
216   fpats[1]="$ACTIONDB_FILES"
217   fpats[2]="$HELPVOLUME_FILES_OLD $HELPVOLUME_FILES_NEW $HELPFAMILY_FILES"
218   fpats[3]="$APPMAN_FILES"
219
220   rc=1
221   while (( i < 4 ))
222   do
223     if [[ $APP_LANG = "" ]] then
224       languages=$(ls -d ${srcs[i]}/COMMENT_STAR 2>/dev/null)
225       if [[ $? = 0 ]] then
226         for lang in $languages
227         do
228           baselang=$(basename $lang)
229           if [[ -d $lang ]] then
230             if [[ $torf = TRUE ]] then
231               if [[ ! -d ${trgs[i]}/$baselang ]] then
232                 mkdir -p ${trgs[i]}/$baselang
233               fi
234             fi
235             LinkCfgs ${srcs[i]}/$baselang ${trgs[i]}/$baselang $torf ${fpats[i]}
236             if [[ $? != 0 ]] then
237               rc=0
238             fi
239           fi
240         done
241       fi
242     else
243       LinkCfgs ${srcs[i]} ${trgs[i]} $torf ${fpats[i]}
244       if [[ $? != 0 ]] then
245         rc=0
246       fi
247     fi
248     i=i+1
249   done
250   return $rc
251 }
252 XCOMM ------------------------------------------------------------------#
253 XCOMM  ExitOut                                                          #
254 XCOMM                                                                   #
255 XCOMM       Exit the program.                                           #
256 XCOMM                                                                   #
257 XCOMM       input:                                                      #
258 XCOMM         $1 = return code                                          #
259 XCOMM                                                                   #
260 XCOMM ------------------------------------------------------------------#
261 function ExitOut
262 {
263   typeset retcode=$1
264   echo "<<<<<<< END  OF  APPLICATION INTEGRATION >>>>>>>" >> $LOGFILE
265
266   echo "See $LOGFILE file for more information"
267   exit $retcode
268 }
269 XCOMM  ----<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>-----
270 XCOMM  ----<<<<<<<<<<<.-------------------------.>>>>>>>>>>>-----
271 XCOMM  ----<<<<<<<<<<<|                         |>>>>>>>>>>>-----
272 XCOMM  ----<<<<<<<<<<<|  START OF MAIN ROUTINE  |>>>>>>>>>>>>-----
273 XCOMM  ----<<<<<<<<<<<|_________________________|>>>>>>>>>>>-----
274 XCOMM  ----<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>-----
275
276 XCOMM -------------------------------------------------------------------
277 XCOMM  Initialize variables
278 XCOMM -------------------------------------------------------------------
279 sFLAG=0
280 tFLAG=0
281 lFLAG=0
282 uFLAG=0
283
284 TYPES=/types
285 APPMANAGER=/appmanager
286 ICONS=/icons
287 HELP=/help
288 APPCONFIG=/dt/appconfig
289 CONFIG_TOP=CDE_CONFIGURATION_TOP
290 DT=`basename $CONFIG_TOP`
291 APP_TARGET=${CONFIG_TOP%/$DT}$APPCONFIG
292
293 PIXMAP_FILES=*.pm
294 BITMAP_FILES=*.bm
295 HELPVOLUME_FILES_OLD=*.hv
296 HELPVOLUME_FILES_NEW=*.sdl
297 HELPFAMILY_FILES=*.hf
298 ACTIONDB_FILES=*.dt
299 FRONTPANEL_FILES=*.fp
300 APPMAN_FILES="(*)"
301
302 ID=$(id)
303 LOGFILE=$(mktemp /tmp/dtappint.logXXXXXXXXXXXXXXXX)
304 PATH=CDE_INSTALLATION_TOP/bin:/usr/bin
305
306 XCOMM -------------------------------------------------------------------
307 XCOMM  Save application's name in variable.
308 XCOMM -------------------------------------------------------------------
309 SCRIPT_NAME=$0
310
311 XCOMM -------------------------------------------------------------------
312 XCOMM  Check if root user.  Exit if not.
313 XCOMM -------------------------------------------------------------------
314 ID=${ID##*uid=}
315 ID=${ID#*\(}
316 ID=${ID%%\)*}
317 if [[ $ID != root ]] then
318    echo "Error: Must be root user to run $0!" >&2
319    exit 3
320 fi
321
322 XCOMM -------------------------------------------------------------------
323 XCOMM  Put prolog into log file.
324 XCOMM -------------------------------------------------------------------
325 echo "<<<<<<< START OF APPLICATION INTEGRATION >>>>>>>" > $LOGFILE
326
327 XCOMM -------------------------------------------------------------------
328 XCOMM  Put the date of application integration into the log file.
329 XCOMM -------------------------------------------------------------------
330 echo $(date) >> $LOGFILE
331
332 XCOMM -------------------------------------------------------------------
333 XCOMM  Put the command line into the log file.
334 XCOMM -------------------------------------------------------------------
335 echo "$SCRIPT_NAME $*" >> $LOGFILE
336
337 XCOMM -------------------------------------------------------------------
338 XCOMM  Check if there are no command line arguments.
339 XCOMM  If none, then display the command syntax.
340 XCOMM -------------------------------------------------------------------
341 if [[ $# = 0 ]] then
342   ShowSyntax
343   ExitOut 0
344 fi
345
346 XCOMM -------------------------------------------------------------------
347 XCOMM  Parse the command line into flags and variables.
348 XCOMM -------------------------------------------------------------------
349 while getopts s:t:l:u  flag
350 do
351   case $flag in
352      s)   sFLAG=1
353           APP_ROOT="$OPTARG";;
354      t)   tFLAG=1
355           APP_TARGET="$OPTARG";;
356      l)   lFLAG=1
357           APP_LANG="$OPTARG";;
358      u)   uFLAG=1;;
359      ?)   echo " "
360           ShowSyntax
361           ExitOut 2;;
362   esac
363 done
364
365 XCOMM -------------------------------------------------------------------
366 XCOMM  Check if application's root was specified and is valid.
367 XCOMM -------------------------------------------------------------------
368 if [[ $sFLAG = 0 ]] then
369   echo "Error: Did not specify -s option!" >&2
370   ExitOut 4
371 else
372   if [[ ! -d $APP_ROOT ]] then
373     APP_PATH=$APP_ROOT
374     echo "Error: $APP_PATH is not a directory!" >&2
375     ExitOut 4
376   fi
377   if [[ ! -r $APP_ROOT ]] || [[ ! -x $APP_ROOT ]] then
378     APP_PATH=$APP_ROOT
379     echo "Error: Can not read $APP_PATH directory!" >&2
380     ExitOut 4
381   fi
382 fi
383
384 if [[ ${APP_ROOT%%/COMMENT_STAR} != "" ]] then
385   APP_ROOT=$(pwd)/$APP_ROOT
386 fi
387
388 XCOMM -------------------------------------------------------------------
389 XCOMM  If target is specified, do some sanity checks on this path.
390 XCOMM -------------------------------------------------------------------
391 if [[ $tFLAG = 1 ]] then
392   if [[ ! -d $APP_TARGET ]] then
393     APP_PATH=$APP_TARGET
394     echo "Error: $APP_PATH is not a directory!" >&2
395     ExitOut 4
396   fi
397   if [[ ! -r $APP_TARGET ]] || [[ ! -x $APP_TARGET ]] then
398     APP_PATH=$APP_TARGET
399     echo "Error: Can not read $APP_PATH directory!" >&2
400     ExitOut 4
401   fi
402
403   if [[ ${APP_TARGET%%/COMMENT_STAR} != "" ]] then
404     APP_TARGET=$(pwd)/$APP_TARGET
405   fi
406 fi
407
408 XCOMM -------------------------------------------------------------------
409 XCOMM  Set up variables.
410 XCOMM -------------------------------------------------------------------
411 APP_ROOT=$APP_ROOT$APPCONFIG
412
413 if [[ $APP_LANG != "" ]] then
414   APP_LANG=/$APP_LANG
415 fi
416
417 XCOMM -------------------------------------------------------------------
418 XCOMM  Unintegrate the application by un-doing the integration steps.
419 XCOMM -------------------------------------------------------------------
420 if [[ $uFLAG = 1 ]] then
421   IntegrateUnintegrate FALSE
422   if [[ $? = 0 ]] then
423     echo "Unintegration Complete"
424   else
425     echo "No files to unintegrate"
426   fi
427   ExitOut 0
428 fi
429
430 XCOMM -------------------------------------------------------------------
431 XCOMM  See if these directories exist.  If they don't, then create them.
432 XCOMM -------------------------------------------------------------------
433 for i in $APP_TARGET$ICONS$APP_LANG $APP_TARGET$TYPES$APP_LANG $APP_TARGET$APPMANAGER$APP_LANG $APP_TARGET$HELP$APP_LANG
434  do
435   if [[ ! -d $i ]] then
436     mkdir -p $i
437   fi
438  done
439
440 XCOMM -------------------------------------------------------------------
441 XCOMM  Determine which awk to use.
442 XCOMM -------------------------------------------------------------------
443 $(type nawk > /dev/null 2>&1)
444 if [[ $? != 0 ]] then
445   AWK="awk"
446 else
447   AWK="nawk"
448 fi
449
450 XCOMM -------------------------------------------------------------------
451 XCOMM  Integrate the application.
452 XCOMM -------------------------------------------------------------------
453 IntegrateUnintegrate TRUE
454 if [[ $? = 0 ]] then
455   echo "Integration Complete"
456 else
457   echo "No files to integrate"
458 fi
459
460 XCOMM -------------------------------------------------------------------
461 XCOMM  Exit
462 XCOMM -------------------------------------------------------------------
463 ExitOut 0