#! /bin/sh

set +e

create_db () {
  echo "Checking for db"
  mysqladmin --defaults-file=/etc/mysql/debian.cnf -f reload
  # test if database if already present...
  if ! $(echo quit | mysql --defaults-file=/etc/mysql/debian.cnf zm > /dev/null 2> /dev/null) ; then
    echo "Creating zm db"
    cat /usr/share/zoneminder/db/zm_create.sql | mysql --defaults-file=/etc/mysql/debian.cnf
    if [ $? -ne 0 ]; then
      echo "Error creating db."
      exit 1;
    fi
  else
    echo "Db exists."
  fi
}

create_update_user () {
  USER_EXISTS="$(mysql --defaults-file=/etc/mysql/debian.cnf  -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$ZM_DB_USER')")"
  if [ $USER_EXISTS -ne 1 ]; then
    echo "Creating zm user $ZM_DB_USER"
    # This creates the user.
    echo "CREATE USER '${ZM_DB_USER}'@${ZM_DB_HOST} IDENTIFIED BY '${ZM_DB_PASS}';" | mysql --defaults-file=/etc/mysql/debian.cnf mysql
  fi
  echo "Updating permissions for user ${ZM_DB_USER}@${ZM_DB_HOST}"
  echo "GRANT LOCK tables,alter,drop,select,insert,update,delete,create,index,alter routine,create routine,trigger,execute,REFERENCES ON ${ZM_DB_NAME}.* TO '${ZM_DB_USER}'@${ZM_DB_HOST};" | mysql --defaults-file=/etc/mysql/debian.cnf mysql
}

update_db () {

  zmupdate.pl -s --nointeractive
  if [ $? -ne 0 ]; then
    echo "Error updating db."
    exit 1;
  fi
  zmupdate.pl --nointeractive -f
  if [ $? -ne 0 ]; then
    echo "Error updating config."
    exit 1;
  fi

  # Add any new PTZ control configurations to the database (will not overwrite)
  zmcamtool.pl --import >/dev/null 2>&1
  echo "Done Updating"
}

if [ "$1" = "configure" ]; then

  . /etc/zm/zm.conf
  for CONFFILE in /etc/zm/conf.d/*.conf; do
    . "$CONFFILE"
  done

  # The logs can contain passwords, etc... so by setting group root, only www-data can read them, not people in the www-data group
  chown www-data:root /var/log/zm
  chown www-data:www-data /var/lib/zm
  chown www-data:www-data /var/cache/zoneminder /var/cache/zoneminder/*
  if [ ! -e "/etc/apache2/mods-enabled/cgi.load" ] && [ "$(command -v a2enmod)" != "" ]; then
    echo "The cgi module is not enabled in apache2.  I am enabling it using a2enmod cgi."
    if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
        . /usr/share/apache2/apache2-maintscript-helper
        apache2_invoke enmod cgi || exit $?
    fi
  fi

  SYSTEMD=0
  if [ -e "/run/systemd/system" ]; then
    SYSTEMD=1
    echo "detected systemd"
    # Ensure zoneminder is stopped
    deb-systemd-invoke stop zoneminder.service || exit $?
  else
    # Ensure zoneminder is stopped
    invoke-rc.d zoneminder stop || true
  fi

  if [ "$ZM_DB_HOST" = "localhost" ]; then

    if [ $SYSTEMD -eq 1 ] && ([ -e "/lib/systemd/system/mysql.service" ] || [ -e "/lib/systemd/system/mariadb.service" ]); then
      #
      # Get mysql started if it isn't running
      #

      if [ -e "/lib/systemd/system/mariadb.service" ]; then
        DBSERVICE="mariadb.service"
      else
        DBSERVICE="mysql.service"
      fi
      echo "Detected db service is $DBSERVICE"
      if systemctl is-failed --quiet $DBSERVICE; then
        echo "$DBSERVICE is in a failed state; it will not be started."
        echo "If you have already resolved the problem preventing $DBSERVICE from running,"
        echo "run sudo systemctl restart $DBSERVICE then run sudo dpkg-reconfigure zoneminder."
        exit 1
      fi

      if ! systemctl is-active --quiet mysql.service mariadb.service; then
        # Due to /etc/init.d service autogeneration, mysql.service always returns the status of mariadb.service
        # However, mariadb.service will not return the status of mysql.service.
        deb-systemd-invoke start $DBSERVICE
      fi

      # Make sure systemctl status exit code is 0; i.e. the DB is running
      if systemctl is-active --quiet "$DBSERVICE"; then
        create_db
        create_update_user
        update_db
      else
        echo 'NOTE: MySQL/MariaDB not running; please start mysql and run dpkg-reconfigure zoneminder when it is running.'
      fi

    elif [ -e "/etc/init.d/mysql" ]; then
      #
      # Get mysql started if it isn't
      #
      if ! $(/etc/init.d/mysql status >/dev/null 2>&1); then
        service mysql start
      fi
      if $(/etc/init.d/mysql status >/dev/null 2>&1); then
        create_db
        create_update_user
        update_db
      else
        echo 'NOTE: MySQL/MariaDB not running; please start mysql and run dpkg-reconfigure zoneminder when it is running.'
      fi

    else
      echo 'MySQL/MariaDB not found; assuming remote server.'
    fi
  fi

else
  echo "Not doing database upgrade due to remote db server ($ZM_DB_HOST)."
fi

if [ $SYSTEMD -eq 1 ]; then
  deb-systemd-invoke restart zoneminder.service
#else
  #service zoneminder start || true
fi

#DEBHELPER#
