<?php

	require_once 'cli.php';
	
	/**
	 * Prints usage
	 */
	function usage() {
		$usage = new Usage(
			'Productizer 1.0',
			basename(__FIlE__),
			'Creates the equivalent of C#.NET\'s "AssemblyInfo.cs"',
			'Tommy Montgomery',
			'2009'
		);
		
		global $switches;
		$usage->setSwitches($switches);
		
		echo $usage;
	}
	
	global $switches;
	$switches = new CliSwitchCollection();
	$switches->addSwitch(new CliSwitch('name',        'n', true,  'name',           'Product name'))
			 ->addSwitch(new CliSwitch('version',     'v', true,  'version_number', 'Product version'))
			 ->addSwitch(new CliSwitch('author',      'a', true,  'author_name',    'Product author'))
			 ->addSwitch(new CliSwitch('website',     'w', true,  'url',            'Product website'))
			 ->addSwitch(new CliSwitch('package',     'p', true,  'package_name ',  'Name of the package for use in @package tag'))
			 ->addSwitch(new CliSwitch('doc-version', 'd', false, 'version_number', 'Version number for use in @version tag'))
			 ->addSwitch(new CliSwitch('output',      'o', false, 'file|dir',       'The file/directory to write the generated class to; default is stdout'));
	
	array_shift($argv);
	$args = Cli::parseArgs($argv, $switches);
	$args = $args['switches'];
	
	if (!isset($args['name'], $args['version'], $args['author'], $args['website'])) {
		usage();
		fwrite(STDERR, 'name, version, author, website and package are required');
		exit(1);
	}
	
	$self            = basename(__FILE__);
	$datetime        = date('Y-m-d H:i:s');
	
	$args['name']    = addslashes($args['name']);
	$args['version'] = addslashes($args['version']);
	$args['author']  = addslashes($args['author']);
	$args['website'] = addslashes($args['website']);
	
	$code            = <<<ENDCODE
<?php
	
	/**
	 * Product
	 *
	 * @package $args[package]
	 */
	
	/**
	 * Product meta information
	 *
	 * This class was auto-generated by $self on $datetime.
	 *
	 * @package $args[package]
	 */
	final class Product {
		
		/**#@+
		 * @var string
		 */
		/**
		 * Product name
		 */
		const NAME    = '$args[name]';
		
		/**
		 * Product version
		 */
		const VERSION = '$args[version]';
		
		/**
		 * Product author
		 */
		const AUTHOR  = '$args[author]';
		
		/**
		 * Product website
		 */
		const WEBSITE = '$args[website]';
		
		/**
		 * Product build date
		 */
		const DATE    = '$datetime';
		/**#@-*/
		
		private function __construct() {}
		
	}
	
?>
ENDCODE;

	if (isset($args['output']) && !is_dir(dirname($args['output'])) && !mkdir(dirname($args['output']), 0777, true)) {
		fwrite(STDERR, 'Unable to mkdir(): ' . dirname($args['output']));
		exit(1);
	} else if (!isset($args['output'])) {
		$args['output'] = 'php://stdout';
	}
	
	if (is_dir($args['output'])) {
		$args['output'] .= DIRECTORY_SEPARATOR . 'Product.php';
	}
		
	$fp = fopen($args['output'], 'w');
	if (!$fp) {
		fwrite(STDERR, 'Unable to open ' . $args['output'] . ' for writing');
		exit(1);
	}
	
	fwrite($fp, $code);
	fclose($fp);
	echo 'Wrote data to ' . $args['output'] . ' (' . strlen($code) . ' bytes)' . "\n";
	
	exit(0);



?>