set -e
-
die() {
echo "$@" 1>&2
exit 1
}
-
generate_config_file() {
touch "$2"
chown "root:$username" "$2"
-e "s/__OPACSERVER__/$domain/g" \
-e "s/__INTRASERVER__/$intradomain/g" \
-e "s/__ZEBRA_PASS__/$zebrapwd/g" \
+ -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
+ -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
-e "s/__DB_NAME__/$mysqldb/g" \
+ -e "s/__DB_HOST__/$mysqlhost/g" \
-e "s/__DB_USER__/$mysqluser/g" \
-e "s/__DB_PASS__/$mysqlpwd/g" \
-e "s/__UNIXUSER__/$username/g" \
"/etc/koha/$1" > "$2"
}
+getmysqlhost() {
+ awk '
+ /^\[/ { inclient = 0 }
+ /^\[client\]/ { inclient = 1 }
+ inclient && /^ *host *=/ { print $3 }' \
+ /etc/mysql/koha-common.cnf
+}
+
+getinstancemysqlpassword() {
+ sed -n '/<pass>/s:.*>\(.*\)</pass>.*:\1:p' \
+ "/etc/koha/sites/$1/koha-conf.xml"
+}
# Set defaults and read config file, if it exists.
DOMAIN=""
INTRAPREFIX=""
INTRASUFFIX=""
DEFAULTSQL=""
+ZEBRA_MARC_FORMAT="marc21"
+ZEBRA_LANGUAGE="en"
if [ -e /etc/koha/koha-sites.conf ]
then
. /etc/koha/koha-sites.conf
fi
+[ $# -ge 2 ] && [ $# -le 6 ] ||
+ die "Usage: $0 [--create-db|--request-db|--populate-db] \
+[--marcflavor marc21|normarc|unimarc] \
+[--zebralang en|fr|nb] instancename"
+
+TEMP=`getopt -o crpm:l: -l create-db,request-db,populate-db,marcflavor:,zebralang: \
+ -n "$0" -- "$@"`
+
+# Note the quotes around `$TEMP': they are essential!
+eval set -- "$TEMP"
+
+while true ; do
+ case "$1" in
+ -c|--create-db) op=create ; shift ;;
+ -r|--request-db) op=request ; shift ;;
+ -p|--populate-db) op=populate ; shift ;;
+ -m|--marcflavor) ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
+ -l|--zebralang) ZEBRA_LANGUAGE="$2" ; shift 2 ;;
+ -d|--defaultsql) DEFAULTSQL="$2" ; shift 2 ;;
+ --) shift ; break ;;
+ *) die "Internal error! " ;;
+ esac
+done
-# Parse command line.
-[ "$#" = 1 ] || die "Usage: $0 instancename"
name="$1"
+
domain="$name$DOMAIN"
if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
then
fi
-# Create new user and group.
-username="$name-koha"
-if getent passwd "$username" > /dev/null
-then
- die "User $username already exists."
-fi
-if getent group "$username" > /dev/null
+mysqldb="koha_$name"
+mysqlhost="$(getmysqlhost)"
+mysqluser="koha_$name"
+
+if [ "$op" = create ] || [ "$op" = request ]
then
- die "Group $username already exists."
+ mysqlpwd="$(pwgen -1)"
+else
+ mysqlpwd="$(getinstancemysqlpassword $name)"
fi
-adduser --no-create-home --disabled-login --gecos "Koha instance $username" \
- --quiet "$username"
-
-
-# Create the site-specific directories.
-mkdir "/etc/koha/sites/$name"
-
-mkdir "/var/lock/koha/$name"
-mkdir "/var/lock/koha/$name/authorities"
-mkdir "/var/lock/koha/$name/biblios"
-chown -R "root:$username" "/var/lock/koha/$name"
-chmod -R g+w "/var/lock/koha/$name"
-[ -d "/var/spool/koha/$name" ] || mkdir "/var/spool/koha/$name"
-
-install -d -o "$username" -g "$username" "/var/lib/koha/$name"
-install -d -o "$username" -g "$username" "/var/lib/koha/$name/authorities"
-install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios"
-install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios/key"
-install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios/register"
-install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios/shadow"
-
-install -d "/var/run/koha/$name/authorities"
-install -d "/var/run/koha/$name/biblios"
-# Generate Zebra database password.
-zebrapwd="$(pwgen -1)"
-
-
-# Set up MySQL database for this instance.
-mysqldb="koha_$name"
-mysqluser="koha_$name"
-mysqlpwd="$(pwgen -1)"
-mysql --defaults-extra-file=/etc/mysql/debian.cnf <<eof
-CREATE DATABASE $mysqldb;
-CREATE USER '$mysqluser' IDENTIFIED BY '$mysqlpwd';
-GRANT ALL PRIVILEGES ON $mysqldb.* TO '$mysqluser';
+if [ "$op" = create ] || [ "$op" = request ]
+then
+ # Create new user and group.
+ username="$name-koha"
+ if getent passwd "$username" > /dev/null
+ then
+ die "User $username already exists."
+ fi
+ if getent group "$username" > /dev/null
+ then
+ die "Group $username already exists."
+ fi
+ adduser --no-create-home --disabled-login \
+ --gecos "Koha instance $username" \
+ --home "/var/lib/koha/$name" \
+ --quiet "$username"
+
+ # Create the site-specific directories.
+ koha-create-dirs "$name"
+
+ # Generate Zebra database password.
+ zebrapwd="$(pwgen -1)"
+ # Set up MySQL database for this instance.
+ if [ "$op" = create ]
+ then
+ mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
+CREATE DATABASE \`$mysqldb\`;
+CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
+GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
FLUSH PRIVILEGES;
eof
+ fi
+
+ # Generate and install Apache site-available file and log dir.
+ generate_config_file apache-site.conf.in \
+ "/etc/apache2/sites-available/$name"
+ mkdir "/var/log/koha/$name"
+ chown "$username:$username" "/var/log/koha/$name"
+
+
+ # Generate and install main Koha config file.
+ generate_config_file koha-conf-site.xml.in \
+ "/etc/koha/sites/$name/koha-conf.xml"
+
+ # Generate and install Zebra config files.
+ generate_config_file zebra-biblios-site.cfg.in \
+ "/etc/koha/sites/$name/zebra-biblios.cfg"
+ generate_config_file zebra-authorities-site.cfg.in \
+ "/etc/koha/sites/$name/zebra-authorities.cfg"
+ generate_config_file zebra-authorities-dom-site.cfg.in \
+ "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
+ generate_config_file zebra.passwd.in \
+ "/etc/koha/sites/$name/zebra.passwd"
+
+
+ # Create a GPG-encrypted file for requesting a DB to be set up.
+ if [ "$op" = request ]
+ then
+ touch "$name-db-request.txt"
+ chmod 0600 "$name-db-request.txt"
+ cat > "$name-db-request.txt" << eof
+Please create a MySQL database and user on $mysqlhost as follows:
+
+database name: $mysqldb
+database user: $mysqluser
+ password: $mysqlpwd
+
+Thank you.
+eof
+
+ echo "See $name-db-request.txt for database creation request."
+ echo "Please forward it to the right person, and then run"
+ echo "$0 --populate-db $name"
+ echo "Thanks."
+ fi
+fi
-# Use the default database content if that exists.
-if [ -e "$DEFAULTSQL" ]
+if [ "$op" = create ] || [ "$op" = populate ]
then
- # Populate the database with default content.
- zcat "$DEFAULTSQL" |
- sed "s/__KOHASITE__/$name/g" |
- mysql --defaults-extra-file=/etc/mysql/debian.cnf
-
-
- # Change the default user's password.
- staffpass="$(pwgen -1)"
- staffdigest=$(echo -n "$staffpass" |
- perl -e '
- use Digest::MD5 qw(md5_base64);
- while (<>) { print md5_base64($_), "\n"; }')
- mysql --defaults-extra-file=/etc/mysql/debian.cnf <<eof
+ # Use the default database content if that exists.
+ if [ -e "$DEFAULTSQL" ]
+ then
+ # Populate the database with default content.
+ zcat "$DEFAULTSQL" |
+ sed "s/__KOHASITE__/$name/g" |
+ mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
+
+
+ # Change the default user's password.
+ staffpass="$(pwgen -1)"
+ staffdigest=$(echo -n "$staffpass" |
+ perl -e '
+ use Digest::MD5 qw(md5_base64);
+ while (<>) { print md5_base64($_), "\n"; }')
+ mysql --host="$mysqlhost" --user="$mysqluser" \
+--password="$mysqlpwd" <<eof
USE \`$mysqldb\`;
UPDATE borrowers
SET password = '$staffdigest'
WHERE borrowernumber = 3;
eof
- echo "staff user password is '$staffpass' but keep that secret"
-else
- echo "Koha instance is empty, no staff user created."
-fi
-
-# Generate and install Apache site-available file and log dir.
-generate_config_file apache-site.conf.in "/etc/apache2/sites-available/$name"
-mkdir "/var/log/koha/$name"
-chown "$username:$username" "/var/log/koha/$name"
+ echo "staff user password is '$staffpass' but keep that secret"
-
-# Generate and install main Koha config file.
-generate_config_file koha-conf-site.xml.in \
- "/etc/koha/sites/$name/koha-conf.xml"
+ # Upgrade the database schema, just in case the dump was from an
+ # old version.
+ koha-upgrade-schema "$name"
+ else
+ echo "Koha instance is empty, no staff user created."
+ fi
+fi
-# Generate and install Zebra config files.
-generate_config_file zebra-biblios-site.cfg.in \
- "/etc/koha/sites/$name/zebra-biblios.cfg"
-generate_config_file zebra-authorities-site.cfg.in \
- "/etc/koha/sites/$name/zebra-authorities.cfg"
-generate_config_file zebra-authorities-dom-site.cfg.in \
- "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
+if [ "$op" = create ] || [ "$op" = populate ]
+then
+ # Reconfigure Apache.
+ a2ensite "$name"
+ service apache2 restart
+ # Start Zebra.
+ koha-start-zebra "$name"
+fi
-# Upgrade the database schema, just in case the dump was from an old version.
-koha-upgrade-schema "$name"
+if [ "$op" = request ]
+then
+ koha-disable "$name"
+fi
-# Reconfigure Apache.
-a2ensite "$name"
-service apache2 restart
+echo <<eoh
-# Start Zebra.
-koha-start-zebra "$name"
+Email for this instance is disabled. When you're ready to enable it, use:
+koha-email-enable $name
+eoh