import os
import requests
import wget
#from perceval.backends.core.git import Git
from git import Repo
import shutil
import pexpect
import subprocess

def debian_vcs_query(debian_vcs_link):
    upstream_repo_link = ""
    project_name = debian_vcs_link.split("/")[-1]
    output_directory = "/data/users/mgaughan/tmp1/" + project_name
    print(output_directory)
    #url = debian_vcs_link + '/-/blob/master/debian/upstream/metadata'
    # the below try is the password handling
    try:
        #Repo.clone_from(debian_vcs_link, output_directory, env={"Username for 'https://salsa.debian.org": "", "Password for 'https://salsa.debian.org":""})
        command = "git clone " + debian_vcs_link + " " + output_directory 
        p = pexpect.spawn(command)
        p.expect("Username for 'https://salsa.debian.org':")
        p.sendline ("")
        p.expect ("Password for 'https://salsa.debian.org':")
        p.sendline ("")
        p.expect(pexpect.EOF, timeout=None)
    #p.expect([pexpect.TIMEOUT, 'pattern', pexpect.EOF])
        p.close()
    except:
        print("handling misc pexpect issues")
        #return upstream_repo_link
    try:
        upstream_metadata = open(output_directory + "/debian/upstream/metadata", "r").read().split("\n")
        upstream_repo_array = upstream_metadata[3].split(":")[-2:]
        upstream_repo_link = ":".join(upstream_repo_array)
        print(upstream_repo_link)
    except:
        print("no file!")
        upstream_repo_link = ""
    #filename = wget.download(url, out=output_directory)
    #print(type(filename))
    shutil.rmtree(output_directory, ignore_errors=True)
    print('success')
    return upstream_repo_link


def debian_query(package_name):
    headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
    first_api_url = "https://sources.debian.org/api/src/" + package_name
    try:
        first_response = requests.get(url = first_api_url, headers=headers)
        first_response_dict = first_response.json()
    except:
        print('error with the first debian request')
        first_response_dict = {}
        return ("")
    if first_response_dict == {'error': 404}:
        print('not found in debian system')
        return ("")
    #print(first_response_dict)
    most_recent_package_version = first_response_dict['versions'][0]['version']
    #print(first_response_dict['versions'][0]['version'])
    second_api_url = "https://sources.debian.org/api/info/package/" + package_name + "/" + most_recent_package_version + "/"
    try:
        second_response = requests.get(url = second_api_url, headers=headers)
        second_response_dict = second_response.json()
    except:
        print('error with the second debian request')
        second_response_dict = {}
        return ("")
    if 'vcs_browser' not in second_response_dict['pkg_infos'].keys():
        print('no vcs link')
        return ("")
    print(second_response_dict)
    print(second_response_dict['pkg_infos']['vcs_browser'])
    return second_response_dict['pkg_infos']['vcs_browser']


if __name__ == "__main__":
    #main()
    #debian_query("broccoli-ruby")
    debian_vcs_query(debian_query("brotli"))