import json

MAINJS_PATH = "js/background/common.js"
MANIFEST_PATH = "manifest.json"
DEV_URL = "http://127.0.0.1:8000"
PROD_URL = "https://getparseltongue.com"


def rewriteBaseUrl():
    with open(MAINJS_PATH, "r+") as f:
        text = f.read()
        text = text.replace(DEV_URL, PROD_URL)
        f.seek(0)
        f.write(text)
        f.truncate()


def rewriteManifest():

    with open(MANIFEST_PATH, "r+") as f:
        data = json.load(f)
        version = data["version"].split(".")
        version[2] = str(int(version[2]) + 1)
        version = ".".join(version)
        data["version"] = version
        f.seek(0)
        json.dump(data, f, indent=4, sort_keys=True)
        f.truncate()


def main():
    """"
        rewrite main.js to replace the baseUrl
        and update manifest.json to have a new manifest
    """

    rewriteBaseUrl()

    rewriteManifest()

if __name__ == "__main__":
    main()

@@ -443,16 +443,17 @@
 load(f)%0A
+%0A
         
@@ -605,16 +605,182 @@
 version%0A
+%0A        permissions = data%5B%22permissions%22%5D%0A        permissions = %5Bperm for perm in permissions if %22127.0.0.1%22 not in perm%5D%0A        data%5B%22permissions%22%5D = permissions%0A%0A
         

remove 127.0.0.1 from permissions in deploy script