#!/bin/sh

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

# this is very important for generating all pyc files (the debhleper-declaration)
#DEBHELPER# 

echo "postinst running"

echo "generating *.pyc files"
# generate *.pyc files to speed up startup
# also, after changing the permissions user ran python cant create them
python2.5 -m compileall /opt/modrana

echo "seting modRana folder ownership and permissions"
# NOTE: other means in this case the user running modrana
chown -R root:root /opt/modrana # all owned by root:root by default
chmod o+x /opt/modrana # other can access the main directory
find /opt/modrana -type d | xargs chmod o+x # make sure all subdirectories are accessible by other (thanks kureyon!)
chmod -R o+r /opt/modrana # make sure other can read everything by default
# folders are writable by other only where needed:
chmod o+rw /opt/modrana/map_config.conf # map config file
chmod o+rw /opt/modrana/user_config.conf # user config file
chmod -R o+rw /opt/modrana/tracklogs # tracklogs + provision for possible future subfolders
chmod -R o+rw /opt/modrana/data # config storage + poi datastructure dump
chmod -R o+rw /opt/modrana/cache/tracklogs # tracklogs caching

echo "modRana upgrade: removing possible old cache files"
if [ -w  /opt/modrana/cache/tracklogs/tracklog_cache.txt ];then
  echo "removing tracklog_cache.txt"
  rm /opt/modrana/cache/tracklogs/tracklog_cache.txt
fi

exit 0

    