#!/bin/bash

# code  extrait issu du paquet nagios-common
set -e

. /usr/share/debconf/confmodule


setperm() {
    local user="$1"
    local group="$2"
    local mode="$3"
    local file="$4"
    shift 4
    # only do something when no setting exists
    #if ! dpkg-statoverride --list "$file" >/dev/null 2>&1; then
      chown "$user":"$group" "$file"
      chmod "$mode" "$file"
    #fi
}

case "$1" in
  configure)

    if ! getent passwd nagios > /dev/null ; then
      echo "Ajout de l'utilisateur nagios" 1>&2
      adduser --home /home/nagios --gecos "" --shell /bin/bash --disabled-password --force-badname nagios > /dev/null
    fi
		if [ -f /home/nagios/bashrc ]
		then
			mv /home/nagios/bashrc /home/nagios/.bashrc
			setperm nagios nagios 0644 /home/nagios/.bashrc
		fi
		if [ -f /home/nagios/bash_profile ]
		then
			mv /home/nagios/bash_profile /home/nagios/.bash_profile
			setperm nagios nagios 0644 /home/nagios/.bash_profile
		fi

		# explicitly set permissions on some files that are dependent
		# on the uid/gid of the nagios user, which is dynamically created.
        setperm nagios nagios 0750 /usr/lib/nagios/plugins
		for i in `ls /usr/lib/nagios/plugins`
		do
        	setperm nagios nagios 0750 /usr/lib/nagios/plugins/$i
		done
        setperm nagios nagios 0700 /home/nagios
        setperm nagios nagios 0700 /home/nagios/.ssh
        setperm nagios nagios 0600 /home/nagios/.ssh/authorized_keys
        setperm root root 0440 /etc/sudoers.d/psin
		for i in `ls /root/scripts`
		do
        	setperm root root 0550 /root/scripts/$i
		done
    ;;

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

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

