#!/usr/bin/python3
# -*- coding: utf-8 -*-

from distutils.version import LooseVersion
import argparse
import sys

from univention.appcenter.app_cache import Apps
from univention.appcenter.actions import get_action

class CarelessArgumentParser(argparse.ArgumentParser):
    def error(self, message):
        pass

parser = CarelessArgumentParser(description='Preinst of oxseforucs')
parser.add_argument('--locale')
parser.add_argument('--error-file')
parser.add_argument('--old-version')

args = parser.parse_args()

if args.old_version:
    app = Apps().find('ox-connector')
    domain = get_action('domain')
    result = domain.to_dict([app])
    app = result[0]
    if not app or not app['is_installed_anywhere']:
        if args.error_file:
            with open(args.error_file, 'ab') as fd:
                if args.locale == 'de':
                    fd.write('Die App "OX Connector" ist nicht installiert. Bitte installieren Sie diese auf einem beliebigen UCS-System. Am einfachsten ist es, Sie nehmen das System, auf dem auch die "OX App Suite" läuft. Aktualisieren Sie danach diese App.')
                else:
                    fd.write('The App "OX Connector" is not installed. Please install it on any UCS system, preferably on the one that also runs "OX App Suite". Afterwards, upgrade this App.')
        sys.exit(1)
