#!/usr/bin/python3

import os
import shutil
import subprocess
import sys

OPENLINUX_MIRROR       = 'ftp://ftp.nvg.org/pub/mirrors/metalab.unc.edu/distributions/caldera'
OPENDOS702RPM_URL = OPENLINUX_MIRROR + '/updates/OpenLinux/1.3/current/SRPMS/opendos-hdimage-7.02-4.src.rpm'
OPENDOS702RPM_SHA256SUM = 'bb5cf740d7dcae40665a235a23d8a59136e09bd23ed89bc29e270d5c61c685cb'

SVARDOS_URL = 'http://svardos.org/download/20220816/svardos-20220816-dosemu.zip'

THIRD_PARTY_DOS_CHOICES = [
    {'short_name': 'opendos702', 'script_name': 'opendos702', 'full_name': 'Caldera OpenDOS 7.02 (1997)'  , 'url': OPENDOS702RPM_URL, 'sha256sums': [OPENDOS702RPM_SHA256SUM]},
    {'short_name': 'svardos816', 'script_name': 'svardos', 'full_name': 'SvarDOS (rolling release)'  , 'url': SVARDOS_URL}
    ]

CACHE_DIR = os.path.join(os.environ['HOME'], '.cache', 'dosemu')

SCRIPT_ROOT = os.path.dirname(os.path.realpath(__file__))

for dos_choice in THIRD_PARTY_DOS_CHOICES:
    print('Installing ' +  dos_choice.get('full_name'))
    download_dir = os.path.join(CACHE_DIR, dos_choice.get('short_name'))
    download_command = [os.path.join(SCRIPT_ROOT, 'dosemu-dlimg')]
    download_command.append('-d')
    download_command.append(download_dir)
    download_command.append(dos_choice.get('url'))

    if dos_choice.get('sha256sums') is not None:
        download_command.append('-s')
        for sha256sum in dos_choice.get('sha256sums'):
            download_command.append(sha256sum)

    destination_dir = os.path.join(os.environ['HOME'], '.dosemu', 'install', dos_choice.get('short_name'), 'drive_c')

    if subprocess.run(download_command).returncode == 0:
        subprocess.run([os.path.join(SCRIPT_ROOT, 'dosemu-install' + dos_choice.get('script_name')), download_dir, destination_dir])
