#!/usr/bin/env python3

import os
import time
import shutil
import subprocess
import sys

import ogGlobals
from SystemLib import ogEcho, ogRaiseError

if __name__ == "__main__":
    prog = sys.argv[0]
    if len (sys.argv) != 4:
        print (f'Usage: {prog} <disk> <partition> <dest_file>')
        sys.exit (1)

    disk, par, dest_file = sys.argv[1:]

    TIME1 = time.time()

# Limpia los ficheros temporales usados como log de seguimiento para httpdlog
    open (ogGlobals.OGLOGSESSION,          'w').close()
    open (ogGlobals.OGLOGCOMMAND,          'w').close()
    open (f"{ogGlobals.OGLOGCOMMAND}.tmp", 'w').close()

# Registro de inicio de ejecución
    ogEcho (['log', 'session'], None, f'{ogGlobals.lang.MSG_INTERFACE_START} {prog} {disk} {par} {dest_file}')

    listsi_out = subprocess.run ([f'{ogGlobals.OGSCRIPTS}/listSoftwareInfo.py', disk, par], capture_output=True, text=True).stdout
    if listsi_out:
        file = listsi_out.splitlines()[0]
    else:
        ogRaiseError ([], ogGlobals.OG_ERR_GENERIC, 'listSoftwareInfo.py failed')
        sys.exit (1)

    shutil.copy (file, dest_file)

    TIME = time.time() - TIME1
    ogEcho(['log', 'session'], None, f'   [ ]      {ogGlobals.lang.MSG_SCRIPTS_TIME_PARTIAL} : {int(TIME // 60)}m {int(TIME % 60)}s')
