summaryrefslogtreecommitdiff
path: root/src/engine/SCons/Tool/docbook/docbook-xsl-1.76.1/install.sh
blob: 40716191a68ab10b4529700280808a3d48bc95a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
#!/bin/bash
# $Id: install.sh 7942 2008-03-26 06:08:08Z xmldoc $
# $Source$ #

# install.sh - Set up user environment for a XML/XSLT distribution

# This is as an interactive installer for updating your
# environment to use an XML/XSLT distribution such as the DocBook
# XSL Stylesheets. Its main purpose is to configure your
# environment with XML catalog data and schema "locating rules"
# data provided in the XML/XSLT distribution.
#
# Although this installer was created for the DocBook project, it
# is a general-purpose tool that can be used with any XML/XSLT
# distribution that provides XML/SGML catalogs and locating rules.
#
# This script is mainly intended to make things easier for you if
# you want to install a particular XML/XSLT distribution that has
# not (yet) been packaged for your OS distro (Debian, Fedora,
# whatever), or to use "snapshot" or development releases 
#
# It works by updating your shell startup file (e.g., .bashrc and
# .cshrc) and .emacs file and by finding or creating a writable
# CatalogManager.properties file to update.
#
# It makes backup copies of any files it touches, and also
# generates a uninstall.sh script for reverting its changes.
#
# In the same directory where it is located, it expects to find
# the following four files:
#   - locatingrules.xml
#   - catalog.xml
#   - catalog
#   - .urilist
# And if it's unable to locate a CatalogManager.properties file in
# your environment, it expects to find an "example" one in the
# same directory as itself, which it copies over to your
# ~/.resolver directory.
#
# If the distribution contains any executables, change the value
# of the thisBinDir to a colon-separated list of the pathnames of
# the directories that contain those executables.

# mydir is the "canonical" absolute pathname for install.sh
mydir=$(cd -P $(dirname $0) && pwd -P) || exit 1

thisLocatingRules=$mydir/locatingrules.xml
thisXmlCatalog=$mydir/catalog.xml
thisSgmlCatalog=$mydir/catalog

# .urilist file contains a list of pairs of local pathnames and
# URIs to test for catalog resolution
thisUriList=$mydir/.urilist
exampleCatalogManager=$mydir/.CatalogManager.properties.example
thisCatalogManager=$HOME/.resolver/CatalogManager.properties

# thisBinDir directory is a colon-separated list of the pathnames
# to all directories that contain executables provided with the
# distribution (for example, the DocBook XSL Stylesheets
# distribution contains a "docbook-xsl-update" convenience script
# for rsync'ing up to the latest docbook-xsl snapshot). The
# install.sh script adds the value of thisBinDir to your PATH
# environment variable
thisBinDir=$mydir/tools/bin

emit_message() {
  echo "$1" 1>&2
}

if [ ! "${*#--batch}" = "$*" ]; then
  batchmode="Yes";
else
  batchmode="No";
  emit_message
  if [ ! "$1" = "--test" ]; then 
    emit_message "NOTE: For non-interactive installs/uninstalls, use --batch"
    if [ ! "$1" = "--uninstall" ]; then
      emit_message
    fi
  fi
fi

osName="Unidentified"
if uname -s | grep -qi "cygwin"; then
  osName="Cygwin"
fi

classPathSeparator=":"
if [ "$osName" = "Cygwin" ]; then
  thisJavaXmlCatalog=$(cygpath -m $thisXmlCatalog)
  classPathSeparator=";"
else
  thisJavaXmlCatalog=$thisXmlCatalog
fi

main() {
  removeOldFiles
  checkRoot
  updateCatalogManager
  checkForResolver
  writeDotFiles
  updateUserStartupFiles
  updateUserDotEmacs
  writeUninstallFile
  writeTestFile
  printExitMessage
}

removeOldFiles() {
  rm -f $mydir/.profile.incl
  rm -f $mydir/.cshrc.incl
  rm -f $mydir/.emacs.el
}

checkRoot() {
  if [ $(id -u)  == "0" ]; then
    cat 1>&2 <<EOF

WARNING: This install script is meant to be run as a non-root
         user, but you are running it as root.

EOF
    read -s -n1 -p "Are you sure you want to continue? [No] "
    emit_message "$REPLY"
    case $REPLY in
      [yY])
      emit_message
      ;;
      *) emit_message "OK, exiting without making changes."
      exit
      ;;
    esac
  fi
  return 0
}

updateCatalogManager() {

  #  - finds or creates a writable CatalogManager.properties file
  #
  #  - adds the catalog.xml file for this distribution to the
  #    CatalogManager.properties file found

  if [ -z "$CLASSPATH" ]; then
    cat 1>&2 <<EOF

NOTE: There is no CLASSPATH variable set in your environment.
      No attempt was made to find a CatalogManager.properties
      file.  Using $thisCatalogManager instead
EOF
  else
    # split CLASSPATH in a list of pathnames by replacing all separator
    # characters with spaces
    if [ "$osName" = "Cygwin" ]; then
      pathnames=$(echo $CLASSPATH | tr ";" " ")
    else
      pathnames=$(echo $CLASSPATH | tr ":" " ")
    fi
    for path in $pathnames; do
    if [ "$osName" = "Cygwin" ]; then
      path=$(cygpath -u $path)
    fi
      # strip out trailing slash from pathname
      path=$(echo $path | sed 's/\/$//')
      # find CatalogManager.properties file
      if [ -f $path/CatalogManager.properties ];
      then
        existingCatalogManager=$path/CatalogManager.properties
        break
      fi
    done
  fi
  # end of CLASSPATH check

  if [ -w "$existingCatalogManager" ]; then
    # existing CatalogManager.properties was found and it is
    # writable, so use it
    myCatalogManager=$existingCatalogManager
  else
    if [ -f "$existingCatalogManager" ]; then
      # a non-writable CatalogManager.properties exists, so emit a
      # note saying that it won't be used
      cat 1>&2 <<EOF
NOTE: $existingCatalogManager file found,
      but you don't have permission to write to it.
      Will instead use:
      $thisCatalogManager
EOF
    else
      # CLASSPATH is set, but no CatalogManager.properties found
      if [ -n "$CLASSPATH" ]; then
        cat 1>&2 <<EOF
NOTE: No CatalogManager.properties found from CLASSPATH.
      Will instead use:
      $thisCatalogManager
EOF
      fi
    fi
    if [ "$batchmode" = "Yes" ]; then
      emit_message
    fi
    # end of check for existing writable CatalogManager.properties

    if [ -f $thisCatalogManager ]; then
      myCatalogManager=$thisCatalogManager
    else
      REPLY=""
      if [ ! "$batchmode" = "Yes" ]; then
        emit_message
        read -s -n1 -p "Create $thisCatalogManager file? [Yes] "
        emit_message "$REPLY"
        emit_message
      fi
      case $REPLY in
        [nNqQ])
        emitNoChangeMsg
        ;;
        *)
        if [ ! -d "${thisCatalogManager%/*}" ]; then
          mkdir -p ${thisCatalogManager%/*}
        fi
        cp $mydir/.CatalogManager.properties.example $thisCatalogManager || exit 1
        emit_message "NOTE: Created the following file:"
        emit_message "      $thisCatalogManager"
        myCatalogManager=$thisCatalogManager
        ;;
      esac
      # end of creating "private" CatalogManager.properties
    fi
    # end of check for "private" CatalogManager.properties
  fi
  # end of check finding/creating writable CatalogManager.properties

  if [ -n "$myCatalogManager" ]; then
    etcXmlCatalog=
    catalogsLine=$(grep "^catalogs=" $myCatalogManager)
    if [ -f /etc/xml/catalog ] && [ "$osName" != "Cygwin" ] \
      && [ "${catalogsLine#*/etc/xml/catalog*}" = "$catalogsLine" ]; then
      cat 1>&2 <<EOF

WARNING: /etc/xml/catalog exists but was not found in:
         $myCatalogManager
         If /etc/xml/catalog file has content, you probably
         should reference it in:
         $myCatalogManager
         This installer can automatically add it for you,
         but BE WARNED that once it has been added, the
         uninstaller for this distribution CANNOT REMOVE IT
         automatically during uninstall. If you no longer want
         it included, you will need to remove it manually.

EOF
      REPLY=""
      if [ ! "$batchmode" = "Yes" ]; then
        read -s -n1 -p "Add /etc/xml/catalog to $myCatalogManager? [Yes] "
        emit_message "$REPLY"
      fi
      case $REPLY in
        [nNqQ])
        emit_message
        ;;
        *)
        etcXmlCatalog=/etc/xml/catalog
        ;;
      esac
    fi

    catalogBackup="$myCatalogManager.$$.bak"
    if [ ! -w "${myCatalogManager%/*}" ]; then
      emit_message
      emit_message "WARNING: ${myCatalogManager%/*} directory is not writable."
      emit_message
      emitNoChangeMsg
    else
      REPLY=""
      if [ ! "$batchmode" = "Yes" ]; then
        emit_message
        emit_message "Add $thisJavaXmlCatalog"
        read -s -n1 -p "to $myCatalogManager file? [Yes] "
        emit_message "$REPLY"
        emit_message
      fi
      case $REPLY in
        [nNqQ])
        emitNoChangeMsg
        ;;
        *)
        if [ "$catalogsLine" ] ; then
          if [ "${catalogsLine#*$thisJavaXmlCatalog*}" != "$catalogsLine" ]; then
            emit_message "NOTE: $thisJavaXmlCatalog"
            emit_message "      already in:"
            emit_message "      $myCatalogManager"
          else
            mv $myCatalogManager $catalogBackup || exit 1
            sed "s#^catalogs=\(.*\)\$#catalogs=$thisJavaXmlCatalog;\1;$etcXmlCatalog#" $catalogBackup \
            | sed 's/;\+/;/' | sed 's/;$//' > $myCatalogManager || exit 1
            emit_message "NOTE: Successfully updated the following file:"
            emit_message "      $myCatalogManager"
            emit_message "      Backup written to:"
            emit_message "      $catalogBackup"
          fi
        else
          mv $myCatalogManager $catalogBackup || exit 1
          cp $catalogBackup $myCatalogManager
          echo "catalogs=$thisJavaXmlCatalog;$etcXmlCatalog" \
          | sed 's/;\+/;/' | sed 's/;$//' >> $myCatalogManager || exit 1
          emit_message "NOTE: \"catalogs=\" line added to $myCatalogManager."
          emit_message "      Backup written to $catalogBackup"
        fi
        ;;
      esac
      # end of backing up and updating CatalogManager.properties
    fi
  fi
  # end of CatalogManager.properties updates

  if [ "$osName" = "Cygwin" ]; then
    myCatalogManager=$(cygpath -m $myCatalogManager)
  fi
  return 0
}

writeDotFiles() {
  while read; do
    echo "$REPLY" >> $mydir/.profile.incl
  done <<EOF
# $thisBinDir is not in PATH, so add it
if [ "\${PATH#*$thisBinDir*}" = "\$PATH" ]; then
  PATH="$thisBinDir:\$PATH"
  export PATH
fi
if [ -z "\$XML_CATALOG_FILES" ]; then
  XML_CATALOG_FILES="$thisXmlCatalog"
else
  # $thisXmlCatalog is not in XML_CATALOG_FILES, so add it
  if [ "\${XML_CATALOG_FILES#*$thisXmlCatalog*}" = "\$XML_CATALOG_FILES" ]; then
    XML_CATALOG_FILES="$thisXmlCatalog \$XML_CATALOG_FILES"
  fi
fi
# /etc/xml/catalog exists but is not in XML_CATALOG_FILES, so add it
if [ -f /etc/xml/catalog ] && \
  [ "\${XML_CATALOG_FILES#*/etc/xml/catalog*}" = "\$XML_CATALOG_FILES" ]; then
  XML_CATALOG_FILES="\$XML_CATALOG_FILES /etc/xml/catalog"
fi
export XML_CATALOG_FILES

if [ -z "\$SGML_CATALOG_FILES" ]; then
  SGML_CATALOG_FILES="$thisSgmlCatalog"
else
  # $thisSgmlCatalog is not in SGML_CATALOG_FILES, so add it
  if [ "\${SGML_CATALOG_FILES#*$thisSgmlCatalog}" = "\$SGML_CATALOG_FILES" ]; then
    SGML_CATALOG_FILES="$thisSgmlCatalog:\$SGML_CATALOG_FILES"
  fi
fi
# /etc/sgml/catalog exists but is not in SGML_CATALOG_FILES, so add it
if [ -f /etc/sgml/catalog ] && \
  [ "\${SGML_CATALOG_FILES#*/etc/sgml/catalog*}" = "\$SGML_CATALOG_FILES" ]; then
  SGML_CATALOG_FILES="\$SGML_CATALOG_FILES:/etc/sgml/catalog"
fi
export SGML_CATALOG_FILES
EOF

while read; do
  echo "$REPLY" >> $mydir/.cshrc.incl
done <<EOF
# $thisBinDir is not in PATH, so add it
if ( "\\\`echo \$PATH | grep -v $thisBinDir\\\`" != "" ) then
  setenv PATH "$thisBinDir:\$PATH"
endif
if ( ! $\?XML_CATALOG_FILES ) then
  setenv XML_CATALOG_FILES "$thisXmlCatalog"
# $thisXmlCatalog is not in XML_CATALOG_FILES, so add it
else if ( "\\\`echo \$XML_CATALOG_FILES | grep -v $thisXmlCatalog\\\`" != "" ) then
  setenv XML_CATALOG_FILES "$thisXmlCatalog \$XML_CATALOG_FILES"
endif
endif
# /etc/xml/catalog exists but is not in XML_CATALOG_FILES, so add it
if ( -f /etc/xml/catalog && "\\\`echo \$XML_CATALOG_FILES | grep -v /etc/xml/catalog\\\`" != "" ) then
  setenv XML_CATALOG_FILES "\$XML_CATALOG_FILES /etc/xml/catalog"
endif

endif
if ( ! $\?SGML_CATALOG_FILES ) then
  setenv SGML_CATALOG_FILES "$thisSgmlCatalog"
else if ( "\\\`echo \$SGML_CATALOG_FILES | grep -v $thisSgmlCatalog\\\`" != "" ) then
  setenv SGML_CATALOG_FILES "$thisSgmlCatalog:\$SGML_CATALOG_FILES"
endif
endif
# /etc/SGML/catalog exists but is not in SGML_CATALOG_FILES, so add it
if ( -f /etc/sgml/catalog && "\\\`echo \$SGML_CATALOG_FILES | grep -v /etc/sgml/catalog\\\`" != "" ) then
  setenv SGML_CATALOG_FILES {\$SGML_CATALOG_FILES}:/etc/sgml/catalog
endif
EOF

if [ -n "$myCatalogManager" ]; then
  myCatalogManagerDir=${myCatalogManager%/*}
  while read; do
    echo "$REPLY" >> $mydir/.profile.incl
  done <<EOF


if [ -z "\$CLASSPATH" ]; then
  CLASSPATH="$myCatalogManagerDir"
else
  # $myCatalogManagerDir is not in CLASSPATH, so add it
  if [ "\${CLASSPATH#*$myCatalogManagerDir*}" = "\$CLASSPATH" ]; then
    CLASSPATH="$myCatalogManagerDir$classPathSeparator\$CLASSPATH"
  fi
fi
export CLASSPATH
EOF

  while read; do
    echo "$REPLY" >> $mydir/.cshrc.incl
  done <<EOF


if ( ! $\?CLASSPATH ) then
  setenv CLASSPATH "$myCatalogManagerDir"
# $myCatalogManagerDir is not in CLASSPATH, so add it
else if ( "\\\`echo \$CLASSPATH | grep -v $myCatalogManagerDir\\\`" != "" ) then
  setenv CLASSPATH "$myCatalogManagerDir$classPathSeparator\$CLASSPATH"
endif
endif
EOF

fi

while read; do
  echo "$REPLY" >> $mydir/.emacs.el
done <<EOF
(add-hook
  'nxml-mode-hook
  (lambda ()
    (setq rng-schema-locating-files-default
          (append '("$thisLocatingRules")
                  rng-schema-locating-files-default ))))
EOF

return 0
}

updateUserStartupFiles() {
  if [ ! "$batchmode" = "Yes" ]; then
  cat 1>&2 <<EOF

NOTE: To source your environment correctly for using the catalog
      files in this distribution, you need to update one or more
      of your shell startup files. This installer can
      automatically make the necessary changes. Or, if you prefer,
      you can make the changes manually.

EOF
  else
    emit_message
  fi

  # if running csh or tcsh, target .cshrc and .tcshrc files for
  # update; otherwise, target .bash_* and .profiles

  parent=$(ps -p $PPID | grep "/")
  if [ "${parent#*csh}" != "$parent" ] || [ "${parent#*tcsh}" != "$parent" ]; then
    myStartupFiles=".cshrc .tcshrc"
    appendLine="source $mydir/.cshrc.incl"
  else
    myStartupFiles=".bash_profile .bash_login .profile .bashrc"
    appendLine=". $mydir/.profile.incl"
  fi

  for file in $myStartupFiles; do
    if [ -f "$HOME/$file" ]; then
      dotFileBackup=$HOME/$file.$$.bak
      REPLY=""
      if [ ! "$batchmode" = "Yes" ]; then
        read -s -n1 -p "Update $HOME/$file? [Yes] "
        emit_message "$REPLY"
      fi
      case $REPLY in
        [nNqQ])
        cat 1>&2 <<EOF

NOTE: No change made to $HOME/$file. You either need
      to add the following line to it, or manually source
      the shell environment for this distribution each
      time you want use it.

$appendLine

EOF
        ;;
        *)
        lineExists="$(grep "$appendLine" $HOME/$file )"
        if [ ! "$lineExists" ]; then
          mv $HOME/$file $dotFileBackup     || exit 1
          cp $dotFileBackup $HOME/$file     || exit 1
          echo "$appendLine" >> $HOME/$file || exit 1
          cat 1>&2 <<EOF
NOTE: Successfully updated the following file:
      $HOME/$file 
      Backup written to:
      $dotFileBackup

EOF
        else
          cat 1>&2 <<EOF
NOTE: The following file already contains information for this
      distribution, so I did not update it.
      $HOME/$file

EOF
        fi
        ;;
      esac
    fi
  done
  if [ -z "$dotFileBackup" ]; then
    if [ ! "$batchmode" = "Yes" ]; then
      emit_message
    fi
    cat 1>&2 <<EOF
NOTE: No shell startup files updated. You can source the
      environment for this distribution manually, each time you
      want to use it, by typing the following.

$appendLine

EOF
  fi
}

updateUserDotEmacs() {
  if [ -f $thisLocatingRules ]; then
  cat 1>&2 <<EOF

NOTE: This distribution includes a "schema locating rules" file
      for Emacs/nXML.  To use it, you should update either your
      .emacs or .emacs.el file.  This installer can automatically
      make the necessary changes. Or, if you prefer, you can make
      the changes manually.

EOF

  emacsAppendLine="(load-file \"$mydir/.emacs.el\")"
  myEmacsFile=
  for file in .emacs .emacs.el; do
    if [ -f "$HOME/$file" ]; then
      myEmacsFile=$HOME/$file
      break
    fi
  done
  if [ ! -f "$myEmacsFile" ]; then
    REPLY=""
    if [ ! "$batchmode" = "Yes" ]; then
      read -s -n1 -p "No .emacs or .emacs.el file. Create one? [No] "
      emit_message "$REPLY"
      emit_message
    fi
    case $REPLY in
      [yY])
      myEmacsFile=$HOME/.emacs
      touch $myEmacsFile
      ;;
      *)
      cat 1>&2 <<EOF
NOTE: No Emacs changes made. To use this distribution with,
      Emacs/nXML, you can create a .emacs file and manually add
      the following line to it, or you can run it as a command
      within Emacs.

$emacsAppendLine

EOF
      ;;
    esac
  fi
  if [ -n "$myEmacsFile" ]; then
    REPLY=""
    if [ ! "$batchmode" = "Yes" ]; then
      read -s -n1 -p  "Update $myEmacsFile? [Yes] "
      emit_message "$REPLY"
      emit_message
    fi
    case $REPLY in
      [nNqQ])
      cat 1>&2 <<EOF

NOTE: No change made to $myEmacsFile. To use this distribution
      with Emacs/nXML, you can manually add the following line
      to your $myEmacsFile, or you can run it as a command
      within Emacs.

$emacsAppendLine

EOF
      ;;
      *)
      lineExists="$(grep "$emacsAppendLine" $myEmacsFile)"
      if [ ! "$lineExists" ]; then
        dotEmacsBackup=$myEmacsFile.$$.bak
        mv $myEmacsFile $dotEmacsBackup    || exit 1
        cp $dotEmacsBackup $myEmacsFile    || exit 1
        echo "$emacsAppendLine" >> $myEmacsFile || exit 1
        cat 1>&2 <<EOF
NOTE: Successfully updated the following file:
      $myEmacsFile
      Backup written to:
      $dotEmacsBackup
EOF
      else
        cat 1>&2 <<EOF

NOTE: The following file already contains information for this
      distribution, so I did not update it.
      $myEmacsFile

EOF
      fi
      ;;
    esac
  fi
fi
}

uninstall() {
  if [ ! "$batchmode" = "Yes" ]; then
  cat 1>&2 <<EOF

NOTE: To "uninstall" this distribution, the changes made to your
      CatalogManagers.properties, startup files, and/or .emacs
      file need to be reverted. This uninstaller can automatically
      revert them.  Or, if you prefer, you can revert them manually.

EOF
  fi

  if [ "$osName" = "Cygwin" ]; then
    thisXmlCatalog=$thisJavaXmlCatalog
  fi

  # make "escaped" version of PWD to use with sed and grep
  escapedPwd=$(echo $mydir | sed "s#/#\\\\\/#g")

  # check to see if a non-empty value for catalogManager was fed
  # to uninstaller.
  if [ -n ${1#--catalogManager=} ]; then
    myCatalogManager=${1#--catalogManager=}
    catalogBackup="$myCatalogManager.$$.bak"
    catalogsLine=$(grep "^catalogs=" $myCatalogManager)
    if [ "$catalogsLine" ] ; then
      if [ "${catalogsLine#*$thisXmlCatalog*}" != "$catalogsLine" ]; then
        REPLY=""
        if [ ! "$batchmode" = "Yes" ]; then
          read -s -n1 -p "Revert $myCatalogManager? [Yes] "
          emit_message "$REPLY"
        fi
        case $REPLY in
          [nNqQ]*)
          cat 1>&2 <<EOF

NOTE: No change made to $myCatalogManager. You need to manually
      remove the following path from the "catalog=" line.

          $thisXmlCatalog

EOF
          ;;
          *)
          mv $myCatalogManager $catalogBackup || exit 1
          sed "s#^catalogs=\(.*\)$thisXmlCatalog\(.*\)\$#catalogs=\1\2#" $catalogBackup \
          | sed 's/;\+/;/' | sed 's/;$//' | sed 's/=;/=/' > $myCatalogManager || exit 1
          cat 1>&2 <<EOF
NOTE: Successfully updated the following file:
      $myCatalogManager
      Backup written to:
      $catalogBackup

EOF
          ;;
        esac
      else
        emit_message "NOTE: No data for this distribution found in:"
        emit_message "       $myCatalogManager"
        emit_message
      fi
    else
      cat 1>&2 <<EOF
NOTE: No data for this distribution was found in the following
      file, so I did not revert it.
      $myCatalogManager
EOF
    fi
  fi

  if [ -n "$myEmacsFile" ]; then 
    # check to see if a non-empty value for --dotEmacs file was fed
    # to uninstaller.
    if [ -n ${2#--dotEmacs=} ]; then
      myEmacsFile=${2#--dotEmacs=}
      revertLine="(load-file \"$escapedPwd\/\.emacs\.el\")"
      loadLine="$(grep "$revertLine" "$myEmacsFile")"
      if [ -n "$loadLine" ]; then
        emit_message
        REPLY=""
        if [ ! "$batchmode" = "Yes" ]; then
          read -s -n1 -p "Revert $myEmacsFile? [Yes] "
          emit_message "$REPLY"
        fi
        case $REPLY in
          [nNqQ]*)
          cat 1>&2 <<EOF

NOTE: No change made to $myEmacsFile. You need to manually
remove the following line.

(load-file \"$mydir/.emacs.el\")

EOF
          ;;
          *)
          dotEmacsBackup=$myEmacsFile.$$.bak
          sed -e "/$revertLine/d" -i".$$.bak" $myEmacsFile  || exit 1
          cat 1>&2 <<EOF
NOTE: successfully reverted the following file:
      $myEmacsFile
      Backup written to:
      $dotEmacsBackup

EOF
          ;;
        esac
      else
        emit_message "NOTE: No data for this distribution found in:"
        emit_message "      $myEmacsFile"
      fi
    fi
  fi

  # check all startup files
  myStartupFiles=".bash_profile .bash_login .profile .bashrc .cshrc .tcshrc"
  for file in $myStartupFiles; do
    if [ -e "$HOME/$file" ]; then
      case $file in
        .tcshrc|.cshrc)
        revertLine="source $mydir/.cshrc.incl"
        revertLineEsc="source $escapedPwd\/\.cshrc\.incl"
        ;;
        *)
        revertLine=". $mydir/.profile.incl"
        revertLineEsc="\. $escapedPwd\/\.profile\.incl"
        ;;
      esac
      lineExists="$(grep "$revertLineEsc" $HOME/$file )"
      if [ "$lineExists" ]; then
        REPLY=""
        if [ ! "$batchmode" = "Yes" ]; then
          read -s -n1 -p "Update $HOME/$file? [Yes] "
          emit_message "$REPLY"
        fi
        case $REPLY in
          [nNqQ]*)
          cat 1>&2 <<EOF

NOTE: No change made to $HOME/$file. You need to manually remove
      the following line from it.

 $revertLine

EOF
          ;;
          *)
          dotFileBackup=$HOME/$file.$$.bak
          sed -e "/$revertLineEsc/d" -i".$$.bak" $HOME/$file  || exit 1
          cat 1>&2 <<EOF
NOTE: Successfully updated the following file:
      $HOME/$file
      Backup written to:
      $dotFileBackup

EOF
          ;;
        esac
      else
        emit_message "NOTE: No data for this distribution found in:"
        emit_message "      $HOME/$file"
        emit_message
      fi
    fi
  done
  removeOldFiles
  emit_message "Done. Deleted uninstall.sh file."
  rm -f $mydir/test.sh      || exit 1
  rm -f $mydir/uninstall.sh || exit 1
}

writeUninstallFile() {
  uninstallFile=$mydir/uninstall.sh
  echo '#!/bin/bash'                               > $uninstallFile || exit 1
  echo 'mydir=$(cd -P $(dirname $0) && pwd -P)'   >> $uninstallFile || exit 1
  echo "\$mydir/install.sh \\"                    >> $uninstallFile || exit 1
  echo "  --uninstall \\"                         >> $uninstallFile || exit 1
  echo "  --catalogManager=$myCatalogManager \\"  >> $uninstallFile || exit 1
  echo "  --dotEmacs='$myEmacsFile' \\"           >> $uninstallFile || exit 1
  echo '  $@'                                     >> $uninstallFile || exit 1
  chmod 755 $uninstallFile || exit 1
}

writeTestFile() {
  testFile=$mydir/test.sh
  echo "#!/bin/bash"                                > $testFile || exit 1
  echo 'mydir=$(cd -P $(dirname $0) && pwd -P)'    >> $testFile || exit 1
  echo '$mydir/install.sh --test'                  >> $testFile || exit 1
  chmod 755 $testFile || exit 1
}

printExitMessage() {
  cat 1>&2 <<EOF
To source your shell environment for this distribution, type the
following:

$appendLine

EOF
}

checkForResolver() {
  resolverResponse="$(java org.apache.xml.resolver.apps.resolver uri -u foo 2>/dev/null)"
  if [ -z "$resolverResponse" ]; then
    cat 1>&2 <<EOF

NOTE: Your environment does not seem to contain the Apache XML
      Commons Resolver; without that, you can't use XML catalogs
      with Java applications. For more information, see the "How
      to use a catalog file" section in Bob Stayton's "DocBook
      XSL: The Complete Guide"

      http://sagehill.net/docbookxsl/UseCatalog.html

EOF
  fi
}

emitNoChangeMsg() {
  cat 1>&2 <<EOF

NOTE: No changes were made to CatalogManagers.properties. To
      provide your Java tools with XML catalog information for
      this distribution, you will need to make the appropriate
      changes manually.

EOF
}

testCatalogs() {
  if [ ! -f "$thisXmlCatalog" ]; then
    cat 1>&2 <<EOF

FATAL: $thisXmlCatalog file needed but not found. Stopping.
EOF
  exit
  fi

  if [ -z "$XML_CATALOG_FILES" ]; then
    emit_message
    emit_message "WARNING: XML_CATALOG_FILES not set. Not testing with xmlcatalog."
  else
    xmlCatalogResponse="$(xmlcatalog 2>/dev/null)"
    if [ -z "$xmlCatalogResponse" ]; then
    cat 1>&2 <<EOF

WARNING: Cannot locate the "xmlcatalog" command. Make sure that
         you have libxml2 and its associated utilities installed.

         http://xmlsoft.org/

EOF
    else
      emit_message "Testing with xmlcatalog..."
      # read in pathname-uri pairs from .urilist file
      while read pair; do
        if [ ! "${pair%* *}" = "." ]; then
          path=$mydir/${pair%* *}
        else
          path=$mydir/
        fi
        uri=${pair#* *}
        emit_message
        emit_message "  Tested: $uri"
        for catalog in $XML_CATALOG_FILES; do
          response="$(xmlcatalog $catalog $uri| grep -v "No entry")"
          if [ -n "$response" ]; then
            if [ "$response" = "$path" ]; then
              emit_message "  Result: $path"
              break
            else
              emit_message "  Result: FAILED"
            fi
          fi
        done
      done < $mydir/.urilist
    fi
  fi

  if [ -z "$CLASSPATH" ]; then
    emit_message
    emit_message "NOTE: CLASSPATH not set. Not testing with Apache XML Commons Resolver."
  else
    if [ "$(checkForResolver)" ]; then
      checkForResolver
    else
      emit_message
      emit_message "Testing with Apache XML Commons Resolver..."
      # read in pathname-uri pairs from .urilist file
      while read pair; do
        if [ ! "${pair%* *}" = "." ]; then
          path=$mydir/${pair%* *}
        else
          path=$mydir/
        fi
        uri=${pair#* *}
        emit_message
        emit_message "  Tested: $uri"
        if [ ${uri%.dtd} != $uri ]; then
          response="$(java org.apache.xml.resolver.apps.resolver system -s $uri | grep "Result")"
        else
          response="$(java org.apache.xml.resolver.apps.resolver uri -u $uri | grep "Result")"
        fi
        if [ "$response" ]; then
          if [ "${response#*$path}" != "$response" ]; then
            emit_message "  Result: $path"
          else
            emit_message "  Result: FAILED"
          fi
          echo
        fi
      done < $mydir/.urilist
    fi
  fi
}

# get opts and execute appropriate function
case $1 in
  *-uninstall)
  uninstall $2 $3 $4
  ;;
  *-test)
  testCatalogs
  ;;
  *)
  main
  ;;
esac

# Copyright
# ---------
# Copyright 2005-2007 Michael(tm) Smith <smith@sideshowbarker.net>
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# vim: number