#!/bin/bash
# postinst script 
#
# 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


common_settings(){
    # configure alternatives
    _update_alternatives=$(which update-alternatives)
    if [ -x "${_update_alternatives}" ]; then
        ${_update_alternatives} --install /bin/sh sh /bin/bash 100
        ${_update_alternatives} --install /usr/bin/editor editor /usr/bin/vim 100
    fi


    # configure locales
    for locale in en_GB.UTF-8 en_IE.UTF-8 en_US.UTF-8 ru_RU.UTF-8 ; do
        sed -i -e "s|.*${locale}.*|${locale} UTF-8|" /etc/locale.gen
    done
    locale-gen

    # update grub
    update-grub2
}


security_settings(){
    sed -i -e "s/\(^#.*\)\(auth.*required.*pam_wheel.so$\)/\2/" /etc/pam.d/su
    systemctl restart ssh
}


clean_motd(){
    [ -f /etc/motd ] && etc_motd=$(md5sum /etc/motd | awk '{print $1}')
    [ -f /usr/share/base-files/motd ] && base_motd=$(md5sum /usr/share/base-files/motd | awk '{print $1}')

    if [ $etc_motd = $base_motd ] ; then 
        > /etc/motd
    fi
}



case "$1" in
    configure)
        for i in 60-fs-tuning.conf 60-network-tuning.conf 60-vm-tuning.conf ; do
            sysctl -p /etc/sysctl.d/$i || true
        done

        common_settings || true
        security_settings || true
        clean_motd || true
    ;;

    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

