# -*- coding: utf-8 -*-
#
# script made by Z
#



def ecrire(p): 
    fsortie.write ("// =============================//\n")
    fsortie.write ("// script generated by zeus \n")
    fsortie.write ("// a Z tool\n")
    fsortie.write ("// z_arma@hotmail.com\n")
    fsortie.write ("// =============================//\n")
    fsortie.write ("\n")
#    print p
    for i in p:
        fsortie.write(i + "\n")

def lireconfig():
    config = []
    fconfig.readlines
    for line in fconfig:
            config.append(line.strip()) ,
    print "param extraits \n" + str(config)
    return config
def liremission(config):
    selection = []
    fmission.readlines
    for line in fmission:
        for chaine in config:            
            if line.find(chaine) > -1:
                selection.append(line.strip()),
    return selection
    
def affine(selection):
    print "selection dans affine\n" + str(selection)
    top=False
    liste=[]
    for i in selection:
#        print i, top
        # vehicules
        chaine = str("text=")
        if i.find(chaine) > -1:
           top=True
           liste.append(i)
        if i.find("health=") > -1:
            if top==True:
                liste.append(i)
        if i.find("skill=") > -1:
            if top==True:
                liste.append(i)
        if i.find("vehicle=") > -1:
            if top==True:
                liste.append(i)
        if i.find("azimut=") > -1:
            if top==True:
                liste.append(i)
        if i.find("placement=") > -1:
            if top==True:
                liste.append(i)
        if i.find("position[]=") > -1:
            if top==True:
                liste.append(i)
        if i.find("presence=") > -1:
            if top==True:
                liste.append(i)
        # waypoint
        if i.find("type=") > -1:
            if top==True:
                liste.append(i)
        if i.find("combatMode=") > -1:
            if top==True:
                liste.append(i)
        if i.find("formation=") > -1:
            if top==True:
                liste.append(i)
        if i.find("speed=") > -1:
            if top==True:
                liste.append(i)
        if i.find("combat=") > -1:
            if top==True:
                liste.append(i)                                
        
        # rupture class
        if i.find("Item") > -1:
#            print "item ???"  , top
            if top==True:
                liste.append("finclasse")
                top=False 
    print "liste \n" + str(liste)               
    return liste

def scriptsqf(p):
    lescript = []
    finvehicule=False
    markers , placement , special, skill, armor = "[]","0","NONE",0,1
    print "scriptsqf(p) \n" + str(p)
    for i in p:
        chaine = "vehicle="
        if i.find(chaine) > -1:
            letype = str(i).lstrip(chaine).rstrip(";")
        chaine = "azimut="
        if i.find(chaine) > -1:
            azimut = str(i).lstrip(chaine).rstrip(";")
        chaine = "position[]={"
        if i.find(chaine) > -1:
            laposition = i.lstrip(chaine).rstrip("};") + ",0"
            liste = laposition.split(",")
            liste.pop(1)
            laposition = str(liste).lstrip("[").rstrip("]").replace("'", "")
        chaine = "skill="
        if i.find(chaine) > -1:
            skill = i.split("=",1)[-1]
        chaine = "health="
        if i.find(chaine) > -1:
            armor = i.split("=",1)[-1]
        chaine = "type="
        if i.find(chaine) > -1:
            armor = i.split("=",1)[-1]
        chaine = "combatMode="
        if i.find(chaine) > -1:
            armor = i.split("=",1)[-1]
        chaine = "formation="
        if i.find(chaine) > -1:
            armor = i.split("=",1)[-1]
        chaine = "speed="
        if i.find(chaine) > -1:
            armor = i.split("=",1)[-1]
        chaine = "combat="
        if i.find(chaine) > -1:
            armor = i.split("=",1)[-1]
        
        # fin de classe
        chaine = "finclasse"
        if i.find(chaine) > -1:
            finvehicule=True
        
        if finvehicule:
            result = "_pos = [" + laposition + "]"
            lescript.append(result)
            result = "_veh = createVehicle [" + letype + ", _pos," + markers + "," + placement + "," + special+"];"
            lescript.append(result)
            if azimut:
                result = "_veh setDir " + azimut + ";"
                lescript.append(result)
            result = "_veh setPos _pos;"
            lescript.append(result)
            result = "_veh setSkill " + skill 
            lescript.append(result)
            if armor != 1:
                result = "_veh setVehicleArmor " + str(armor) 
                lescript.append(result)
            lescript.append("")
            
            markers , placement , special, skill, armor = "[]","0","NONE",0,1
            finvehicule=False
    return lescript

#### d�but    
print "DEBUT"                
fsortie = open ("F:\zas\script.sqf","w")
fconfig = open ("F:\zas\config.txt","r")
fmission = open ("F:\zas\mission.sqm","r")


config = lireconfig()

selection = liremission(config)
selection.reverse()
laliste = affine(selection)
lescript = scriptsqf(laliste)
ecrire(lescript)

fmission.close
fconfig.close
fsortie.close

print "FIN"
