#!/usr/bin/python3

# SPDX-License-Identifier: GPL-2.0-only
# Copyright (C) 2022-present Team LibreELEC (https://libreelec.tv)

import os
import sys

sys.path.append('/storage/.kodi/addons/browser.chrome/resources')
import unix_ar
import tarfile


if len(sys.argv) != 3:
    print("Parameter error", file=sys.stderr)
    sys.exit(1)

if os.path.isfile(sys.argv[1]):
    try:
        ar = unix_ar.open(sys.argv[1])
    except ValueError:
        print(f"Unable to open archive: {sys.argv[1]}", file=sys.stderr)
        sys.exit(1)
    else:
        tarball = ar.open('data.tar.xz/')
        tar = tarfile.open(fileobj=tarball)
        tar.extractall(path=sys.argv[2])
else:
    print(f"File not found: {sys.argv[1]}", file=sys.stderr)
    sys.exit(1)
