#!/usr/bin/env python3
#
# Copyright (C) 2012-2017 Chris Cummins.
#
# This file is part of emu.
#
# Emu 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.
#
# Emu 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 emu.  If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import print_function

import os
import sys
from sys import exit

from emu import *


def main(argv, argc):
    parser = Parser(source_dir_arg=False)
    parser.add_option("-S", "--source-dir", action="store", type="string",
                      dest="source_dir", default=os.getcwd())
    parser.add_option("-t", "--template-dir", action="store", type="string",
                      dest="template_dir", default=Meta.source_templates)
    parser.add_option("-f", "--force", action="store_true", dest="force",
                      default=False)
    (options, args) = parser.parse_args()

    # Fail if no read/write permissions
    Util.readable(options.source_dir, error=True)
    Util.writable(options.source_dir, error=True)

    # Perform initialisation
    try:
        Source.create(options.source_dir, options.template_dir,
                      force=options.force)
        return 0
    except SourceCreateError as e:
        print(e)
        return 1


if __name__ == "__main__":
    argv = sys.argv[1:]
    ret = main(argv, len(argv))
    exit(ret)
