#!/usr/bin/env python

import re
import sys
import subprocess


def main(argv):
    """Takes the root of an application changes the appid and then deploys it to AppScale"""
    app_id, path = argv[0], argv[1]

    assert isinstance(path, str)
    if not path.endswith("/"):
        path = path + '/'

    file_path = path + 'war/WEB-INF/appengine-web.xml'
    print('Modifying app id to ' + app_id + ' at ' + file_path)

    try:
        f = open(file_path, 'r')
        xml = re.sub(r'<application>.*</application>', '<application>' + app_id + '</application>', f.read())
        f.close()

        f = open(file_path, 'w')
        f.write(xml)
        f.close()

        subprocess.call(["appscale", "deploy", path])
    except IOError:
        print('cannot open', file_path)


if __name__ == "__main__":
    main(sys.argv[1:])