#!/usr/bin/python2.5
# -*- coding: utf-8 -*-
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##

import os
os.environ["USERNAME"]='user'
import py2deb

if __name__ == "__main__":
    try:
        os.chdir(os.path.dirname(sys.argv[0]))
    except:
        pass
    print
    p=py2deb.Py2deb("modrana")   #This is the package name and MUST be in lowercase! (using e.g. "mClock" fails miserably...)
    p.description="A flexible GPS navigation system.\n Online routing and POI search.\n Tiled maps with overlay support.\n Automatic and batch tile download.\n GPX Tracklog support."
    p.author="Martin Kolman"
    p.mail="modrana@gmail.com"
    p.depends = "python2.5, python-gtk2, python-cairo, python-location, python-support"
    p.section="user/navigation"
    p.icon = "modrana-icon.png"
    p.arch="all"                #should be all for python, any for all arch
    p.urgency="low"             #not used in maemo onl for deb os
    p.distribution="fremantle"
    p.repository="extras-devel"
    p.xsbc_bugtracker="http://talk.maemo.org/showthread.php?t=58861"
    p.postinstall="""#!/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 # 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

    """ #Set here your post install script
    p.postremove="""#!/bin/sh


set -e


# summary of how this script can be called:
#        * <postrm> `remove'
#        * <postrm> `purge'
#        * <old-postrm> `upgrade' <new-version>
#        * <new-postrm> `failed-upgrade' <old-version>
#        * <new-postrm> `abort-install'
#        * <new-postrm> `abort-install' <old-version>
#        * <new-postrm> `abort-upgrade' <old-version>
#        * <disappearer's-postrm> `disappear' <r>overwrit>r> <new-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
        remove)
#               ldconfig
                ;;

        purge|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
                ;;
        *)
                echo "postrm called with unknown argument \`$1'" >&2
                exit 1

esac

if [ "$1" = "remove" ]; then
        rm -rf /opt/modrana/*.pyc
	rm -rf /opt/modrana/*.py
        rm -rf /opt/modrana/*.pyo
	rm -rf /opt/modrana/modules
        rm -rf /opt/modrana/cache/images/*.png
        if [ -w  /opt/modrana/cache/tracklogs/tracklog_cache.txt ];then
          rm /opt/modrana/cache/tracklogs/tracklog_cache.txt
        fi


fi

if [ "$1" = "purge" ]; then
        echo "removing all modrana files"
	rm -rf /opt/modrana/
fi

exit 0

      
    """ #Set here your post remove script

    #  p.preinstall="""#!/bin/sh
    #  chmod +x /usr/bin/mclock.py""" #Set here your pre install script
    #  p.preremove="""#!/bin/sh
    #  chmod +x /usr/bin/mclock.py""" #Set here your pre remove script
    version = "0.12"
    build = "9"
                                #Text with changelog information to be displayed in the package "Details" tab of the Maemo Application Manager
    changeloginformation = "fix icon size\nadd access permission for subfolders\nthis time using the correct N900 specific find syntax\nkudos kureyon"


    dir_name = "src"            #Name of the subfolder containing your package source files (e.g. usr\share\icons\hicolor\scalable\myappicon.svg, usr\lib\myapp\somelib.py). We suggest to leave it named src in all projects and will refer to that in the wiki article on maemo.org
    #Thanks to DareTheHair from talk.maemo.org for this snippet that recursively builds the file list 
    for root, dirs, files in os.walk(dir_name):
        real_dir = root[len(dir_name):]
        fake_file = []
        for f in files:
            fake_file.append(root + os.sep + f + "|" + f)
        if len(fake_file) > 0:
            p[real_dir] = fake_file
    print p
    r = p.generate(version,build,changelog=changeloginformation,tar=True,dsc=True,changes=True,build=False,src=True)
