#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2005-2007 TUBITAK/UEKAE
# Licensed under the GNU General Public License, version 2.
# See the file http://www.gnu.org/copyleft/gpl.txt.

from pisi.actionsapi import autotools
from pisi.actionsapi import pisitools
from pisi.actionsapi import shelltools
from pisi.actionsapi import get

WorkDir = "linux-2.6.18"
EXTRAVERSION = 8
NoStrip = "/"

def setup():
    # Cleanup (i know its hacky!)
    shelltools.system("find . -name \"*.orig\" | xargs rm -f")

    # Branding
    if EXTRAVERSION is not None:
        pisitools.dosed("Makefile", "EXTRAVERSION =.*", "EXTRAVERSION = .%s-%s" % (EXTRAVERSION, get.srcRELEASE()))
    else:
        pisitools.dosed("Makefile", "EXTRAVERSION =.*", "EXTRAVERSION = -%s" % get.srcRELEASE())

    autotools.make("oldconfig")

def build():
    # Ugly hack but in order to embed splash-theme into kernel we have to make twice (second one just recreates bzImage)
    autotools.make()

    # Embed splash-theme into kernel
    shelltools.system("/usr/bin/splash_geninitramfs -v -g usr/initramfs_data.cpio.gz -r 1024x768 pardus")
    shelltools.touch("%s/usr/initramfs_data.cpio.gz" % get.curDIR())

    autotools.make()

def install():
    suffix = "%s-%s" % (get.srcVERSION(), get.srcRELEASE())

    # install modules
    autotools.rawInstall("INSTALL_MOD_PATH=%s/" % get.installDIR(), "modules_install")

    # install kernel headers
    autotools.rawInstall("INSTALL_HDR_PATH=%s/usr" % get.installDIR(), "headers_install")
    # alsa-headers provides these
    pisitools.removeDir("/usr/include/sound")

    # glibc provides these
    pisitools.removeDir("/usr/include/scsi/")

    # remove wrong symlinks
    pisitools.remove("/lib/modules/%s/source" % suffix)
    pisitools.remove("/lib/modules/%s/build" % suffix)

    # create true symlinks
    pisitools.dosym("/usr/src/linux-%s/" % suffix, "/lib/modules/%s/source" % suffix)
    pisitools.dosym("/usr/src/linux-%s/" % suffix, "/lib/modules/%s/build" % suffix)

    # insert System.map, bzImage and Module.symvers
    pisitools.insinto("/boot/", "System.map", "System.map-%s" % suffix)
    pisitools.insinto("/boot/", "arch/i386/boot/bzImage", "kernel-%s" % suffix)
    pisitools.insinto("/boot/", "Module.symvers", "Module.sysmvers-%s" % suffix)

    # cp source to installDIR for kernel-source package
    pisitools.dodir("/usr/src")
    shelltools.copytree("../%s/" % WorkDir, "%s/usr/src/linux-%s/" % (get.installDIR(), suffix))

    # create linux symlink
    pisitools.dosym("/usr/src/linux-%s/" % suffix, "/usr/src/linux")

    # Clean the on in installDIR not compiled one and prepare kernel for module compiliation
    shelltools.cd("%s/usr/src/linux-%s" % (get.installDIR(), suffix))
    autotools.make("clean")
    autotools.make("modules_prepare")

    # generate mkinitramfs
    shelltools.system("/sbin/mkinitramfs kernel=%s --full --root-dir=%s --output=/%s/boot" % (suffix, get.installDIR(), get.installDIR()))

    # remove modules.* files, they will autogenerated on next boot
    pisitools.remove("/lib/modules/%s/modules.*" % suffix)

    # used by postInstall
    pisitools.dosym("/boot/kernel-%s" % suffix, "/boot/latestkernel")
    pisitools.dosym("/boot/initramfs-%s" % suffix, "/boot/latestinitramfs")
