diff --git a/start.sh b/start.sh index ac9db80470ea1e9c373cf6e8c1b82c3f6e1283a4..7ff7e09b9e67d06845954cd42128aad86e95e15d 100755 --- a/start.sh +++ b/start.sh @@ -13,53 +13,49 @@ TARGET_DIR=/var/www/webmail # Name of the MySQL database to use. MYSQL_DB="ai_roundcube" -install_config() { - install -o root -g root -m 644 ${CONFIG_DIR}/config.inc.php ${TARGET_DIR}/config/config.inc.php - - # For each known plugin, look for a NAME.config.inc.php file in - # the config directory: if found, copy it to the plugin directory - # as plugins/NAME/config.inc.php. - for p in $(cd ${TARGET_DIR}/plugins && echo *) ; do - if [ -e "${CONFIG_DIR}/${p}.config.inc.php" ]; then - install -o root -g root -m 644 ${CONFIG_DIR}/${p}.config.inc.php \ - ${TARGET_DIR}/plugins/${p}/config.inc.php - fi - done -} - -setup_database() { - # The current Roundcube version is stored in the database. - cur_version=$(mysql -NBe 'select version from rc_version' ${MYSQL_DB} 2>/dev/null) - - # If cur_version is empty, it means that the database has not been - # initialized yet. - if [ -z "${cur_version}" ]; then - mysql ${MYSQL_DB} < ${TARGET_DIR}/SQL/mysql.initial.sql - mysql -e "create table rc_version (version text); insert into rc_version (version) values ('unknown')" ${MYSQL_DB} - fi - - # Run database upgrade if necessary. - ${TARGET_DIR}/bin/update.sh -v ${cur_version} -y - - mysql -e "update rc_version set version='${RC_VERSION}'" ${MYSQL_DB} +die() { + echo "ERROR: $*" >&2 + exit 1 } - -set -e -set -u - # Abort if some fundamental environment variables are not defined. -# This is nicer than having Apache die with an error. -if [ -z "${DOMAIN}" ]; then - echo "ERROR: The 'DOMAIN' environment variable is not defined" >&2 - exit 1 -fi -if [ -z "${SHARD_ID}" ]; then - echo "ERROR: The 'SHARD_ID' environment variable is not defined" >&2 - exit 1 +# 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" + +# Install the main Roundcube configuration to TARGET_DIR/config. +test -e ${CONFIG_DIR}/config.inc.php \ + || die "${CONFIG_DIR}/config.inc.php is missing" +install -o root -g root -m 644 ${CONFIG_DIR}/config.inc.php ${TARGET_DIR}/config/config.inc.php + +# For each known plugin, look for a NAME.config.inc.php file in +# the config directory: if found, copy it to the plugin directory +# as plugins/NAME/config.inc.php. +for p in $(cd ${TARGET_DIR}/plugins && echo *) ; do + test -e "${CONFIG_DIR}/${p}.config.inc.php" \ + && install -o root -g root -m 644 ${CONFIG_DIR}/${p}.config.inc.php \ + ${TARGET_DIR}/plugins/${p}/config.inc.php +done + +# Now set up the MySQL database for Roundcube. The current Roundcube +# version is stored in the database. +cur_version=$(mysql -NBe 'select version from rc_version' ${MYSQL_DB} 2>/dev/null) + +# If cur_version is empty, it means that the database has not been +# initialized yet. +if [ -z "${cur_version}" ]; then + mysql ${MYSQL_DB} < ${TARGET_DIR}/SQL/mysql.initial.sql \ + || die "could not load initial SQL schema to ${MYSQL_DB}" + mysql -e "create table rc_version (version text); insert into rc_version (version) values ('unknown')" ${MYSQL_DB} \ + || die "could not create rc_version table in ${MYSQL_DB}" fi + +# Run database upgrade if necessary. +${TARGET_DIR}/bin/update.sh -v ${cur_version} -y \ + || die "Roundcube update script failed" -install_config -setup_database +mysql -e "update rc_version set version='${RC_VERSION}'" ${MYSQL_DB} exit 0