#!/bin/sh
set -e

generate_pem_file() {

if [ ! -f /usr/local/etc/piler.pem ]; then
	echo "generating pem file . . ."
	openssl req -new -newkey rsa:4096 -days 3650 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -keyout /usr/local/etc/piler.pem -out 1.cert
	cat 1.cert >> /usr/local/etc/piler.pem
	chmod 600 /usr/local/etc/piler.pem
	rm 1.cert
fi

}


generate_key_file() {

if [ ! -f /usr/local/etc/piler.key ]; then
        echo "generating key file . . ."
	dd if=/dev/urandom bs=56 count=1 of=/usr/local/etc/piler.key
	chgrp piler /usr/local/etc/piler.key
	chmod 640 /usr/local/etc/piler.key
fi

}


fix_permissions() {
   chown piler:piler /usr/local/bin/pilerexport /usr/local/bin/pilerpurge /usr/local/bin/pilerimport /usr/local/bin/pilerget /usr/local/bin/pileraget /usr/local/bin/reindex
   chmod +s /usr/local/bin/pilerexport /usr/local/bin/pilerpurge /usr/local/bin/pilerimport /usr/local/bin/pilerget /usr/local/bin/pileraget /usr/local/bin/reindex
   chgrp piler /usr/local/etc/piler.conf
   chmod 640 /usr/local/etc/piler.conf

   chown piler:piler /var/piler/imap /var/piler/sphinx /var/piler/stat
}


case "$1" in
    configure)
	chown piler:piler /var/run/piler /var/piler/store /var/piler/tmp

	generate_pem_file
	###generate_key_file
	fix_permissions

	ldconfig

        ;;

    upgrade|abort-upgrade)
        ;;

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

#DEBHELPER#

exit 0


