#!/usr/bin/env python3

# from: https://github.com/kneufeld/minio-put/blob/master/minio-put.py
import os
import sys
import glob
import argparse
import datetime
import urllib.request
import yaml
import requests

def upload(key, secret, host, bucket, filename, dest_name=None):
    """
    call this function when using this file as a library
    date header must be relatively in sync with server or you
    get forbidden error
    returns True on success
    raises on error
    """
    base_name = os.path.basename(filename)

    try:
        hostname, port = host.split(':')
    except ValueError:
        hostname = host

    if dest_name:
        resource     = f"/{bucket}/{dest_name}"
    else:
        resource     = f"/{bucket}/{base_name}"
    content_type = "application/octet-stream"
    date         = tznow().strftime('%a, %d %b %Y %X %z')
    _signature   = f"PUT\n\n{content_type}\n{date}\n{resource}"
    signature    = sig_hash(secret, _signature)

    url = f"http://{host}{resource}"
    headers = {
        'Host':          hostname,
        'Date':          date,                      # eg. Sat, 03 Mar 2018 10:11:16 -0700
        'Content-Type':  content_type,
        'Authorization': f"AWS {key}:{signature}",
    }
    #import pprint
    #pprint.pprint(headers)
    #pprint.pprint(url)
    filedata =  open(filename, 'rb')
    #req = urllib.request.Request(url, data=filedata, method='PUT', headers=headers)
    #resp = urllib.request.urlopen(req)
    req = requests.put(url, data=filedata, headers=headers)
    print(req)
    return req or True

# based on: https://gist.github.com/heskyji/5167567b64cb92a910a3
def sig_hash(secret, sig):
    import hashlib
    import hmac
    import base64

    # signature=`echo -en ${sig} | openssl sha1 -hmac ${secret} -binary | base64`

    secret     = bytes(secret, 'UTF-8')
    sig        = bytes(sig, 'UTF-8')

    digester   = hmac.new(secret, sig, hashlib.sha1)
    signature1 = digester.digest()
    signature2 = base64.standard_b64encode(signature1)
    #signature2 = base64.urlsafe_b64encode(signature1)

    return str(signature2, 'UTF-8')


def tznow():
    def utc_to_local(utc_dt):
        return utc_dt.replace(tzinfo=datetime.timezone.utc).astimezone(tz=None)

    ts = datetime.datetime.utcnow()
    return utc_to_local(ts)


if __name__ == "__main__":
    key = os.environ["s3_key"]
    secret = os.environ["s3_secret"]
    host = "mirror.infra.pkm:9000"
    bucket = "nmreval-appimage"
    if "GO_PIPELINE_LABEL" in os.environ:
        appimage_version = os.environ["GO_PIPELINE_LABEL"]
    else:
        appimage_version = yaml.safe_load(open("AppImageBuilder.yml"))["AppDir"]["app_info"]["version"]
    print(f"Uploading NMReval-{appimage_version}-x86_64.AppImage and .zsync")
    upload(key, secret, host, bucket,
            f"NMReval-{appimage_version}-x86_64.AppImage")
    upload(key, secret, host, bucket,
            f"NMReval-{appimage_version}-x86_64.AppImage",
            f"NMReval-latest-x86_64.AppImage")
    upload(key, secret, host, bucket,
            f"NMReval-{appimage_version}-x86_64.AppImage.zsync")
    upload(key, secret, host, bucket,
            f"NMReval-{appimage_version}-x86_64.AppImage.zsync",
            f"NMReval-latest-x86_64.AppImage.zsync")

