def main():
    """
    The main() function handles command line arguments and maneuvers the cover
    image generation.
    """
    # Helper function.
    def _draw_and_save(title, subtitle, author, filename):
        """
        Draw a cover and write it to a file. Note that only PNG is supported.
        """
        cover_image = draw(title, subtitle, author)
        if filename == "-":
            assert not "Implement."
        else:
            _, ext = os.path.splitext(os.path.basename(filename))
            if ext.upper() == ".PNG":
                try:
                    with open(filename, "wb") as f:
                        cover_image.save(f)
                except FileNotFoundError:
                    print("Error opening target file " + filename)
                    return 1
            else:
                print("Unsupported image file format '" + ext + "', use PNG")
                return 1
        return 0

    # Set up and parse the command line arguments passed to the program.
    usage = "Python implementation of the 10PRINT Cover image generator."
    parser = argparse.ArgumentParser(usage=usage)

    parser.add_argument("-t", "--title", dest="title", help="Book title")
    parser.add_argument("-s", "--subtitle", dest="subtitle", help="Book subtitle", default="")
    parser.add_argument("-a", "--author", dest="author", help="Author(s) of the book")
    parser.add_argument("-o", "--cover", dest="outfile", help="Filename of the cover image in PNG format")
    parser.add_argument("-j", "--json-covers", dest="json_covers", help="JSON file containing cover information")
    args = parser.parse_args()

    # A JSON file is given as command line parameter; ignore the other ones.
    # Read the file line by line and use the given information to generate the
    # book covers. The file contains lines of JSON maps of the format
    #
    #   {"authors": "..", "identifier": "..", "subtitle": null, "title": "..",
    #    "identifier_type": "Gutenberg ID", "filename": ".."}
    if args.json_covers:
        try:
            with open(args.json_covers, "r") as f:
                for line in f:
                    data = json.loads(line)
                    print("Generating cover for " + data["identifier"])
                    status = _draw_and_save(
                        data["title"],
                        data["subtitle"],
                        data["authors"],
                        data["filename"]
                    )
                    if status:
                        print("Error generating book cover image, skipping")
            return 0
        except ValueError:
            print("Error reading from JSON file, exiting")
        except FileNotFoundError:
            print("JSON cover file does not exist: " + args.json_covers)

    # Generate only a single cover based on the given command line arguments.
    else:
        if not args.title or not args.author:
            print("Missing --title or --author argument, exiting")
        elif not args.outfile:
            print("No outfile specified, exiting")
        else:
            return _draw_and_save(args.title, args.subtitle, args.author, args.outfile)
    return 1