TIVI-153: add as dependency for iputils
[profile/ivi/sgml-common.git] / bin / install-catalog.in
1 #!/bin/sh
2 # Script to install a catalog in the centralized SGML catalog
3 # Send any comments to Eric Bischoff <eric@caldera.de>
4 # This program is under GPL license. See LICENSE file for details.
5
6 # Set help message
7 SGML_HELP_MESSAGE="Usage: `basename $0` [<option>] <action>\n\
8 where <option> is:\n\
9 \040 -d|--delegate: \t\t\t Use DELEGATE instead of CATALOG\n\
10 and where <action> is:\n\
11 \040 -a|--add <centralized> <ordinary>: \t Declare ordinary catalog in the centralized catalog\n\
12 \040 -r|--remove <centralized> <ordinary>:\t Remove ordinary catalog from the centralized catalog\n\
13 \040 -h, --help: \t\t\t\t Print this help message and exit\n\
14 \040 -v, --version: \t\t\t Print the version number and exit\n"
15
16 # Set version message
17 SGML_VERSION_MESSAGE="sgml-common version @VERSION@ (install-catalog version 1.0)"
18
19 # Set type of pointer
20 SGML_POINTER="CATALOG"
21
22 # Set action to be performed
23 SGML_ACTION=""
24
25 # Set catalogs
26 SGML_CENTRALIZED=""
27 SGML_ORDINARY=""
28
29 # Process options
30 case $1 in
31    -d|--delegate) SGML_POINTER="DELEGATE"
32                 shift 1
33                 ;;
34 esac
35
36 # Process actions
37 case $1 in
38    -a|--add)    SGML_ACTION="addition"
39                 SGML_CENTRALIZED=$2
40                 SGML_ORDINARY=$3
41                 ;;
42    -r|--remove) if [ -z "$3" -o "$3" = "--version" ]
43                 then
44                   echo "install-catalog: Old syntax; doing nothing"
45                   exit 0
46                 fi
47                 SGML_ACTION="removal"
48                 SGML_CENTRALIZED=$2
49                 SGML_ORDINARY=$3
50                 ;;
51    -h|--help)   echo -e $SGML_HELP_MESSAGE
52                 exit 0
53                 ;;
54    -v|--version) echo -e $SGML_VERSION_MESSAGE
55                 exit 0
56                 ;;
57    --install)   echo "install-catalog: Old syntax; doing nothing"
58                 exit 0
59                 ;;
60    *)           echo -e $SGML_HELP_MESSAGE >&2
61                 exit 1
62                 ;;
63 esac
64
65 # Check that the super catalog can be created and changed and deleted
66 if [ ! -w /etc/sgml ]
67 then
68   echo "`basename $0`: unable to write in /etc/sgml." >&2
69   exit 2
70 fi
71 case $SGML_ACTION in
72    addition)
73         if [ -e /etc/sgml/catalog -a ! -w /etc/sgml/catalog ]
74         then
75           echo "`basename $0`: can not modify \"/etc/sgml/catalog\"." >&2
76           exit 2
77         fi
78         ;;
79    removal)
80         if [ ! -w /etc/sgml/catalog ]
81         then
82           echo "`basename $0`: can not modify \"/etc/sgml/catalog\"." >&2
83           exit 2
84         fi
85         ;;
86 esac
87
88 # Check that the centralized catalog can be created, changed and deleted
89 if [ -z "$SGML_CENTRALIZED" ]
90 then
91   echo -e $SGML_HELP_MESSAGE >&2
92   exit 1
93 fi
94 case $SGML_ACTION in
95    addition)
96         if [ -e $SGML_CENTRALIZED -a ! -w $SGML_CENTRALIZED ]
97         then
98           echo "`basename $0`: can not modify \"$SGML_CENTRALIZED\"." >&2
99           exit 2
100         fi
101         ;;
102    removal)
103         if [ ! -w $SGML_CENTRALIZED ]
104         then
105           echo "`basename $0`: can not modify \"$SGML_CENTRALIZED\"." >&2
106           exit 2
107         fi
108         ;;
109 esac
110
111 # Check that we have at least one ordinary package to process
112 if [ -z "$SGML_ORDINARY" ]
113 then
114   echo -e $SGML_HELP_MESSAGE >&2
115   exit 1
116 fi
117 case $SGML_ACTION in
118    addition)
119         if [ ! -s $SGML_ORDINARY ]
120         then
121           echo "`basename $0`: \"$SGML_ORDINARY\" does not exist or is empty." >&2
122           exit 2
123         fi
124         ;;
125 esac
126
127 # Installation or removal of pointers
128 case $SGML_ACTION in
129     addition)
130         echo "`basename $0`: addition of $SGML_ORDINARY in $SGML_CENTRALIZED"
131         if grep -q $SGML_ORDINARY $SGML_CENTRALIZED 2>/dev/null
132         then
133           echo "Warning: $SGML_ORDINARY is already installed in the centralized catalog $SGML_CENTRALIZED" >&2
134         else
135           echo "$SGML_POINTER $SGML_ORDINARY" >> $SGML_CENTRALIZED
136         fi
137         grep -q $SGML_CENTRALIZED /etc/sgml/catalog 2>/dev/null
138         if [ $? -ne 0 ]
139         then
140           echo "`basename $0`: addition of $SGML_CENTRALIZED in /etc/sgml/catalog"
141           echo "$SGML_POINTER $SGML_CENTRALIZED" >> /etc/sgml/catalog
142         fi
143         ;;
144    removal)
145         echo "`basename $0`: removal of $SGML_ORDINARY from $SGML_CENTRALIZED"
146         if grep -q $SGML_ORDINARY $SGML_CENTRALIZED 2>/dev/null
147         then
148           sed -e "\:$SGML_POINTER $SGML_ORDINARY:d" < $SGML_CENTRALIZED > ${SGML_CENTRALIZED}.new
149           mv ${SGML_CENTRALIZED}.new $SGML_CENTRALIZED
150         else
151           echo "Warning: $SGML_ORDINARY was not found in the centralized catalog $SGML_CENTRALIZED" >&2
152         fi
153         if [ ! -s $SGML_CENTRALIZED ]
154         then
155           rm $SGML_CENTRALIZED
156           echo "`basename $0`: removal of $SGML_CENTRALIZED from /etc/sgml/catalog"
157           sed -e "\:$SGML_POINTER $SGML_CENTRALIZED:d" < /etc/sgml/catalog > /etc/sgml/catalog.new
158           mv /etc/sgml/catalog.new /etc/sgml/catalog
159         fi
160         ;;
161 esac
162
163 exit 0