start.sh 1.93 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/bin/sh
#
# Start RoundCube.
#
# This script is invoked before starting apache2 and the PHP
# runner. It copies the configuration files in their desired location,
# and runs database upgrades if necessary.
#

CONFIG_DIR=/etc/roundcube
TARGET_DIR=/var/www/webmail

# Name of the MySQL database to use.
MYSQL_DB="ai_roundcube"

16 17 18 19 20 21
# If ${CONFIG_DIR}/my.cnf exists, use it as a defaults file for mysql.
MYSQL=mysql
if [ -e "${CONFIG_DIR}/my.cnf" ]; then
    MYSQL="${MYSQL} --defaults-file=${CONFIG_DIR}/my.cnf"
fi

ale's avatar
ale committed
22 23 24
die() {
    echo "ERROR: $*" >&2
    exit 1
ale's avatar
ale committed
25 26
}

ale's avatar
ale committed
27
# Abort if some fundamental environment variables are not defined.
ale's avatar
ale committed
28 29 30 31 32 33
# This is nicer than having Apache die with an error later.
test -n "${DOMAIN}" \
    || die "the 'DOMAIN' environment variable is not defined"
test -n "${SHARD_ID}" \
    || die "the 'SHARD_ID' environment variable is not defined"

34
# Verify that the main Roundcube configuration is in place.
ale's avatar
ale committed
35 36 37 38 39
test -e ${CONFIG_DIR}/config.inc.php \
    || die "${CONFIG_DIR}/config.inc.php is missing"

# Now set up the MySQL database for Roundcube. The current Roundcube
# version is stored in the database.
ale's avatar
ale committed
40 41
rc_version=$(cat ${TARGET_DIR}/.rc_version)
cur_version=$(${MYSQL} -NBe 'select version from rc_version' ${MYSQL_DB} 2>/dev/null)
ale's avatar
ale committed
42 43 44 45

# If cur_version is empty, it means that the database has not been
# initialized yet.
if [ -z "${cur_version}" ]; then
46
    ${MYSQL} ${MYSQL_DB} < ${TARGET_DIR}/SQL/mysql.initial.sql \
ale's avatar
ale committed
47
        || die "could not load initial SQL schema to ${MYSQL_DB}"
48
    ${MYSQL} -e "create table rc_version (version text); insert into rc_version (version) values ('unknown')" ${MYSQL_DB} \
ale's avatar
ale committed
49
        || die "could not create rc_version table in ${MYSQL_DB}"
ale's avatar
ale committed
50
fi
ale's avatar
ale committed
51 52
    
# Run database upgrade if necessary.
ale's avatar
ale committed
53 54 55
if [ "${rc_version}" != "${cur_version}" ]; then
    ${TARGET_DIR}/bin/update.sh -v ${cur_version} -y \
        || die "Roundcube update script failed"
ale's avatar
ale committed
56

ale's avatar
ale committed
57 58
    ${MYSQL} -e "update rc_version set version='${rc_version}'" ${MYSQL_DB}
fi
ale's avatar
ale committed
59 60

exit 0