Bump to docbook-xsl-stylesheets 1.79.2
[platform/upstream/docbook-xsl-stylesheets.git] / install.sh
1 #!/bin/bash
2
3 # install.sh - Set up user environment for a XML/XSLT distribution
4
5 # This is as an interactive installer for updating your
6 # environment to use an XML/XSLT distribution such as the DocBook
7 # XSL Stylesheets. Its main purpose is to configure your
8 # environment with XML catalog data and schema "locating rules"
9 # data provided in the XML/XSLT distribution.
10 #
11 # Although this installer was created for the DocBook project, it
12 # is a general-purpose tool that can be used with any XML/XSLT
13 # distribution that provides XML/SGML catalogs and locating rules.
14 #
15 # This script is mainly intended to make things easier for you if
16 # you want to install a particular XML/XSLT distribution that has
17 # not (yet) been packaged for your OS distro (Debian, Fedora,
18 # whatever), or to use "snapshot" or development releases 
19 #
20 # It works by updating your shell startup file (e.g., .bashrc and
21 # .cshrc) and .emacs file and by finding or creating a writable
22 # CatalogManager.properties file to update.
23 #
24 # It makes backup copies of any files it touches, and also
25 # generates a uninstall.sh script for reverting its changes.
26 #
27 # In the same directory where it is located, it expects to find
28 # the following four files:
29 #   - locatingrules.xml
30 #   - catalog.xml
31 #   - catalog
32 #   - .urilist
33 # And if it's unable to locate a CatalogManager.properties file in
34 # your environment, it expects to find an "example" one in the
35 # same directory as itself, which it copies over to your
36 # ~/.resolver directory.
37 #
38 # If the distribution contains any executables, change the value
39 # of the thisBinDir to a colon-separated list of the pathnames of
40 # the directories that contain those executables.
41
42 # mydir is the "canonical" absolute pathname for install.sh
43 mydir=$(cd -P $(dirname $0) && pwd -P) || exit 1
44
45 thisLocatingRules=$mydir/locatingrules.xml
46 thisXmlCatalog=$mydir/catalog.xml
47 thisSgmlCatalog=$mydir/catalog
48
49 # .urilist file contains a list of pairs of local pathnames and
50 # URIs to test for catalog resolution
51 thisUriList=$mydir/.urilist
52 exampleCatalogManager=$mydir/.CatalogManager.properties.example
53 thisCatalogManager=$HOME/.resolver/CatalogManager.properties
54
55 # thisBinDir directory is a colon-separated list of the pathnames
56 # to all directories that contain executables provided with the
57 # distribution (for example, the DocBook XSL Stylesheets
58 # distribution contains a "docbook-xsl-update" convenience script
59 # for rsync'ing up to the latest docbook-xsl snapshot). The
60 # install.sh script adds the value of thisBinDir to your PATH
61 # environment variable
62 thisBinDir=$mydir/tools/bin
63
64 emit_message() {
65   echo "$1" 1>&2
66 }
67
68 if [ ! "${*#--batch}" = "$*" ]; then
69   batchmode="Yes";
70 else
71   batchmode="No";
72   emit_message
73   if [ ! "$1" = "--test" ]; then 
74     emit_message "NOTE: For non-interactive installs/uninstalls, use --batch"
75     if [ ! "$1" = "--uninstall" ]; then
76       emit_message
77     fi
78   fi
79 fi
80
81 osName="Unidentified"
82 if uname -s | grep -qi "cygwin"; then
83   osName="Cygwin"
84 fi
85
86 classPathSeparator=":"
87 if [ "$osName" = "Cygwin" ]; then
88   thisJavaXmlCatalog=$(cygpath -m $thisXmlCatalog)
89   classPathSeparator=";"
90 else
91   thisJavaXmlCatalog=$thisXmlCatalog
92 fi
93
94 main() {
95   removeOldFiles
96   checkRoot
97   updateCatalogManager
98   checkForResolver
99   writeDotFiles
100   updateUserStartupFiles
101   updateUserDotEmacs
102   writeUninstallFile
103   writeTestFile
104   printExitMessage
105 }
106
107 removeOldFiles() {
108   rm -f $mydir/.profile.incl
109   rm -f $mydir/.cshrc.incl
110   rm -f $mydir/.emacs.el
111 }
112
113 checkRoot() {
114   if [ $(id -u)  == "0" ]; then
115     cat 1>&2 <<EOF
116
117 WARNING: This install script is meant to be run as a non-root
118          user, but you are running it as root.
119
120 EOF
121     read -s -n1 -p "Are you sure you want to continue? [No] "
122     emit_message "$REPLY"
123     case $REPLY in
124       [yY])
125       emit_message
126       ;;
127       *) emit_message "OK, exiting without making changes."
128       exit
129       ;;
130     esac
131   fi
132   return 0
133 }
134
135 updateCatalogManager() {
136
137   #  - finds or creates a writable CatalogManager.properties file
138   #
139   #  - adds the catalog.xml file for this distribution to the
140   #    CatalogManager.properties file found
141
142   if [ -z "$CLASSPATH" ]; then
143     cat 1>&2 <<EOF
144
145 NOTE: There is no CLASSPATH variable set in your environment.
146       No attempt was made to find a CatalogManager.properties
147       file.  Using $thisCatalogManager instead
148 EOF
149   else
150     # split CLASSPATH in a list of pathnames by replacing all separator
151     # characters with spaces
152     if [ "$osName" = "Cygwin" ]; then
153       pathnames=$(echo $CLASSPATH | tr ";" " ")
154     else
155       pathnames=$(echo $CLASSPATH | tr ":" " ")
156     fi
157     for path in $pathnames; do
158     if [ "$osName" = "Cygwin" ]; then
159       path=$(cygpath -u $path)
160     fi
161       # strip out trailing slash from pathname
162       path=$(echo $path | sed 's/\/$//')
163       # find CatalogManager.properties file
164       if [ -f $path/CatalogManager.properties ];
165       then
166         existingCatalogManager=$path/CatalogManager.properties
167         break
168       fi
169     done
170   fi
171   # end of CLASSPATH check
172
173   if [ -w "$existingCatalogManager" ]; then
174     # existing CatalogManager.properties was found and it is
175     # writable, so use it
176     myCatalogManager=$existingCatalogManager
177   else
178     if [ -f "$existingCatalogManager" ]; then
179       # a non-writable CatalogManager.properties exists, so emit a
180       # note saying that it won't be used
181       cat 1>&2 <<EOF
182 NOTE: $existingCatalogManager file found,
183       but you don't have permission to write to it.
184       Will instead use:
185       $thisCatalogManager
186 EOF
187     else
188       # CLASSPATH is set, but no CatalogManager.properties found
189       if [ -n "$CLASSPATH" ]; then
190         cat 1>&2 <<EOF
191 NOTE: No CatalogManager.properties found from CLASSPATH.
192       Will instead use:
193       $thisCatalogManager
194 EOF
195       fi
196     fi
197     if [ "$batchmode" = "Yes" ]; then
198       emit_message
199     fi
200     # end of check for existing writable CatalogManager.properties
201
202     if [ -f $thisCatalogManager ]; then
203       myCatalogManager=$thisCatalogManager
204     else
205       REPLY=""
206       if [ ! "$batchmode" = "Yes" ]; then
207         emit_message
208         read -s -n1 -p "Create $thisCatalogManager file? [Yes] "
209         emit_message "$REPLY"
210         emit_message
211       fi
212       case $REPLY in
213         [nNqQ])
214         emitNoChangeMsg
215         ;;
216         *)
217         if [ ! -d "${thisCatalogManager%/*}" ]; then
218           mkdir -p ${thisCatalogManager%/*}
219         fi
220         cp $mydir/.CatalogManager.properties.example $thisCatalogManager || exit 1
221         emit_message "NOTE: Created the following file:"
222         emit_message "      $thisCatalogManager"
223         myCatalogManager=$thisCatalogManager
224         ;;
225       esac
226       # end of creating "private" CatalogManager.properties
227     fi
228     # end of check for "private" CatalogManager.properties
229   fi
230   # end of check finding/creating writable CatalogManager.properties
231
232   if [ -n "$myCatalogManager" ]; then
233     etcXmlCatalog=
234     catalogsLine=$(grep "^catalogs=" $myCatalogManager)
235     if [ -f /etc/xml/catalog ] && [ "$osName" != "Cygwin" ] \
236       && [ "${catalogsLine#*/etc/xml/catalog*}" = "$catalogsLine" ]; then
237       cat 1>&2 <<EOF
238
239 WARNING: /etc/xml/catalog exists but was not found in:
240          $myCatalogManager
241          If /etc/xml/catalog file has content, you probably
242          should reference it in:
243          $myCatalogManager
244          This installer can automatically add it for you,
245          but BE WARNED that once it has been added, the
246          uninstaller for this distribution CANNOT REMOVE IT
247          automatically during uninstall. If you no longer want
248          it included, you will need to remove it manually.
249
250 EOF
251       REPLY=""
252       if [ ! "$batchmode" = "Yes" ]; then
253         read -s -n1 -p "Add /etc/xml/catalog to $myCatalogManager? [Yes] "
254         emit_message "$REPLY"
255       fi
256       case $REPLY in
257         [nNqQ])
258         emit_message
259         ;;
260         *)
261         etcXmlCatalog=/etc/xml/catalog
262         ;;
263       esac
264     fi
265
266     catalogBackup="$myCatalogManager.$$.bak"
267     if [ ! -w "${myCatalogManager%/*}" ]; then
268       emit_message
269       emit_message "WARNING: ${myCatalogManager%/*} directory is not writable."
270       emit_message
271       emitNoChangeMsg
272     else
273       REPLY=""
274       if [ ! "$batchmode" = "Yes" ]; then
275         emit_message
276         emit_message "Add $thisJavaXmlCatalog"
277         read -s -n1 -p "to $myCatalogManager file? [Yes] "
278         emit_message "$REPLY"
279         emit_message
280       fi
281       case $REPLY in
282         [nNqQ])
283         emitNoChangeMsg
284         ;;
285         *)
286         if [ "$catalogsLine" ] ; then
287           if [ "${catalogsLine#*$thisJavaXmlCatalog*}" != "$catalogsLine" ]; then
288             emit_message "NOTE: $thisJavaXmlCatalog"
289             emit_message "      already in:"
290             emit_message "      $myCatalogManager"
291           else
292             mv $myCatalogManager $catalogBackup || exit 1
293             sed "s#^catalogs=\(.*\)\$#catalogs=$thisJavaXmlCatalog;\1;$etcXmlCatalog#" $catalogBackup \
294             | sed 's/;\+/;/' | sed 's/;$//' > $myCatalogManager || exit 1
295             emit_message "NOTE: Successfully updated the following file:"
296             emit_message "      $myCatalogManager"
297             emit_message "      Backup written to:"
298             emit_message "      $catalogBackup"
299           fi
300         else
301           mv $myCatalogManager $catalogBackup || exit 1
302           cp $catalogBackup $myCatalogManager
303           echo "catalogs=$thisJavaXmlCatalog;$etcXmlCatalog" \
304           | sed 's/;\+/;/' | sed 's/;$//' >> $myCatalogManager || exit 1
305           emit_message "NOTE: \"catalogs=\" line added to $myCatalogManager."
306           emit_message "      Backup written to $catalogBackup"
307         fi
308         ;;
309       esac
310       # end of backing up and updating CatalogManager.properties
311     fi
312   fi
313   # end of CatalogManager.properties updates
314
315   if [ "$osName" = "Cygwin" ]; then
316     myCatalogManager=$(cygpath -m $myCatalogManager)
317   fi
318   return 0
319 }
320
321 writeDotFiles() {
322   while read; do
323     echo "$REPLY" >> $mydir/.profile.incl
324   done <<EOF
325 # $thisBinDir is not in PATH, so add it
326 if [ "\${PATH#*$thisBinDir*}" = "\$PATH" ]; then
327   PATH="$thisBinDir:\$PATH"
328   export PATH
329 fi
330 if [ -z "\$XML_CATALOG_FILES" ]; then
331   XML_CATALOG_FILES="$thisXmlCatalog"
332 else
333   # $thisXmlCatalog is not in XML_CATALOG_FILES, so add it
334   if [ "\${XML_CATALOG_FILES#*$thisXmlCatalog*}" = "\$XML_CATALOG_FILES" ]; then
335     XML_CATALOG_FILES="$thisXmlCatalog \$XML_CATALOG_FILES"
336   fi
337 fi
338 # /etc/xml/catalog exists but is not in XML_CATALOG_FILES, so add it
339 if [ -f /etc/xml/catalog ] && \
340   [ "\${XML_CATALOG_FILES#*/etc/xml/catalog*}" = "\$XML_CATALOG_FILES" ]; then
341   XML_CATALOG_FILES="\$XML_CATALOG_FILES /etc/xml/catalog"
342 fi
343 export XML_CATALOG_FILES
344
345 if [ -z "\$SGML_CATALOG_FILES" ]; then
346   SGML_CATALOG_FILES="$thisSgmlCatalog"
347 else
348   # $thisSgmlCatalog is not in SGML_CATALOG_FILES, so add it
349   if [ "\${SGML_CATALOG_FILES#*$thisSgmlCatalog}" = "\$SGML_CATALOG_FILES" ]; then
350     SGML_CATALOG_FILES="$thisSgmlCatalog:\$SGML_CATALOG_FILES"
351   fi
352 fi
353 # /etc/sgml/catalog exists but is not in SGML_CATALOG_FILES, so add it
354 if [ -f /etc/sgml/catalog ] && \
355   [ "\${SGML_CATALOG_FILES#*/etc/sgml/catalog*}" = "\$SGML_CATALOG_FILES" ]; then
356   SGML_CATALOG_FILES="\$SGML_CATALOG_FILES:/etc/sgml/catalog"
357 fi
358 export SGML_CATALOG_FILES
359 EOF
360
361 while read; do
362   echo "$REPLY" >> $mydir/.cshrc.incl
363 done <<EOF
364 # $thisBinDir is not in PATH, so add it
365 if ( "\\\`echo \$PATH | grep -v $thisBinDir\\\`" != "" ) then
366   setenv PATH "$thisBinDir:\$PATH"
367 endif
368 if ( ! $\?XML_CATALOG_FILES ) then
369   setenv XML_CATALOG_FILES "$thisXmlCatalog"
370 # $thisXmlCatalog is not in XML_CATALOG_FILES, so add it
371 else if ( "\\\`echo \$XML_CATALOG_FILES | grep -v $thisXmlCatalog\\\`" != "" ) then
372   setenv XML_CATALOG_FILES "$thisXmlCatalog \$XML_CATALOG_FILES"
373 endif
374 endif
375 # /etc/xml/catalog exists but is not in XML_CATALOG_FILES, so add it
376 if ( -f /etc/xml/catalog && "\\\`echo \$XML_CATALOG_FILES | grep -v /etc/xml/catalog\\\`" != "" ) then
377   setenv XML_CATALOG_FILES "\$XML_CATALOG_FILES /etc/xml/catalog"
378 endif
379
380 endif
381 if ( ! $\?SGML_CATALOG_FILES ) then
382   setenv SGML_CATALOG_FILES "$thisSgmlCatalog"
383 else if ( "\\\`echo \$SGML_CATALOG_FILES | grep -v $thisSgmlCatalog\\\`" != "" ) then
384   setenv SGML_CATALOG_FILES "$thisSgmlCatalog:\$SGML_CATALOG_FILES"
385 endif
386 endif
387 # /etc/SGML/catalog exists but is not in SGML_CATALOG_FILES, so add it
388 if ( -f /etc/sgml/catalog && "\\\`echo \$SGML_CATALOG_FILES | grep -v /etc/sgml/catalog\\\`" != "" ) then
389   setenv SGML_CATALOG_FILES {\$SGML_CATALOG_FILES}:/etc/sgml/catalog
390 endif
391 EOF
392
393 if [ -n "$myCatalogManager" ]; then
394   myCatalogManagerDir=${myCatalogManager%/*}
395   while read; do
396     echo "$REPLY" >> $mydir/.profile.incl
397   done <<EOF
398
399
400 if [ -z "\$CLASSPATH" ]; then
401   CLASSPATH="$myCatalogManagerDir"
402 else
403   # $myCatalogManagerDir is not in CLASSPATH, so add it
404   if [ "\${CLASSPATH#*$myCatalogManagerDir*}" = "\$CLASSPATH" ]; then
405     CLASSPATH="$myCatalogManagerDir$classPathSeparator\$CLASSPATH"
406   fi
407 fi
408 export CLASSPATH
409 EOF
410
411   while read; do
412     echo "$REPLY" >> $mydir/.cshrc.incl
413   done <<EOF
414
415
416 if ( ! $\?CLASSPATH ) then
417   setenv CLASSPATH "$myCatalogManagerDir"
418 # $myCatalogManagerDir is not in CLASSPATH, so add it
419 else if ( "\\\`echo \$CLASSPATH | grep -v $myCatalogManagerDir\\\`" != "" ) then
420   setenv CLASSPATH "$myCatalogManagerDir$classPathSeparator\$CLASSPATH"
421 endif
422 endif
423 EOF
424
425 fi
426
427 while read; do
428   echo "$REPLY" >> $mydir/.emacs.el
429 done <<EOF
430 (add-hook
431   'nxml-mode-hook
432   (lambda ()
433     (setq rng-schema-locating-files-default
434           (append '("$thisLocatingRules")
435                   rng-schema-locating-files-default ))))
436 EOF
437
438 return 0
439 }
440
441 updateUserStartupFiles() {
442   if [ ! "$batchmode" = "Yes" ]; then
443   cat 1>&2 <<EOF
444
445 NOTE: To source your environment correctly for using the catalog
446       files in this distribution, you need to update one or more
447       of your shell startup files. This installer can
448       automatically make the necessary changes. Or, if you prefer,
449       you can make the changes manually.
450
451 EOF
452   else
453     emit_message
454   fi
455
456   # if running csh or tcsh, target .cshrc and .tcshrc files for
457   # update; otherwise, target .bash_* and .profiles
458
459   parent=$(ps -p $PPID | grep "/")
460   if [ "${parent#*csh}" != "$parent" ] || [ "${parent#*tcsh}" != "$parent" ]; then
461     myStartupFiles=".cshrc .tcshrc"
462     appendLine="source $mydir/.cshrc.incl"
463   else
464     myStartupFiles=".bash_profile .bash_login .profile .bashrc"
465     appendLine=". $mydir/.profile.incl"
466   fi
467
468   for file in $myStartupFiles; do
469     if [ -f "$HOME/$file" ]; then
470       dotFileBackup=$HOME/$file.$$.bak
471       REPLY=""
472       if [ ! "$batchmode" = "Yes" ]; then
473         read -s -n1 -p "Update $HOME/$file? [Yes] "
474         emit_message "$REPLY"
475       fi
476       case $REPLY in
477         [nNqQ])
478         cat 1>&2 <<EOF
479
480 NOTE: No change made to $HOME/$file. You either need
481       to add the following line to it, or manually source
482       the shell environment for this distribution each
483       time you want use it.
484
485 $appendLine
486
487 EOF
488         ;;
489         *)
490         lineExists="$(grep "$appendLine" $HOME/$file )"
491         if [ ! "$lineExists" ]; then
492           mv $HOME/$file $dotFileBackup     || exit 1
493           cp $dotFileBackup $HOME/$file     || exit 1
494           echo "$appendLine" >> $HOME/$file || exit 1
495           cat 1>&2 <<EOF
496 NOTE: Successfully updated the following file:
497       $HOME/$file 
498       Backup written to:
499       $dotFileBackup
500
501 EOF
502         else
503           cat 1>&2 <<EOF
504 NOTE: The following file already contains information for this
505       distribution, so I did not update it.
506       $HOME/$file
507
508 EOF
509         fi
510         ;;
511       esac
512     fi
513   done
514   if [ -z "$dotFileBackup" ]; then
515     if [ ! "$batchmode" = "Yes" ]; then
516       emit_message
517     fi
518     cat 1>&2 <<EOF
519 NOTE: No shell startup files updated. You can source the
520       environment for this distribution manually, each time you
521       want to use it, by typing the following.
522
523 $appendLine
524
525 EOF
526   fi
527 }
528
529 updateUserDotEmacs() {
530   if [ -f $thisLocatingRules ]; then
531   cat 1>&2 <<EOF
532
533 NOTE: This distribution includes a "schema locating rules" file
534       for Emacs/nXML.  To use it, you should update either your
535       .emacs or .emacs.el file.  This installer can automatically
536       make the necessary changes. Or, if you prefer, you can make
537       the changes manually.
538
539 EOF
540
541   emacsAppendLine="(load-file \"$mydir/.emacs.el\")"
542   myEmacsFile=
543   for file in .emacs .emacs.el; do
544     if [ -f "$HOME/$file" ]; then
545       myEmacsFile=$HOME/$file
546       break
547     fi
548   done
549   if [ ! -f "$myEmacsFile" ]; then
550     REPLY=""
551     if [ ! "$batchmode" = "Yes" ]; then
552       read -s -n1 -p "No .emacs or .emacs.el file. Create one? [No] "
553       emit_message "$REPLY"
554       emit_message
555     fi
556     case $REPLY in
557       [yY])
558       myEmacsFile=$HOME/.emacs
559       touch $myEmacsFile
560       ;;
561       *)
562       cat 1>&2 <<EOF
563 NOTE: No Emacs changes made. To use this distribution with,
564       Emacs/nXML, you can create a .emacs file and manually add
565       the following line to it, or you can run it as a command
566       within Emacs.
567
568 $emacsAppendLine
569
570 EOF
571       ;;
572     esac
573   fi
574   if [ -n "$myEmacsFile" ]; then
575     REPLY=""
576     if [ ! "$batchmode" = "Yes" ]; then
577       read -s -n1 -p  "Update $myEmacsFile? [Yes] "
578       emit_message "$REPLY"
579       emit_message
580     fi
581     case $REPLY in
582       [nNqQ])
583       cat 1>&2 <<EOF
584
585 NOTE: No change made to $myEmacsFile. To use this distribution
586       with Emacs/nXML, you can manually add the following line
587       to your $myEmacsFile, or you can run it as a command
588       within Emacs.
589
590 $emacsAppendLine
591
592 EOF
593       ;;
594       *)
595       lineExists="$(grep "$emacsAppendLine" $myEmacsFile)"
596       if [ ! "$lineExists" ]; then
597         dotEmacsBackup=$myEmacsFile.$$.bak
598         mv $myEmacsFile $dotEmacsBackup    || exit 1
599         cp $dotEmacsBackup $myEmacsFile    || exit 1
600         echo "$emacsAppendLine" >> $myEmacsFile || exit 1
601         cat 1>&2 <<EOF
602 NOTE: Successfully updated the following file:
603       $myEmacsFile
604       Backup written to:
605       $dotEmacsBackup
606 EOF
607       else
608         cat 1>&2 <<EOF
609
610 NOTE: The following file already contains information for this
611       distribution, so I did not update it.
612       $myEmacsFile
613
614 EOF
615       fi
616       ;;
617     esac
618   fi
619 fi
620 }
621
622 uninstall() {
623   if [ ! "$batchmode" = "Yes" ]; then
624   cat 1>&2 <<EOF
625
626 NOTE: To "uninstall" this distribution, the changes made to your
627       CatalogManagers.properties, startup files, and/or .emacs
628       file need to be reverted. This uninstaller can automatically
629       revert them.  Or, if you prefer, you can revert them manually.
630
631 EOF
632   fi
633
634   if [ "$osName" = "Cygwin" ]; then
635     thisXmlCatalog=$thisJavaXmlCatalog
636   fi
637
638   # make "escaped" version of PWD to use with sed and grep
639   escapedPwd=$(echo $mydir | sed "s#/#\\\\\/#g")
640
641   # check to see if a non-empty value for catalogManager was fed
642   # to uninstaller.
643   if [ -n ${1#--catalogManager=} ]; then
644     myCatalogManager=${1#--catalogManager=}
645     catalogBackup="$myCatalogManager.$$.bak"
646     catalogsLine=$(grep "^catalogs=" $myCatalogManager)
647     if [ "$catalogsLine" ] ; then
648       if [ "${catalogsLine#*$thisXmlCatalog*}" != "$catalogsLine" ]; then
649         REPLY=""
650         if [ ! "$batchmode" = "Yes" ]; then
651           read -s -n1 -p "Revert $myCatalogManager? [Yes] "
652           emit_message "$REPLY"
653         fi
654         case $REPLY in
655           [nNqQ]*)
656           cat 1>&2 <<EOF
657
658 NOTE: No change made to $myCatalogManager. You need to manually
659       remove the following path from the "catalog=" line.
660
661           $thisXmlCatalog
662
663 EOF
664           ;;
665           *)
666           mv $myCatalogManager $catalogBackup || exit 1
667           sed "s#^catalogs=\(.*\)$thisXmlCatalog\(.*\)\$#catalogs=\1\2#" $catalogBackup \
668           | sed 's/;\+/;/' | sed 's/;$//' | sed 's/=;/=/' > $myCatalogManager || exit 1
669           cat 1>&2 <<EOF
670 NOTE: Successfully updated the following file:
671       $myCatalogManager
672       Backup written to:
673       $catalogBackup
674
675 EOF
676           ;;
677         esac
678       else
679         emit_message "NOTE: No data for this distribution found in:"
680         emit_message "       $myCatalogManager"
681         emit_message
682       fi
683     else
684       cat 1>&2 <<EOF
685 NOTE: No data for this distribution was found in the following
686       file, so I did not revert it.
687       $myCatalogManager
688 EOF
689     fi
690   fi
691
692   if [ -n "$myEmacsFile" ]; then 
693     # check to see if a non-empty value for --dotEmacs file was fed
694     # to uninstaller.
695     if [ -n ${2#--dotEmacs=} ]; then
696       myEmacsFile=${2#--dotEmacs=}
697       revertLine="(load-file \"$escapedPwd\/\.emacs\.el\")"
698       loadLine="$(grep "$revertLine" "$myEmacsFile")"
699       if [ -n "$loadLine" ]; then
700         emit_message
701         REPLY=""
702         if [ ! "$batchmode" = "Yes" ]; then
703           read -s -n1 -p "Revert $myEmacsFile? [Yes] "
704           emit_message "$REPLY"
705         fi
706         case $REPLY in
707           [nNqQ]*)
708           cat 1>&2 <<EOF
709
710 NOTE: No change made to $myEmacsFile. You need to manually
711 remove the following line.
712
713 (load-file \"$mydir/.emacs.el\")
714
715 EOF
716           ;;
717           *)
718           dotEmacsBackup=$myEmacsFile.$$.bak
719           sed -e "/$revertLine/d" -i".$$.bak" $myEmacsFile  || exit 1
720           cat 1>&2 <<EOF
721 NOTE: successfully reverted the following file:
722       $myEmacsFile
723       Backup written to:
724       $dotEmacsBackup
725
726 EOF
727           ;;
728         esac
729       else
730         emit_message "NOTE: No data for this distribution found in:"
731         emit_message "      $myEmacsFile"
732       fi
733     fi
734   fi
735
736   # check all startup files
737   myStartupFiles=".bash_profile .bash_login .profile .bashrc .cshrc .tcshrc"
738   for file in $myStartupFiles; do
739     if [ -e "$HOME/$file" ]; then
740       case $file in
741         .tcshrc|.cshrc)
742         revertLine="source $mydir/.cshrc.incl"
743         revertLineEsc="source $escapedPwd\/\.cshrc\.incl"
744         ;;
745         *)
746         revertLine=". $mydir/.profile.incl"
747         revertLineEsc="\. $escapedPwd\/\.profile\.incl"
748         ;;
749       esac
750       lineExists="$(grep "$revertLineEsc" $HOME/$file )"
751       if [ "$lineExists" ]; then
752         REPLY=""
753         if [ ! "$batchmode" = "Yes" ]; then
754           read -s -n1 -p "Update $HOME/$file? [Yes] "
755           emit_message "$REPLY"
756         fi
757         case $REPLY in
758           [nNqQ]*)
759           cat 1>&2 <<EOF
760
761 NOTE: No change made to $HOME/$file. You need to manually remove
762       the following line from it.
763
764  $revertLine
765
766 EOF
767           ;;
768           *)
769           dotFileBackup=$HOME/$file.$$.bak
770           sed -e "/$revertLineEsc/d" -i".$$.bak" $HOME/$file  || exit 1
771           cat 1>&2 <<EOF
772 NOTE: Successfully updated the following file:
773       $HOME/$file
774       Backup written to:
775       $dotFileBackup
776
777 EOF
778           ;;
779         esac
780       else
781         emit_message "NOTE: No data for this distribution found in:"
782         emit_message "      $HOME/$file"
783         emit_message
784       fi
785     fi
786   done
787   removeOldFiles
788   emit_message "Done. Deleted uninstall.sh file."
789   rm -f $mydir/test.sh      || exit 1
790   rm -f $mydir/uninstall.sh || exit 1
791 }
792
793 writeUninstallFile() {
794   uninstallFile=$mydir/uninstall.sh
795   echo '#!/bin/bash'                               > $uninstallFile || exit 1
796   echo 'mydir=$(cd -P $(dirname $0) && pwd -P)'   >> $uninstallFile || exit 1
797   echo "\$mydir/install.sh \\"                    >> $uninstallFile || exit 1
798   echo "  --uninstall \\"                         >> $uninstallFile || exit 1
799   echo "  --catalogManager=$myCatalogManager \\"  >> $uninstallFile || exit 1
800   echo "  --dotEmacs='$myEmacsFile' \\"           >> $uninstallFile || exit 1
801   echo '  $@'                                     >> $uninstallFile || exit 1
802   chmod 755 $uninstallFile || exit 1
803 }
804
805 writeTestFile() {
806   testFile=$mydir/test.sh
807   echo "#!/bin/bash"                                > $testFile || exit 1
808   echo 'mydir=$(cd -P $(dirname $0) && pwd -P)'    >> $testFile || exit 1
809   echo '$mydir/install.sh --test'                  >> $testFile || exit 1
810   chmod 755 $testFile || exit 1
811 }
812
813 printExitMessage() {
814   cat 1>&2 <<EOF
815 To source your shell environment for this distribution, type the
816 following:
817
818 $appendLine
819
820 EOF
821 }
822
823 checkForResolver() {
824   resolverResponse="$(java org.apache.xml.resolver.apps.resolver uri -u foo 2>/dev/null)"
825   if [ -z "$resolverResponse" ]; then
826     cat 1>&2 <<EOF
827
828 NOTE: Your environment does not seem to contain the Apache XML
829       Commons Resolver; without that, you can't use XML catalogs
830       with Java applications. For more information, see the "How
831       to use a catalog file" section in Bob Stayton's "DocBook
832       XSL: The Complete Guide"
833
834       http://sagehill.net/docbookxsl/UseCatalog.html
835
836 EOF
837   fi
838 }
839
840 emitNoChangeMsg() {
841   cat 1>&2 <<EOF
842
843 NOTE: No changes were made to CatalogManagers.properties. To
844       provide your Java tools with XML catalog information for
845       this distribution, you will need to make the appropriate
846       changes manually.
847
848 EOF
849 }
850
851 testCatalogs() {
852   if [ ! -f "$thisXmlCatalog" ]; then
853     cat 1>&2 <<EOF
854
855 FATAL: $thisXmlCatalog file needed but not found. Stopping.
856 EOF
857   exit
858   fi
859
860   if [ -z "$XML_CATALOG_FILES" ]; then
861     emit_message
862     emit_message "WARNING: XML_CATALOG_FILES not set. Not testing with xmlcatalog."
863   else
864     xmlCatalogResponse="$(xmlcatalog 2>/dev/null)"
865     if [ -z "$xmlCatalogResponse" ]; then
866     cat 1>&2 <<EOF
867
868 WARNING: Cannot locate the "xmlcatalog" command. Make sure that
869          you have libxml2 and its associated utilities installed.
870
871          http://xmlsoft.org/
872
873 EOF
874     else
875       emit_message "Testing with xmlcatalog..."
876       # read in pathname-uri pairs from .urilist file
877       while read pair; do
878         if [ ! "${pair%* *}" = "." ]; then
879           path=$mydir/${pair%* *}
880         else
881           path=$mydir/
882         fi
883         uri=${pair#* *}
884         emit_message
885         emit_message "  Tested: $uri"
886         for catalog in $XML_CATALOG_FILES; do
887           response="$(xmlcatalog $catalog $uri| grep -v "No entry")"
888           if [ -n "$response" ]; then
889             if [ "$response" = "$path" ]; then
890               emit_message "  Result: $path"
891               break
892             else
893               emit_message "  Result: FAILED"
894             fi
895           fi
896         done
897       done < $mydir/.urilist
898     fi
899   fi
900
901   if [ -z "$CLASSPATH" ]; then
902     emit_message
903     emit_message "NOTE: CLASSPATH not set. Not testing with Apache XML Commons Resolver."
904   else
905     if [ "$(checkForResolver)" ]; then
906       checkForResolver
907     else
908       emit_message
909       emit_message "Testing with Apache XML Commons Resolver..."
910       # read in pathname-uri pairs from .urilist file
911       while read pair; do
912         if [ ! "${pair%* *}" = "." ]; then
913           path=$mydir/${pair%* *}
914         else
915           path=$mydir/
916         fi
917         uri=${pair#* *}
918         emit_message
919         emit_message "  Tested: $uri"
920         if [ ${uri%.dtd} != $uri ]; then
921           response="$(java org.apache.xml.resolver.apps.resolver system -s $uri | grep "Result")"
922         else
923           response="$(java org.apache.xml.resolver.apps.resolver uri -u $uri | grep "Result")"
924         fi
925         if [ "$response" ]; then
926           if [ "${response#*$path}" != "$response" ]; then
927             emit_message "  Result: $path"
928           else
929             emit_message "  Result: FAILED"
930           fi
931           echo
932         fi
933       done < $mydir/.urilist
934     fi
935   fi
936 }
937
938 # get opts and execute appropriate function
939 case $1 in
940   *-uninstall)
941   uninstall $2 $3 $4
942   ;;
943   *-test)
944   testCatalogs
945   ;;
946   *)
947   main
948   ;;
949 esac
950
951 # Copyright
952 # ---------
953 # Copyright 2005-2007 Michael(tm) Smith <smith@sideshowbarker.net>
954
955 # Permission is hereby granted, free of charge, to any person
956 # obtaining a copy of this software and associated documentation
957 # files (the "Software"), to deal in the Software without
958 # restriction, including without limitation the rights to use, copy,
959 # modify, merge, publish, distribute, sublicense, and/or sell copies
960 # of the Software, and to permit persons to whom the Software is
961 # furnished to do so, subject to the following conditions:
962
963 # The above copyright notice and this permission notice shall be
964 # included in all copies or substantial portions of the Software.
965
966 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
967 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
968 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
969 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
970 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
971 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
972 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
973 # DEALINGS IN THE SOFTWARE.
974
975 # vim: number