#!/usr/bin/env python
# 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: Book History Online
SID: 198
Ticket: #18021
Origin: Cloud

"""


import os
import sys
import re

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


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

SID = "198"

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)

# 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:
    config = Config.instance()
    inputfile = config.get(SID, "input")

inputfile = open(inputfile, "rb")
reader = pymarc.MARCReader(inputfile, force_utf8=True)


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

for record in reader:

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

    # Format mapping
    try:
        f773 = record["773"]
    except:
        f773 = ""

    if f773:
        format = "Article"
    else:
        format = "Book"

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

    # Access type
    f007 = formats[format]["p007"]
    record.add("007", data=f007)

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

    # Subject facet
    record.add("082", a="002")

    # Put start page in the correct field
    if f773:
        f773t = record["773"]["t"]
        f7737 = record["773"]["7"]

        try:
            f773q = record["773"]["q"]
        except:
            f773q = ""

        try:
            f773d = record["773"]["d"]
        except:
            f773d = ""

        if "<" in f773t and not f773q:
            f773t = re.sub("<pp\.?\s?", "<", f773t)
            f773 = f773t.split("<")
            f773t = f773[0]
            pages = f773[1]
            f773g = "Seite: " + pages
            f773q = "<" + pages
            record.remove_fields("773")
            record.add("773", t=f773t, g=f773g, q=f773q, _7=f7737, d=f773d)

    # Collection
    collections = ["a", f001, "b", "198", "c", "sid-198-col-bho"]
    record.add("980", subfields=collections)

    # Removes surplus delimiters (ISBD etc.), refs #16965
    record = remove_delimiter(record)

    # Removes empty subfields
    marc_clean_record(record)

    if outputformat == "xml":
        outputfile.write(record)
    else:
        outputfile.write(record.as_marc())

inputfile.close()
outputfile.close()
