Bug 5313 - allow creation of libraries with hyphens
[koha.git] / debian / scripts / koha-create
1 #!/bin/sh
2 #
3 # koha-create -- Create a new Koha instance.
4 # Copyright 2010  Catalyst IT, Ltd
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 set -e
21
22
23 die() {
24     echo "$@" 1>&2
25     exit 1
26 }
27
28
29 generate_config_file() {
30     touch "$2"
31     chown "root:$username" "$2"
32     chmod 0640 "$2"
33     sed -e "s/__KOHASITE__/$name/g" \
34         -e "s/__OPACPORT__/80/g" \
35         -e "s/__INTRAPORT__/$INTRAPORT/g" \
36         -e "s/__OPACSERVER__/$domain/g" \
37         -e "s/__INTRASERVER__/$intradomain/g" \
38         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
39         -e "s/__DB_NAME__/$mysqldb/g" \
40         -e "s/__DB_HOST__/$mysqlhost/g" \
41         -e "s/__DB_USER__/$mysqluser/g" \
42         -e "s/__DB_PASS__/$mysqlpwd/g" \
43         -e "s/__UNIXUSER__/$username/g" \
44         -e "s/__UNIXGROUP__/$username/g" \
45         "/etc/koha/$1" > "$2"
46 }
47
48 getmysqlhost() {
49     awk '
50         /^\[/ { inclient = 0 }
51         /^\[client\]/ { inclient = 1 }
52         inclient && /^ *host *=/ { print $3 }' \
53         /etc/mysql/koha-common.cnf
54 }
55
56 getinstancemysqlpassword() {
57     sed -n '/<pass>/s:.*>\(.*\)</pass>.*:\1:p' \
58         "/etc/koha/sites/$1/koha-conf.xml"
59 }
60
61
62 # Set defaults and read config file, if it exists.
63 DOMAIN=""
64 INTRAPORT="8080"
65 INTRAPREFIX=""
66 INTRASUFFIX=""
67 DEFAULTSQL=""
68 if [ -e /etc/koha/koha-sites.conf ]
69 then
70     . /etc/koha/koha-sites.conf
71 fi
72
73
74 # Parse command line.
75 [ "$#" = 2 ] || 
76     die "Usage: $0 [--create-db|--request-db|--populate-db] instancename"
77 case "$1" in
78   --create-db) op=create ;;
79   --request-db) op=request ;;
80   --populate-db) op=populate ;;
81   *) die "Usage: $0 [--create-db|--request-db|--populate-db] instancename" ;;
82 esac
83
84 name="$2"
85 domain="$name$DOMAIN"
86 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
87 then
88     intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
89 else
90     intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
91 fi
92
93
94 mysqldb="koha_$name"
95 mysqlhost="$(getmysqlhost)"
96 mysqluser="koha_$name"
97
98 if [ "$op" = create ] || [ "$op" = request ]
99 then
100     mysqlpwd="$(pwgen -1)"
101 else
102     mysqlpwd="$(getinstancemysqlpassword $name)"
103 fi
104
105
106 if [ "$op" = create ] || [ "$op" = request ]
107 then
108     # Create new user and group.
109     username="$name-koha"
110     if getent passwd "$username" > /dev/null
111     then
112         die "User $username already exists."
113     fi
114     if getent group "$username" > /dev/null
115     then
116         die "Group $username already exists."
117     fi
118     adduser --no-create-home --disabled-login \
119         --gecos "Koha instance $username" \
120         --home "/var/lib/koha/$name" \
121         --quiet "$username"
122
123     # Create the site-specific directories.
124     koha-create-dirs "$name"
125
126     # Generate Zebra database password.
127     zebrapwd="$(pwgen -1)"
128     # Set up MySQL database for this instance.
129     if [ "$op" = create ]
130     then
131         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
132 CREATE DATABASE \`$mysqldb\`;
133 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
134 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
135 FLUSH PRIVILEGES;
136 eof
137     fi
138
139     # Generate and install Apache site-available file and log dir.
140     generate_config_file apache-site.conf.in \
141         "/etc/apache2/sites-available/$name"
142     mkdir "/var/log/koha/$name"
143     chown "$username:$username" "/var/log/koha/$name"
144
145
146     # Generate and install main Koha config file.
147     generate_config_file koha-conf-site.xml.in \
148         "/etc/koha/sites/$name/koha-conf.xml"
149
150     # Generate and install Zebra config files.
151     generate_config_file zebra-biblios-site.cfg.in \
152         "/etc/koha/sites/$name/zebra-biblios.cfg"
153     generate_config_file zebra-authorities-site.cfg.in \
154         "/etc/koha/sites/$name/zebra-authorities.cfg"
155     generate_config_file zebra-authorities-dom-site.cfg.in \
156         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
157     generate_config_file zebra.passwd.in \
158         "/etc/koha/sites/$name/zebra.passwd"
159
160
161     # Create a GPG-encrypted file for requesting a DB to be set up.
162     if [ "$op" = request ]
163     then
164         touch "$name-db-request.txt"
165         chmod 0600 "$name-db-request.txt"
166         cat > "$name-db-request.txt" << eof
167 Please create a MySQL database and user on $mysqlhost as follows:
168
169 database name: $mysqldb
170 database user: $mysqluser
171      password: $mysqlpwd
172
173 Thank you.
174 eof
175
176         echo "See $name-db-request.txt for database creation request."
177         echo "Please forward it to the right person, and then run"
178         echo "$0 --populate-db $name"
179         echo "Thanks."
180     fi
181 fi
182
183
184 if [ "$op" = create ] || [ "$op" = populate ]
185 then
186     # Use the default database content if that exists.
187     if [ -e "$DEFAULTSQL" ]
188     then
189         # Populate the database with default content.
190         zcat "$DEFAULTSQL" |
191         sed "s/__KOHASITE__/$name/g" |
192         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
193
194
195         # Change the default user's password.
196         staffpass="$(pwgen -1)"
197         staffdigest=$(echo -n "$staffpass" |
198                       perl -e '
199                             use Digest::MD5 qw(md5_base64); 
200                             while (<>) { print md5_base64($_), "\n"; }')
201         mysql --host="$mysqlhost" --user="$mysqluser" \
202 --password="$mysqlpwd" <<eof
203 USE \`$mysqldb\`;
204 UPDATE borrowers 
205 SET password = '$staffdigest' 
206 WHERE borrowernumber = 3;
207 eof
208
209         echo "staff user password is '$staffpass' but keep that secret"
210
211         # Upgrade the database schema, just in case the dump was from an 
212         # old version.
213         koha-upgrade-schema "$name"
214     else
215         echo "Koha instance is empty, no staff user created."
216     fi
217 fi
218
219
220 if [ "$op" = create ] || [ "$op" = populate ]
221 then
222     # Reconfigure Apache.
223     a2ensite "$name"
224     service apache2 restart
225
226     # Start Zebra.
227     koha-start-zebra "$name"
228 fi
229
230
231 if [ "$op" = request ]
232 then
233     koha-disable "$name"
234 fi
235
236 echo <<eoh
237
238 Email for this instance is disabled. When you're ready to enable it, use:
239 koha-email-enable $name
240 eoh