#!/bin/env python3
# curl based lfs link fetcher, for systems that don't have git-lfs easily available (haiku, for example)

from subprocess import getoutput
from urllib.parse import urlsplit
from getpass import getpass
from os import walk
import random, string, json, mimetypes

def randomword(length):
   letters = string.ascii_lowercase
   return ''.join(random.choice(letters) for i in range(length))


if __name__ == "__main__":
    origin = getoutput(["git remote get-url origin"])
    print("Origin:", origin)
    parts = urlsplit(origin)
    location = parts.netloc

    name = input("Username: ")
    passw = getpass("Password: ")

    query_data = '{"name": "%s"}' % (randomword(6),)
    query = f"curl -s -H 'Content-Type: application/json' -d '{query_data}' -u '{name}:{passw}' https://{location}/api/v1/users/{name}/tokens"
    token_response = getoutput(query)
    token = json.loads(token_response)["sha1"]

    for root, dirs, files in walk("."):
        for file in files:
            path = root + '/' + file
            with open(path, 'r') as f:
                file_result = getoutput(f"file -b {path}")
                if file_result != "ASCII text":
                    continue
                header = f.readline()
                if header != "version https://git-lfs.github.com/spec/v1\n":
                    continue
                sha = f.readline().split(':')[1].strip()
                size = f.readline().split(' ')[1].strip()

                query_data = '{"operation": "download", "transfer": ["basic"], "objects": [{"oid": "%s", "size": %s}]}' % (sha, size)
                query = f"curl -s -X POST -H 'Authorization: token {token}' -H 'Accept: application/vnd.git-lfs+json' -H 'Content-type: application/json' -d '{query_data}' {origin}.git/info/lfs/objects/batch"
                response = getoutput(query)

                href = json.loads(response)["objects"][0]["actions"]["download"]["href"]
                query = f"curl -s -X GET -H 'Authorization: token {token}' {href} > {path}"
                response = getoutput(query)

                print("Processed", path)