### Copyright 1999-2014. Parallels IP Holdings GmbH. All Rights Reserved.

import sys, os, os.path, tarfile, subprocess
from contextlib import closing
from plesk_atomic import create_include_conf, comment_plesk_managed_directives, \
    run_aum_plesk_installer, prepare_ruleset_layout, fix_asl_config, deactivate_00_mod_security_conf, \
    TORTIX_WAF_FILENAME, run_aum, ASL_LICENSE, ASL_LICENSE_SAVED

def save_asl_license():
    """ backup asl license key to force aum use login/password integration (instead of the key one) """
    if os.path.isfile(ASL_LICENSE):
        os.rename(ASL_LICENSE, ASL_LICENSE_SAVED)

def main():
    try:
        username = os.environ["MODSEC_VENDOR_LOGIN"]
        password = os.environ["MODSEC_VENDOR_PASS"]

        target_archive_path = sys.argv[1]
        ruleset_dir = sys.argv[2]

        run_aum_plesk_installer()
        prepare_ruleset_layout(ruleset_dir)
        fix_asl_config(os.path.join(ruleset_dir, "modsec"), username, password)
        save_asl_license()

        run_aum()
        comment_plesk_managed_directives(os.path.join(ruleset_dir, "modsec", TORTIX_WAF_FILENAME))
        deactivate_00_mod_security_conf()

    except Exception as ex:
        sys.stderr.write("%s\n" % ex)
        sys.exit(1)

if __name__ == "__main__":
    main()

# vim: ts=4 sts=4 sw=4 et :
