zoneminder/distros/debian8/zoneminder.postinst

168 lines
5.6 KiB
Plaintext
Raw Normal View History

#! /bin/sh
2014-12-02 07:58:18 +08:00
# postinst maintainer script for zoneminder
set -e
2014-12-03 20:40:30 +08:00
2014-12-07 04:39:50 +08:00
old_version=$2
2014-12-03 20:40:30 +08:00
# Source the debconf stuff
. /usr/share/debconf/confmodule
2014-12-03 20:40:30 +08:00
CONFIGFILE=/etc/zm/zm.conf
apache_install() {
2014-12-02 07:58:18 +08:00
mkdir -p /etc/apache2/conf-available
ln -sf ../../zm/apache.conf /etc/apache2/conf-available/zoneminder.conf
COMMON_STATE=$(dpkg-query -f '${Status}' -W 'apache2.2-common' 2>/dev/null | awk '{print $3}' || true)
if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
. /usr/share/apache2/apache2-maintscript-helper
apache2_invoke enconf zoneminder
elif [ "$COMMON_STATE" = "installed" ] || [ "$COMMON_STATE" = "unpacked" ] ; then
[ -d /etc/apache2/conf.d/ ] && [ ! -L /etc/apache2/conf.d/zoneminder.conf ] && ln -s ../conf-available/zoneminder.conf /etc/apache2/conf.d/zoneminder.conf
fi
2014-12-07 09:43:08 +08:00
# Enable CGI script module in apache (not enabled by default on jessie)
2014-12-02 07:58:18 +08:00
a2enmod cgi >/dev/null 2>&1
2014-12-02 07:58:18 +08:00
# Reload the web server
deb-systemd-invoke reload apache2.service || true
}
mysql_update() {
2014-12-07 04:39:50 +08:00
2014-12-02 07:58:18 +08:00
# Source the dbconfig stuff
. /usr/share/dbconfig-common/internal/mysql
2014-12-01 00:55:26 +08:00
2014-12-07 04:39:50 +08:00
# Only update the admin account at first install
if [ -z "$old_version" ]; then
if [ ! "$ADMIN_LOGIN" = "admin" ] || [ ! "$ADMIN_PASSWORD" = "admin" ]; then
# Remove the hard-coded default admin account
dbc_mysql_exec_command "DELETE FROM Users WHERE Username = 'admin' AND Password = password('admin');" || true
# Install the new admin account
dbc_mysql_exec_command "INSERT INTO Users VALUES (1,'$ADMIN_LOGIN',password('$ADMIN_PASSWORD'),'',1,'View','Edit','Edit','Edit','Edit','Edit','','');" || true
fi
fi
# Update the database version
dbc_mysql_exec_command "UPDATE Config SET Value = '$DB_VERSION' WHERE Name = 'ZM_DYN_DB_VERSION';" || true
2014-12-01 00:55:26 +08:00
}
2014-12-02 07:58:18 +08:00
# Exit if user didn't want to use debconf
db_get zoneminder/debconf_install
[ "$RET" = "false" ] && exit 0
2014-12-02 07:58:18 +08:00
# Source the config file if exists
[ ! -e $CONFIGFILE ] && exit 0
. $CONFIGFILE
2014-12-02 07:58:18 +08:00
if [ -f /usr/share/dbconfig-common/dpkg/postinst ]; then
# Set the first version in which dbconfig-common was introduced in the package
dbc_first_version="1.28.0"
# Set the database type
dbc_dbtypes="$ZM_DB_TYPE"
# Source the dbconfig-common stuff
. /usr/share/dbconfig-common/dpkg/postinst
fi
if [ "$1" = "configure" ] || [ "$1" = "reconfigure" ]; then
2014-12-02 07:58:18 +08:00
# Create the underlying database and populate it
# dbconfig will take care of applying any updates which are newer than the
# previously installed version
dbc_go zoneminder $@
# Get the adminitrator login and password from debconf
db_get zoneminder/admin_login
ADMIN_LOGIN=${RET:-admin}
db_get zoneminder/admin_password
ADMIN_PASSWORD=${RET:-admin}
# Get the lastest database version from dbconfig upgrade folder
DB_VERSION=$(ls -rv /usr/share/dbconfig-common/data/zoneminder/upgrade/$dbc_dbtypes | head -1)
# Update the default admin account and database version
mysql_update
2014-12-02 07:58:18 +08:00
2014-12-07 04:39:50 +08:00
if [ -z "$old_version" ]; then
2014-12-02 07:58:18 +08:00
2014-12-07 04:39:50 +08:00
# Retrieve data from dbconfig (inputs from user)
. /etc/dbconfig-common/zoneminder.conf
2014-12-02 07:58:18 +08:00
2014-12-07 04:39:50 +08:00
# ZoneMinder config file handling
2014-12-07 09:43:08 +08:00
# Inspired by: http://manpages.debian.org/cgi-bin/man.cgi?query=debconf-devel&sektion=7
2014-12-02 07:58:18 +08:00
2014-12-07 04:39:50 +08:00
# Backup the config file
cp -a -f $CONFIGFILE $CONFIGFILE.postinst.bak
2014-12-02 07:58:18 +08:00
2014-12-07 04:39:50 +08:00
# Redeclare variables if missing in config file
test -z $dbc_dbserver || grep -Eq "^ *ZM_DB_HOST=" $CONFIGFILE || echo "ZM_DB_HOST=" >> $CONFIGFILE
test -z $dbc_dbname || grep -Eq "^ *ZM_DB_NAME=" $CONFIGFILE || echo "ZM_DB_NAME=" >> $CONFIGFILE
test -z $dbc_dbuser || grep -Eq "^ *ZM_DB_USER=" $CONFIGFILE || echo "ZM_DB_USER=" >> $CONFIGFILE
test -z $dbc_dbpass || grep -Eq "^ *ZM_DB_PASS=" $CONFIGFILE || echo "ZM_DB_PASS=" >> $CONFIGFILE
2014-12-02 07:58:18 +08:00
2014-12-07 04:39:50 +08:00
# Prevent ZM_DB_HOST to be empty if user selected the 'unix socket' method
if test -z $dbc_dbserver; then
dbc_dbserver_override="localhost"
else
dbc_dbserver_override=$dbc_dbserver
fi
2014-12-02 07:58:18 +08:00
2014-12-07 04:39:50 +08:00
# Update variables in config file
sed -e "s/^ *ZM_DB_HOST=.*/ZM_DB_HOST=$dbc_dbserver_override/" \
-e "s/^ *ZM_DB_NAME=.*/ZM_DB_NAME=$dbc_dbname/" \
-e "s/^ *ZM_DB_USER=.*/ZM_DB_USER=$dbc_dbuser/" \
-e "s/^ *ZM_DB_PASS=.*/ZM_DB_PASS=$dbc_dbpass/" \
< $CONFIGFILE > $CONFIGFILE.postinst.bak
2014-12-02 07:58:18 +08:00
2014-12-07 04:39:50 +08:00
# Clean-up backup file
mv -f $CONFIGFILE.postinst.bak $CONFIGFILE
fi
2014-12-02 07:58:18 +08:00
# Set some file permissions
chown $ZM_WEB_USER:$ZM_WEB_GROUP /var/log/zm
chown $ZM_WEB_USER:$ZM_WEB_GROUP /var/lib/zm
if [ -z "$2" ]; then
chown $ZM_WEB_USER:$ZM_WEB_GROUP -R /var/cache/zoneminder
fi
# As requested by the Debian Webapps Policy Manual §3.2.1
chown root:$ZM_WEB_GROUP /etc/zm/zm.conf
chmod 640 /etc/zm/zm.conf
2014-12-02 07:58:18 +08:00
# Configure the web server
db_get zoneminder/webserver
webservers="$RET"
for webserver in $webservers; do
webserver=${webserver%,}
# Currently we only support apache2
if [ "$webserver" = "apache2" ] ; then
apache_install $1
2014-12-02 07:58:18 +08:00
fi
done
fi
if [ "$1" = "configure" ]; then
2014-12-02 07:58:18 +08:00
# Ensure zoneminder is stopped
deb-systemd-invoke stop zoneminder.service || exit $?
# If mysql server exists and is running
if [ -e "/usr/sbin/mysqld" ] && deb-systemd-invoke start mysql.service; then
# Run the ZoneMinder update tool
zmupdate.pl
else
# Otherwise exit without error (mysql server may not be available on
# this machine)
exit 0;
fi
fi
#DEBHELPER#
exit 0