#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This file is part of GetTor, a Tor Browser distribution system.
#
# :authors: hiro <hiro@torproject.org>
#           see also AUTHORS file
#
# :license: This is Free Software. See LICENSE for license information.

import os
import sys
import json
import sqlite3
import argparse
from urllib import request

TOR_BROWSER_DOWNLOADS = "https://aus1.torproject.org/torbrowser/update_3/release/downloads.json"


def print_header():
    header = r"""
                             __     __
                            /\ \__ /\ \__
                  __      __\ \ ,_\\\ \ ,_\  ____   _  __
                /'_ `\  /'__`\ \ \/ \ \ \/  / __ `\/\`'__\
               /\ \L\ \/\  __/\ \ \_ \ \ \ /\ \L\  \ \ \/
               \ \____ \ \____\\ \__\ \ \__\ \_____/\ \_\
                \/___L\ \/____/ \/__/  \/__/\/____/  \/_/
                 /\_____/
                 \_/___/

    """
    print("")
    print("@"*100)
    print("@"*100)
    print(header)
    print("@"*100)
    print("")


def print_footer():
    print("")
    print("@"*100)
    print("@"*100)
    print("")


def main():
    parser = argparse.ArgumentParser(
        description="Tool to create the gettor SQLite database."
    )

    parser.add_argument(
        "-f", "--filename", default="gettor.db", metavar="gettor.db",
        help="Database filename."
    )

    args = parser.parse_args()
    abs_filename = os.path.abspath(args.filename)

    webFile = request.urlopen(TOR_BROWSER_DOWNLOADS)
    content = webFile.read()
    jsonObj = json.loads(content.decode('utf-8'))
    version = jsonObj.get("version", None)
    if version is None:
        print("Could not fetch version from {}.".format(TOR_BROWSER_DOWNLOADS))
        sys.exit(1)

    providers = {
        "gitlab": "https://gitlab.com/thetorproject/gettorbrowser/raw/releases/",
        "github": "https://github.com/TheTorProject/gettorbrowser/raw/torbrowser-releases/"
    }

    prefixes = {
        "osx": "TorBrowser-",
        "windows": "torbrowser-install-",
        "linux": "tor-browser-linux64-"
    }

    versions = {"windows": version, 'linux': version, 'osx': version}

    suffixes = {
        "osx": "-osx64_en-US.dmg",
        "windows": "_en-US.exe",
        "linux": "_en-US.tar.xz"
    }

    keys = ['osx', 'windows', 'linux']

    languages = [
        'en-US',
        'es-ES',
        'pt-BR',
        'ar',
        'ca',
        'cs',
        'da',
        'de',
        'el',
        'es-AR',
        'fa',
        'fr',
        'ga-IE',
        'he',
        'hu',
        'id',
        'is',
        'it',
        'ja',
        'ka',
        'ko',
        'nb-NO',
        'nl',
        'pl',
        'pt-BR',
        'ru',
        'sv-SE',
        'tr',
        'vi',
        'zh-CN',
        'zh-TW'
    ]

    releases = {k: "".join(dic.get(k, version) for dic in (prefixes, versions, suffixes))  for k in keys}

    conn = sqlite3.connect(abs_filename)
    with conn:
        c = conn.cursor()
        """
        Here we drop previous links TABLE but probably it would be better to
        just update old links to INACTIVE
        """
        c.execute("DROP TABLE IF EXISTS links")
        c.execute(
            "CREATE TABLE links(link TEXT, platform TEXT, language TEXT, arch TEXT,"
            " version TEXT, provider TEXT, status TEXT)"
        )
        for k in keys:
            for p in providers:
                for l in languages:
                    release_link = releases.get(k).replace("en-US", l)
                    c.execute(
                        "INSERT INTO links(link, platform, language, arch, version, provider, status)"
                        "VALUES ('%s', '%s', '%s', '64', '%s', '%s', 'ACTIVE')" % (providers.get(p) + release_link, k, l, version, p))


if __name__ == "__main__":
    print_header()
    main()
    print_footer()
