#!/usr/bin/python2
# -*- coding: utf-8 -*-

__author__ = "James Meyer"
__date__   = "Dec 11th 2012"


import subprocess, sys


class plymouth_driver():
    def __init__(self):
        self.videocard = self.find_video_card()
        self.old_conf = self.read_mkinit()
        self.set_old_hook(self.old_conf)


    def getvc(self):
        return self.videocard

    def get_old_hooks(self):
        return self.old_hooks

    def get_old_modules(self):
        return self.old_modules

    def get_new_hooks(self):
        return self.new_hooks

    def get_new_modules(self):
        return self.new_modules


    def set_old_hook(self,conf):
        self.old_hooks, self.old_modules = self.find_hooks_modules(conf)

    def find_video_card(self):
        videocard=""
        conf_file="/etc/X11/xorg.conf.d/20-automv.conf"
        lines=[]
        try:
            f=open(conf_file,'r')
            lines=f.readlines()
            f.close()
        except:
            print "    find_video_card: couldn't read in %s" %conf_file
            videocard = "unknown"

        for line in lines:
            parsed=line.split()
            if parsed[0].strip() == "Driver":
                videocard = parsed[1].strip('''"''')
                break

        return  videocard

    def read_mkinit(self):
        conf_file="/etc/mkinitcpio.conf"
        old_conf=[]
        try:
            f=open(conf_file,'r')
            old_conf=f.readlines()
        except:
            print "    plymouth_config: couldn't read %s" %conf_file
            sys.exit(1)
        return old_conf

    def find_hooks_modules(self,conf_list):
        for line in conf_list:
            if line.startswith('HOOKS='):
                hooks=line.split('''"''')[1].split()
            if line.startswith('MODULES='):
                modules=line.split('''"''')[1].split()
        return hooks,modules

    def add_hooks(self,add_hooks, hooks):
        new_hooks = hooks
        for i in add_hooks:
            if not i in (new_hooks):
                new_hooks.append(i)
        return new_hooks

    def remove_hooks(self,remove_hooks, hooks):
        for i in remove_hooks:
            try:
                while True:
                    hooks.remove(i)
            except:
                pass

        return hooks

    def add_modules(self, add_modules, modules):
        new_modules = modules
        for i in add_modules:
            if not i in (new_modules):
                new_modules.append(i)
        return new_modules


    def remove_modules(self,remove_modules, modules):
        for i in remove_modules:
            try:
                while True:
                    modules.remove(i)
            except:
                pass
        return modules

    def create_new_hooks_modules(self):
        new_hooks = list(self.old_hooks)
        new_modules = list(self.old_modules)

        add_hooks=[]
        remove_hooks=[]
        remove_modules=[]
        add_modules=[]

        if self.videocard == "intel":
           add_modules = ['i915']
           remove_hooks = ['v86d']
        else:
           #add_modules = ['nfs','jm']
           remove_modules = ['i915']
           #remove and add to ensure v86d is first
           remove_hooks = ['v86d','plymouth']
           add_hooks = ['v86d','plymouth']

        new_hooks = self.remove_hooks(remove_hooks, new_hooks)
        new_hooks = self.add_hooks(add_hooks , new_hooks)
        new_modules = self.remove_modules(remove_modules, new_modules)
        new_modules = self.add_modules(add_modules, new_modules)

        self.new_hooks = new_hooks
        self.new_modules = new_modules

    def compare_hooks_modules(self):
        h = cmp( self.old_hooks,self.new_hooks)
        m = cmp( self.old_modules,self.new_modules)
        if h == 0 and m == 0:
            return True
        else:
            return False

    def create_newmkinitcpio(self):
        if self.compare_hooks_modules() == True:
            print "    plymouth_config: no changes made"

            return False
        else:
            conf_file="/etc/mkinitcpio.conf"
            try:
                f=open(conf_file,'w')
                for line in self.old_conf:
                    new_line=line
                    if line.startswith('HOOKS='):
                        new_line = 'HOOKS="%s"' %(" ".join(self.new_hooks))
                    if line.startswith('MODULES='):
                        new_line = 'MODULES="%s"'%(" ".join(self.new_modules))
                    f.write(new_line)
                f.close()
            except:
                print "    plymouth_config: couldn't write %s" %conf_file
                return False

        return True

    def run_mkinitcpio(self):
        print "    plymouth_config: Running mkinitcpio "
        subprocess.call(["/usr/bin/mkinitcpio", "-p" , "linux"])


def go():
    p = plymouth_driver()
    vc = p.getvc()
    print "    plymouth_config: found videocard %s" %vc
    p.create_new_hooks_modules()
    #print p.get_old_hooks()
    #print p.get_new_hooks()
    #print "=="
    #print p.get_old_modules()
    #print p.get_new_modules()

    if p.create_newmkinitcpio() == True or "--mkinitcpio" in sys.argv:
        p.run_mkinitcpio()




if __name__ == '__main__':
    go()

