#!/usr/bin/env python3
# -*- coding: utf-8-unix -*-
"""The pot file generator for data files of Endless Sky."""
# Copyright © 2018, 2019, 2021 OOTA, Masato
#
# This is published by CC0 1.0.
# For more information, see CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# at https://creativecommons.org/publicdomain/zero/1.0/deed).

import argparse
import fileinput
import sys
import time
import gettext_pot_db
import endless_sky_po_parser

arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("-o", help="output filename", type=argparse.FileType('w'), default=sys.stdout)
arg_parser.add_argument('data_filename', type=str, nargs='*', help="input data filename", default=('-',))
args = arg_parser.parse_args()

db = gettext_pot_db.pot_db()
parser = endless_sky_po_parser.parser()

def append_db(msg, context, comment, filename, linenumber):
    global db
    db.append(msg, context, comment, filename, linenumber)

parser.set_callback(append_db)

# Main loop
with fileinput.input(files=args.data_filename) as f:
    for line in f:
        if f.isfirstline():
            parser.flush()
        parser.parse_line(line, f.filename(), f.filelineno())
parser.flush()

# output
now = time.strftime('%F %H:%M%z', time.localtime())
args.o.write(r'''# SOME DESCRIPTIVE TITLE.
# Copyright © YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: {0}\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
'''.format(now))
db.write(args.o.write)
