#!/bin/sh
# postinst script for backend-kinopoisk-common
#
# 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)
        # cpufrequtils
        echo "ENABLE="true"
        GOVERNOR="performance"" > /etc/default/cpufrequtils
        /etc/init.d/cpufrequtils restart

        # locale
        LALL=`locale -a|grep utf8|wc -l`
        LSMALL="100"

        if [[ "$LALL" -lt "$LSMALL" ]]; then
            echo "install locales utf8"
            cat /usr/share/i18n/SUPPORTED | grep UTF-8 > /tmp/local
            cat /usr/share/i18n/SUPPORTED | grep CP1251 >> /tmp/local
            mv /tmp/local /var/lib/locales/supported.d/local
            dpkg-reconfigure locales
        else
            echo "set default locale"
            echo "LANG=ru_RU.cp1251
            LANGUAGE="en_US:en"
            LC_ALL=ru_RU.cp1251" > /etc/default/locale
        fi

        # disable host check
        cat /etc/ssh/ssh_config | sed 's/#   StrictHostKeyChecking ask/StrictHostKeyChecking no/g' > /etc/ssh/ssh_config.new
        mv /etc/ssh/ssh_config.new /etc/ssh/ssh_config
        # chown www-data
        chown -R www-data:www-data /var/www/
        chmod 600 /var/www/.ssh/id_rsa


        # add dir for default host
        mkdir -p /home/www/default
        # enable kp conf
        echo "Include conf.d/" > /etc/apache2/apache2.conf
        >/etc/php5/apache2/php.ini
        >/etc/php5/cli/php.ini
        >/etc/php5/cgi/php.ini

        if [ ! -f /etc/yandex/environment.type ]; then echo "No environment file found"; exit 1; fi
        yaenv=$(cat /etc/yandex/environment.type)
        echo "${yaenv} environment detected"

        rm -f /etc/php5/conf.d/php.ini
        ln -sf /etc/php5/php.ini.${yaenv} /etc/php5/conf.d/php.ini

        a2ensite 00-default.conf
        #TODO: disable at non-stable environments
        a2ensite 01-www.kinopoisk.ru.conf
        a2ensite 02-ext.kinopoisk.ru.conf
        if [ ${yaenv} == "testing" ]; then
            a2ensite 03-testing.dev.kinopoisk.ru.conf
            a2ensite 05-ext-testing.kinopoisk.ru.conf
        elif [ ${yaenv} == "prestable" ]; then
            a2ensite 04-prestable.dev.kinopoisk.ru.conf
        fi
        # restart apache2
        /etc/init.d/apache2 restart
        # restart tinyproxy
        cp /etc/tinyproxy-kp.conf /etc/tinyproxy.conf
        /etc/init.d/tinyproxy restart
    ;;

    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
