#!/usr/bin/env python3
import os
import subprocess
from os.path import exists

import binary_updaters

branch_name = subprocess.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
                             stdout=subprocess.PIPE) \
    .stdout \
    .decode('utf-8') \
    .strip()

print(branch_name)

print(f"Downloading shardok for branch {branch_name}")

shardok_artifact_name = 'shardok.tar.gz'
shardok_base_dir = '/tmp/shardok/binaries'

if not exists(shardok_base_dir):
    os.makedirs(shardok_base_dir)

binary_updaters.update(base_dir=shardok_base_dir,
                       artifact_name=shardok_artifact_name,
                       branch_name=branch_name)

exe_path = os.path.join(binary_updaters.binary_dir(shardok_base_dir, branch_name), 'shardok', 'shardok-server')

os.symlink(src=exe_path, dst='/tmp/shardok/binaries/current_')
os.rename(src='/tmp/shardok/binaries/current_', dst='/tmp/shardok/binaries/current')
