#!/usr/bin/env python3
# coding: utf-8
#
# Copyright 2020 by Leipzig University Library, http://ub.uni-leipzig.de
#                   The Finc Authors, http://finc.info
#                   Robert Schenk, <robert.schenk@uni-leipzig.de>
#
# This file is part of some open source application.
#
# Some open source application is free software: you can redistribute
# it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version.
#
# Some open source application is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
#
# @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>

"""

Source: Buchhandschriften
SID: 159
Ticket: #13316
Origin: Speicherwolke

"""


import os
import sys

import marcx
import pymarc
from siskin.mappings import formats
from siskin.configuration import Config
from siskin.utils import marc_clean_record
from siskin.arguments import FincArgumentParser


##################################################################################
# 1. Parse arguments and prepare outputfile
##################################################################################

SID = "159"

fip = FincArgumentParser()

# Get arguments
inputfile = fip.args.inputfile
outputformat = fip.args.outputformat

# Generates string for outputfilename, example: 196-output-20200701.fincmarc.mrc
outputfilename = fip.outputfilename(SID)

# Generates path for inputfile
path = fip.sid_path(SID)

# Removes n old outputfiles as specified in output-hist-size
fip.remove_old_outputfiles(SID)

# Set output format for MARC record
if outputformat == "xml":
    outputfile = pymarc.XMLWriter(open(outputfilename, "wb"))
else:
    outputfile = open(outputfilename, "wb")


##################################################################################
# 2. Get input data
##################################################################################

if not inputfile:

    tmpfile = path + "/159_input.tmp"
    inputfile = path + "/159_input.mrc"
    os.system("mkdir /tmp/159")
    os.system('curl "http://172.18.85.86:12105/repo/Buchhandschriften/fincimport" > /tmp/159/urls')
    os.system("wget -i /tmp/159/urls -P /tmp/159")
    os.system("""sed -i -e 's/ind1=""/ind1=" "/g' -e 's/ind2=""/ind2=" "/g' /tmp/159/*""")
    os.system("""yaz-marcdump -i marcxml -o marc /tmp/159/xml* > %s""" % tmpfile)
    os.system("""yaz-marcdump -i marc -o marcxml %s > %s""" % (tmpfile, inputfile))

inputfile = open(inputfile, "rb")
reader = pymarc.parse_xml_to_array(inputfile)


##################################################################################
# 3. Process data
##################################################################################

for record in reader:

    record = marcx.Record.from_record(record)
    record.force_utf8 = True
    record.strict = False

    # Format
    format = "Manuscript"

    # Leader
    leader = formats[format]["Leader"]
    record.leader = leader

    # Identifier
    f001 = record["001"].data
    f001 = f001.replace("-", "")
    f001 = f001.replace(",T", "T")
    record.remove_fields("001")
    record.add("001", data="159-" + f001)

    # Remove doublet language
    record.remove_fields("546")

    # Additional creators
    unique_persons = []
    fields = record.get_fields("700")
    record.remove_fields("700")
    if fields:
        for field in fields:
            person = field.get_subfields("a")[0]
            gnd = field.get_subfields("1")
            if person and gnd:
                gnd = gnd[0]
                if gnd not in unique_persons:
                    record.add("700", a=person, _1=gnd)
                    unique_persons.append(person)
                    unique_persons.append(gnd)
            elif person:
                if person not in unique_persons:
                    record.add("700", a=person)
                    unique_persons.append(person)

    # Collectionen
    try:
        f912a = record["912"]["a"]
    except:
        continue

    if f912a == "abendländische mittelalterliche Handschriften":
        f912a = "Abendländische mittelalterliche Handschriften"

    if f912a == "abendländische neuzeitliche Handschriften":
        f912a = "Abendländische neuzeitliche Handschriften"

    if f912a == "griechische Handschriften":
        f912a = "Griechische Handschriften"

    if f912a == "hebräische Handschriften":
        f912a = "Hebräische Handschriften"

    if f912a == "Abendländische mittelalterliche Handschriften":
        technicalCollectionID = "sid-159-col-buchhsabendlandmittelalter"
    elif f912a == "Abendländische neuzeitliche Handschriften":
        technicalCollectionID = "sid-159-col-buchhsabendlandneuzeit"
    elif f912a == "Fragmente":
        technicalCollectionID = "sid-159-col-buchhsfragmente"
    elif f912a == "Griechische Handschriften":
        technicalCollectionID = "sid-159-col-buchhsgriechisch"
    elif f912a == "Neuzeitliche Handschriften":
        technicalCollectionID = "id-159-col-buchhsabendlandneuzeit"
    elif f912a == "Hebräische Handschriften":
        technicalCollectionID = "sid-159-col-buchhshebraeisch"
    elif f912a == "Musikhandschriften":
        technicalCollectionID = "sid-159-col-buchhsmusik"
    elif f912a == "Urkunden":
        technicalCollectionID = "sid-159-col-buchhsurkunden"
    elif f912a == "Islamische Handschriften":
        technicalCollectionID = "sid-159-col-buchhsislamisch"
    else:
        continue

    # Digital collection
    record.remove_fields("912")
    record.add("912", a=technicalCollectionID)

    # Collection and sealing
    record.add("980", a=f001, b="159", c=technicalCollectionID)

    # Remove empty subfields
    marc_clean_record(record)

    # Write record to file
    if outputformat == "xml":
        outputfile.write(record)
    else:
        outputfile.write(record.as_marc())

inputfile.close()
outputfile.close()
