#!/bin/bash
# postinst script for ymc-mysql-config
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

# Source debconf library.
#. /usr/share/debconf/confmodule

ymc_mysql_config_dir="/etc/mysql/ymc-mysql-config"

case "$1" in
  configure)
    for my_numbered_service in $(fliwi-get-my-services)
    do
      my_service=$(echo $my_numbered_service | sed -r 's/-[0-9]+$//')
      if [ -z "$my_service" ]; then
        continue
      fi
      fliwi-config-update --service $my_service || true
    done

    . /usr/lib/lib-ymc/ymc-mysql-tools.sh

    link_config=0
    restart_mysql=0

    service_based_config_dir=$(get_service_base_config_dir)
    if [ $? -ne 0 ] || \
       [ "$service_based_config_dir" == "" ]; then
      ymc_error_msg "Failed to calculate the service-based config-dir"
      exit 1
    fi

    if [ ! -d "$service_based_config_dir" ]; then
      ymc_error_msg "Config dir '$service_based_config_dir' missing"
    fi

    if [ ! -L "$ymc_mysql_config_dir" ]; then
      link_config=1
    else
      if [ "$(readlink -m $ymc_mysql_config_dir)" != "$(readlink -m $service_based_config_dir)" ]; then
        link_config=1
      fi
    fi

    if [ $link_config -eq 1 ]; then
      if [ -L "$ymc_mysql_config_dir" ] || \
         [ -f "$ymc_mysql_config_dir" ]; then
        rm -f $ymc_mysql_config_dir
      elif [ -e "$ymc_mysql_config_dir" ]; then
        rm -rf $ymc_mysql_config_dir
      fi

      ln -s $service_based_config_dir $ymc_mysql_config_dir

      if [ "$2" != "" ]; then
        restart_mysql=1
      fi
    fi

    if [ "$2" == "" ]; then
        restart_mysql=1
      fi

    if [ $restart_mysql -eq 1 ]; then
      if which invoke-rc.d >/dev/null 2>&1; then
        invoke-rc.d mysql restart || true
      else
        /etc/init.d/mysql restart || true
      fi
    fi

    if [ "$2" == "" ]; then
        ### innodb_log_file_size might have been adjusts due to adding ymc-mysql-config //start
        try_fixup_nonrunning_mysql=0
        if $(which invoke-rc.d >/dev/null 2>&1); then
          if ! $(invoke-rc.d mysql status >/dev/null 2>&1); then
            try_fixup_nonrunning_mysql=1
          fi
        else
          if ! $(/etc/init.d/mysql mysql status >/dev/null 2>&1); then
            try_fixup_nonrunning_mysql=1
          fi
        fi

        if [ $try_fixup_nonrunning_mysql -eq 1 ]; then
          # The mysql-server is not running - probably due to a changed innodb_log_file_size
          # Since this is the initial install of ymc-mysql-config and setups
          # using ymc-mysql-config generally are run redundant on multiple
          # servers, it should be okay to move those old innodb logfiles out
          # of the way...
          autosave_dir="/var/lib/YMC-MYSQL-CONFIG-AUTOSAVE/ib_logfiles/$(date +%s)"
          mkdir -p $autosave_dir
          mv /var/lib/mysql/ib_logfile* $autosave_dir/
          if which invoke-rc.d >/dev/null 2>&1; then
            invoke-rc.d mysql start
          else
            /etc/init.d/mysql start
          fi
        fi
        ### innodb_log_file_size might have been adjusts due to adding ymc-mysql-config //end

        ### Import backups on backup-servers
        ymc-mysql-setup-auto-import-mysql-backupdata --no-interaction || true

        ### Setup needed mysql permissions
        ymc-mysql-setup-create-mysql-users --no-interaction || true
    fi
  ;;

  abort-upgrade|abort-remove|abort-deconfigure)
  ;;

  *)
    echo "postinst called with unknown argument \`$1'" >&2
    exit 1
  ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


