#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#***************************************************************************
#* SPDX-FileCopyrightText: 2024 S. MANKOWSKI stephane@mankowski.fr
#* SPDX-FileCopyrightText: 2024 G. DE BURE support@mankowski.fr
#* SPDX-License-Identifier: GPL-3.0-or-later
#***************************************************************************

import argparse
import os
import shutil
import subprocess
import sys

__VERSION__ = "1.0.0"


def main():
    parser = argparse.ArgumentParser(prog='skrooge-woob', description='Skrooge woob Bridge')

    # Global arguments
    parser.add_argument('--version', '-v', action='version', version='%(prog)s ' + __VERSION__, help='Shows version information.')
    parser.add_argument('--output', required=True,  help='The folder to store the csv files.')
    parser.add_argument('--filter', required=True,  help='The account filter. Something like account1|account2|account3')
    parser.add_argument('--woob_option', help='The additional woob options.')


    args = parser.parse_args()
    if args.woob_option is None:
        args.woob_option = ''

    command = ['woob', 'bank', 'ls', args.woob_option, '-q', '-f', 'csv', '-s', 'id,label,balance', '-O', args.output]

    if args.filter is not None:
        # Add filter
        command.append('--condition')
        command.append('id|' + ' OR id|'.join(args.filter.split('|')))

    if os.path.exists('/.flatpak-info') and shutil.which('flatpak-spawn'):
        command = ['flatpak-spawn', '--host'] + command

    try:
        process_result = subprocess.run(command, check=False)
        return process_result.returncode
    except FileNotFoundError as e:
        print(str(e), file=sys.stderr)
        return 127

if __name__ == "__main__":
    sys.exit(main())
