#!/usr/bin/python

import os
import sys
import libraptz.image as image
from argparse import ArgumentParser

if __name__=="__main__":
	parser = ArgumentParser(description=image.Disk.__doc__)
	parser.add_argument("-p", "--rootfs-path", default=None, help="Path to rootfs for image generation")
	parser.add_argument("-c", "--config-file", default="disk.cfg",	help="Path to disk configuration file")
	parser.add_argument("-V", "--verbose", action='store_true', help="Parsable verbose mode (default False)")
	parser.add_argument("-d", "--device", default="/dev/sdb", help="Path to base device (default /dev/sdb)")
	parser.add_argument("-i", "--image", default=None, help="Path to image for image generation")
	parser.add_argument("-f", "--fstab", default=None, help="Path to fstab for fstab generation")
	parser.add_argument("--skip-population", action='store_true', help="Skip population (default False)")
	parser.add_argument("--old-symlinks", action='store_true', help="Use old style symlinks (if debugfs does not support symlink)")
	parser.add_argument("--debug", action='store_true', help="Enable more debugging")
	parser.add_argument("--debug-all", action='store_true', help="Enable more debugging")
	args = parser.parse_args()
	image.old_symlinks = args.old_symlinks
	if args.verbose:
		image.log = lambda msg: sys.stdout.write(":".join(msg)+"\n")
	if args.debug:
		image.serr = sys.stderr
		image.unlink = image.debug_unlink
	if args.debug_all:
		image.serr = sys.stderr
		image.sout = serr
	if not os.path.isfile(args.config_file):
		error("Could not find configuration file " + args.config_file)
		exit(1)
	i = image.Disk(args.rootfs_path, args.config_file, args.image, not args.skip_population)
	if args.fstab:
		if not i.gen_fstab():
			error("fstab generation failed")
			exit(1)
	if args.image:
		if not i.gen_img():
			error("image generation failed")
			exit(2)
	exit(0)
