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

. /usr/share/debconf/confmodule

# Workaround https://bugs.launchpad.net/bugs/581796
GRUB_CONF=/etc/default/grub

# Create set_linkspeed script (#35251)
LINKSPEED=/usr/lib/eole/set_linkspeed

use_bash_as_default() {
    if [ "$(readlink -e /bin/sh)" != '/bin/bash' ];
    then
	echo "Use /bin/bash as /bin/sh."
	ln -nfs /bin/bash /bin/sh
    fi
}

gen_passwd() {
    pwgen -s -c -n -B -1 12
}

need_passwd_change() {
    login="${1}"
    password="${2}"

    correct=$(</etc/shadow awk -v user="${login}" -F : 'user == $1 {print $2}')
    if [ "$correct" = '*' ] || [ -z "${correct%!*}" ]
    then
	return 0
    fi
    prefix=${correct%"${correct#\$*\$*\$*\$}"}
    supplied=$(echo "$password" |
		      perl -e '$_ = <STDIN>; chomp; print crypt($_, $ARGV[0])' "$prefix")
    [ "$supplied" = "$correct" ]
}

gen_root_passwd() {
    PASSWD=$(gen_passwd)
    if [ -n "${PASSWD}" ]
    then
	echo "Generate random password for “root”... "
	if ! need_passwd_change "root" '$eole&123456$'
	then
	    cat <<'EOF'
No Password generation for “root”: current password is not “$eole&123456$”
EOF
	    return 0
	fi

	echo -n "root:${PASSWD}" | chpasswd -c SHA512 > /dev/null
	#ajout du mot de passe root par defaut au login
	echo "EOLE \l" > /etc/issue
	echo "Mot de passe par défaut de l'utilisateur root : ${PASSWD}" \
	     | sed -e 's,\\,\\\\,g' >> /etc/issue

	# Permit SSH root login by password
	# No need to restart SSH since server will reboot
	sed -i -e 's,^PermitRootLogin.*,PermitRootLogin yes,' /etc/ssh/sshd_config
    else
	echo "Unable to generate random password for “root”"
    fi
}

gen_eole_passwd() {
    if ! id eole > /dev/null 2>&1
    then
	create_eole_user
    fi

    PASSWD=$(gen_passwd)
    if [ -n "${PASSWD}" ]
    then
	echo "Generate random password for “eole”... "
	if ! need_passwd_change "eole" '$fpmf&123456$'
	then
	    cat <<'EOF'
No Password generation for “eole”: current password is not “$fpmf&123456$”
EOF
	    return 0
	fi
	echo -n "eole:${PASSWD}" | chpasswd -c SHA512 > /dev/null
    fi
}

create_eole_user() {
    echo "Create user “eole” with password disabled... "
    adduser --shell '/usr/bin/manage-eole' \
	    --gecos 'EOLE user' \
	    --disabled-password \
	    eole

    echo "Add user “eole” in “adm” and “mail” groups... "
    usermod --append --groups adm,mail eole
}

disable_apport() {
    if [ ! -f /etc/default/apport ];then
        echo "Disable apport"
        echo enabled=0 > /etc/default/apport
    fi
}

case "$1" in
    configure)
	use_bash_as_default
	[ $(grep ^adm /etc/group | grep -v syslog) ] && adduser syslog adm
	chown -R syslog:adm /var/log/rsyslog
	if [ -f "${GRUB_CONF}" ]
	then
	    sed -i -e 's,^#\?\(GRUB_DEFAULT=\).*,\1saved, ;
                       s,^\(GRUB_TIMEOUT_STYLE\).*,\1=menu, ;
                       s,^\(GRUB_HIDDEN_TIMEOUT.*\),#\1, ;
                       s,^\(GRUB_CMDLINE_LINUX_DEFAULT=\).*,\1"", ;
                       s,^\(GRUB_CMDLINE_LINUX=\).*,\1"rootdelay=90",' \
                ${GRUB_CONF}
	    # Make sure to update grub if configuration change
	    LVM_SUPPRESS_FD_WARNINGS=1 update-grub
	fi

    if [ ! -f "${LINKSPEED}" ]
    then
        echo '#!/bin/bash' > "${LINKSPEED}"
        chmod +x "${LINKSPEED}"
    fi

        # Purge old init script
        if [ -f /etc/init.d/bastion ]
        then
            rm -f /etc/init.d/bastion
            update-rc.d bastion remove
        fi

        disable_apport

	#enable, but only on new installs.
	if [ -z "$2" ]; then
	    # mise en commentaire pour http://dev-eole.ac-dijon.fr/issues/3349
	    for i in /home/*/.bashrc /root/.bashrc /etc/skel/.bashrc
	    do
		test -f "${i}" && sed -i 's/^HISTCONTROL.*$/# &/' "${i}"
	    done

	    #activation du pavé numérique sur la console
	    cat > /etc/rc.local <<EOF
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#activation du pavé numérique sur la console
for tty in /dev/tty[1-9];
do
    setleds -D +num < \$tty > /dev/null
    done

# fix du point sur le keypad temporaire
#loadkeys fr >/dev/null

exit 0
EOF

	    chmod +x /etc/rc.local

	    gen_root_passwd
	    gen_eole_passwd

	    DST=/etc/update-motd.d/
	    if [ -d $DST ]
	    then
		DISTRIB=/usr/share/eole/creole/distrib
		SRC=$DISTRIB/00-header
		if [ -f $SRC ]
		then
		    cp -f $SRC $DST
		    chown 755 $DST
		fi
		SRC=$DISTRIB/10-help-text
		if [ -f $SRC ]
		then
		    cp -f $SRC $DST
		    chown 755 $DST
		fi
	    fi
	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
