#/usr/bin/python

USAGE = "python install.py 'Skip the Fade.dazip' '/Users/username/Documents/BioWare/Dragon Age/'"

import os

import sys

ADDIN_LOCATION = 'Contents/Addins'

PACKAGE_LOCATION = 'Contents/packages'

DAO_ADDIN = 'ADDINS'

DAO_PKG = 'packages/core/data'

DAO_ADDIN_XML = lambda base: '{}/Settings/AddIns.xml' . format ( base )

if sys . argv [ 1 ] == '-h' :

print ( "USAGE (example): {}" . format ( USAGE ) )

sys . exit ( 0 )

def fmtname ( cmd ) :

# escape spaces before system call

cmd = cmd . replace ( ' ' , ' \ ' )

return cmd

def unformat ( f ) :

return f. replace ( ' \ ' , ' ' )

mod = fmtname ( sys . argv [ 1 ] )

daroot = fmtname ( sys . argv [ 2 ] ) # ~/Documents/BioWare/Dragon\ Age

mod_fld = mod. strip ( '.dazip' )

mod_zip = mod. strip ( 'dazip' ) + 'da.zip'

os . system ( 'cp {} {}' . format ( mod , mod_zip ) )

os . system ( 'unzip {} -d {}' . format ( mod_zip , mod_fld ) )

# pull in the lines of the DAO addins

da_addins_lines = open ( DAO_ADDIN_XML ( sys . argv [ 2 ] ) ) . readlines ( )

# pull in the lines of the mod xml

try :

mod_manifest_lines = open ( '{}/manifest.xml' . format ( unformat ( mod_fld ) ) ) . readlines ( )

except IOError :

mod_manifest_lines = open ( '{}/Manifest.xml' . format ( unformat ( mod_fld ) ) ) . readlines ( )

# check if this is already installed

uid_line = filter ( lambda line: 'UID=' in line , mod_manifest_lines ) [ 0 ]

line_fields = filter ( lambda t: '=' in t , uid_line. strip ( ) . lstrip ( ) . split ( ) )

uid = dict ( map ( lambda s: s. split ( '=' ) , line_fields ) ) [ 'UID' ]

if any ( [ 'UID={}' . format ( uid ) in line for line in da_addins_lines ] ) :

raise ValueError ( '{} already installed' . format ( mod ) )

# insert the mod lines into the correct location in the addins xml

# (after the last addinitem)

last_addin_index = filter ( lambda a: '</AddInItem>' in a [ 1 ] , enumerate ( da_addins_lines ) ) [ - 1 ] [ 0 ]

# get the mod lines to insert

mod_begin_index = filter ( lambda a: '<AddInItem' in a [ 1 ] , enumerate ( mod_manifest_lines ) ) [ 0 ] [ 0 ]

mod_end_index = filter ( lambda a: '</AddInItem>' in a [ 1 ] , enumerate ( mod_manifest_lines ) ) [ - 1 ] [ 0 ]

new_addins = da_addins_lines [ 0 :last_addin_index + 1 ]

new_addins. extend ( mod_manifest_lines [ mod_begin_index:mod_end_index + 1 ] )

new_addins. extend ( da_addins_lines [ last_addin_index + 1 : ] )

# make a backup of the DAO manifest

ctr = 1

backup = '{}.{}-{}' . format ( DAO_ADDIN_XML ( daroot ) , 'backup' , ctr )

while os . path . exists ( unformat ( backup ) ) :

ctr + = 1

backup = '{}.{}-{}' . format ( DAO_ADDIN_XML ( daroot ) , 'backup' , ctr )

os . system ( 'cp {}/Settings/AddIns.xml {}' . format ( daroot , backup ) )

# move the addin files to the proper location

os . system ( 'mv {}/{}/* {}{}/' . format ( mod_fld , ADDIN_LOCATION , daroot , DAO_ADDIN ) )

# check to see if there are package files

mod_packages = '{}/{}' . format ( mod_fld , PACKAGE_LOCATION )

if os . path . exists ( unformat ( mod_packages ) ) :

os . system ( 'mv {}/core/data/* {}{}' . format ( mod_packages , daroot , DAO_PKG ) )

# now replace addins.xml

addins_out = open ( unformat ( DAO_ADDIN_XML ( daroot ) ) , 'w' )

addins_out. write ( '' . join ( new_addins ) )