#!/bin/sh
# postinst script for yandex-wordstat-frontend
#
# 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


case "$1" in
    configure)
        # creating group if it isn't already there
        if ! getent group wordstat >/dev/null; then
            addgroup --system --quiet wordstat
        fi

        # creating user if it isn't already there
        if ! getent passwd wordstat >/dev/null; then
            adduser --system --quiet \
                --ingroup wordstat \
                --no-create-home \
                --shell /bin/false \
                wordstat
        fi

        if /etc/init.d/memcached status > /dev/null; then
            echo "Stopping memcached"
            /etc/init.d/memcached stop
        fi

        if [ "$(update-rc.d -f -n memcached remove | wc -l)" -gt 2 ]; then
            echo "Removing memcached from /etc/rc*.d/"
            update-rc.d -f memcached remove
        fi

        if /etc/init.d/nginx status > /dev/null; then
            echo "Stopping nginx"
            /etc/init.d/nginx stop
        fi

        if [ "$(update-rc.d -f -n nginx remove | wc -l)" -gt 2 ]; then
            echo "Removing nginx from /etc/rc*.d/"
            update-rc.d -f nginx remove
        fi

        chown wordstat:wordstat /var/lib/wordstat
        chown wordstat:wordstat /opt/wordstat_errors

        for  l in ru_RU en_GB tr_TR uk_UA; do
            if ! locale -a | grep "$l.utf8" > /dev/null; then
                locale-gen "$l.utf8"
            fi
        done
    ;;

    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
