#!/usr/bin/php
<?php                                     // -*- php -*-
/***************************************************************
*  Copyright notice
*
*  (c) 2004, 2005 Michael Stucki (michael@typo3.org), Christian Leutloff <leutloff@debian.org>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project 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.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*  A copy is found in the textfile GPL.txt and important notices to the license
*  from the author is found in LICENSE.txt distributed with these scripts.
*
*
*  This script 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.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * TYPO3 site installer
 * This script will create new TYPO3 sites.
 *
 * $Id: typo3-site-installer,v 1.8 2005/05/04 16:42:11 leutloff Exp $
 *
 * @author	Christian Leutloff <leutloff@debian.org>
 * @author	Sven Wilhelm <wilhelm@icecrash.com>
 * @author	Michael Stucki <michael@typo3.org>
 */
//$debug = 2; error_reporting(E_ALL);
$debug = 0; error_reporting(E_NONE);


require_once('Console/Getopt.php');

// FIXME get actual path from the environment and set the include dir
// correctly
$INCLUDE_DIR = 'include/';
if (!file_exists($INCLUDE_DIR.'class.site_installer.php')) {
    	$INCLUDE_DIR = dirname($_SERVER['SCRIPT_FILENAME']).'/include/';
    	if (!file_exists($INCLUDE_DIR.'class.site_installer.php')) {
            	$INCLUDE_DIR = '/usr/share/typo3-site-installer/';
                if (!file_exists($INCLUDE_DIR.'class.site_installer.php')) die('Could not determine the path to include class.site_installer.php!!'."\n");
        }
}

	// Include the base class and start
require($INCLUDE_DIR.'class.site_installer.php');


/**
 * Display version info
 */
function printVersion()	{
	global $debug;

	echo "\n";
	echo 'typo3-site-installer, v0.90, 06.05.05' ;
	if ($debug > 0)	echo ", Debugging enabled";
	echo "\n";
}


/**
 * Display usage info
 */
function printUsage()	{
	global $argv;

	echo "
typo3-site-installer helps to install fresh TYPO3 sites

  Usage: $argv[0] [OPTIONS]

  General options:
    -d, --destination <DIR>    Specify the target directory for your new site (Default: ".DEFAULT_DESTINATION_DIR.")
    -s, --t3source <DIR>       typo3 source installation directory (Default: ".DEFAULT_TYPO3SOURCE_DIR.")\n";

	/*
	not supported at the moment
	echo "    -a, --always-latest        If set, the symlink typo3_src will point to\n" ;
	echo "                               /var/lib/typo3/latest\n" ;
	*/

	echo "
    -r, --root <DIR>           root directory (Default: '/')
    -n, --dry-run [false]      dry-run (Default: true)
    --debug                    Increase debugging level by one
    -h, -?, --help             Display this help
    -v, --version              Display version of this script

  Options you can only use as root:
    -f, --fix-permissions      Fix all permissions
    -g, --group <GROUP>        Change group ownership to this group (Default: ".DEFAULT_WWW_GROUP.")
    -u, --user <USER>          Change user ownership (Default: ".DEFAULT_WWW_USER.")\n\n";
}


/**
 * parse the command line
 */
function parseOptions($theSiteInstaller) {
	global $debug;

	$shortoptions='ad:fg:hnr:s:u:v?';
	$longoptions=array("always-latest",
                           "debug",
                           "destination=",
                           "dry-run",
                           "dry-run=",
                           "fix-permissions",
                           "group=",
                           "root=",
                           "t3source=",
                           "user=",
                           "help",
                           "version");
        
	$con = new Console_Getopt;
	$args = $con->readPHPArgv();
	array_shift($args);

	if (($options = $con->getopt($args, $shortoptions, $longoptions)) === false) {
		echo 'wrong parameters',"\n";
		printVersion();
		printUsage();
		exit();
	}

	if ($debug > 1)	print_r($options[0]);

	foreach ($options[0] as $opt) {
		if ($debug > 1)	print_r($opt);

		switch($opt[0]) {
			case 'h':
			case '?':
			case '--help':
				printVersion();
				printUsage();
				exit();
			break;

			case 'v':
			case '--version':
				printVersion();
				exit();
			break;

			case 'a':
			case '--always-latest':
				$theSiteInstaller->setAlwaysLatest(true);
			break;

			case 'd':
			case '--destination':
				$theSiteInstaller->setDestinationDir($opt[1]);
			break;

			case 'f':
			case '--fix-permissions':
				$theSiteInstaller->fixPermissions(true);
			break;

			case 'g':
			case '--group':
				$theSiteInstaller->setWWWGroup($opt[1]);
			break;

			case '--debug':
				$debug++;
			break;

			case 'n':
				$theSiteInstaller->setDryRun(true);
			break;

			case '--dry-run':
				if (empty($opt[1]) || ($opt[1] !== 'false'))	$theSiteInstaller->setDryRun(true);
				else	$theSiteInstaller->setDryRun(false);
			break;

			case 'r':
			case '--root':
				$theSiteInstaller->setRootDir($opt[1]);
			break;

			case 's':
			case '--t3source':
				$theSiteInstaller->setSourceDir($opt[1]);
			break;

			case 'u':
			case '--user':
				$theSiteInstaller->setWWWUser($opt[1]);
			break;

			default:
				// all right
			break;
		}
	}
}


$siteInstaller = new site_installer;

parseOptions(&$siteInstaller);

$siteInstaller->startUpCheck();
$siteInstaller->doActions();

?>