#!/usr/bin/python3
###############################################################################
#                                                                             #
# Bricklayer - An Installer for IPFire                                        #
# Copyright (C) 2021 IPFire Development Team                                  #
#                                                                             #
# 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 2              #
# 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 argparse
import logging
import os
import sys

import bricklayer

from bricklayer.i18n import _

# Setup logging
log = logging.getLogger("bricklayer")

class Cli(object):
	"""
		This class is called from the command line interface and parses any
		relevant switches and configures bricklayer.
	"""
	def parse_cli(self):
		parser = argparse.ArgumentParser(
			description=_("IPFire Installation Tool CLI"),
		)
		parser.add_argument("--pakfire-conf", metavar=_("FILE"),
			help=_("Pakfire Configuration file used for the installation process"))
		parser.add_argument("--first-install", action="store_true",
			help=_("Runs the \"first install\" setup process"))
		parser.add_argument("--arch", nargs="?", default=self.native_arch,
			help=_("Select the target architecture"))
		parser.add_argument("--debug", action="store_true",
			help=_("Enable debugging mode"))
		parser.add_argument("--unattended", action="store_true",
			help=_("Enable unattended mode"))
		parser.add_argument("--disk", nargs="*", dest="disks", default=[],
			help=_("A disk image file or device which will be used"))

		# Parse arguments
		return parser.parse_args()

	def __call__(self):
		# Parse command line arguments
		args = self.parse_cli()

		# Run bricklayer
		r = self.run_bricklayer(args)

		if r:
			sys.exit(r)

	def run_bricklayer(self, args):
		# Setup bricklayer
		bl = bricklayer.Bricklayer(**vars(args))

		# Run it
		return bl()

	@property
	def native_arch(self):
		"""
			Return the native system architecture (i.e. uname -m)
		"""
		sysname, nodename, release, version, arch = os.uname()

		return arch

# Main
c = Cli()
c()
