#!/usr/bin/env python3

# Flexlay - A Generic 2D Game Editor
# Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.


import sys
import argparse
import logging

import sys
from flexlay.config import Config
from supertux.level import Level


def main():
    has_error = False
    parser = argparse.ArgumentParser(description="SuperTux Level Tool")
    parser.add_argument("LEVELFILE", action="store", type=str, nargs="+",
                        help=".stl file to load")
    parser.add_argument("-d", "--datadir", metavar="DIR", action="store", type=str,
                        help="SuperTux data directory directory")
    parser.add_argument("-o", "--output", metavar="FILE", action="store", type=str,
                        help="Load and resave the level to FILE")
    args = parser.parse_args()

    config = Config.create("supertux-editor")
    if args.datadir is not None:
        config.datadir = args.datadir
    elif not config.datadir:
        raise RuntimeError("datadir missing, use --datadir DIR")

    for levelfile in args.LEVELFILE:
        logging.info("Loading {}".format(levelfile))
        try:
            level = Level.from_file(levelfile)
            if args.output:
                level.save_v2(args.output)
        except Exception as e:
            logging.exception("ERROR")
            has_error = True

    return 1 if has_error else 0

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


# EOF #
