<file_sep>#!/usr/bin/php
<?php
$ops = getopt("d:i:o:eyqvh");

$quiet = false;
$verbose = false;
$prompt = true;
$erase = false;
$output = ".";
$input = "";
$operation = "GIT";
$commands = array();
$display = array();

if(array_key_exists('h',$ops))
{
	usage();
	return 0;
}

if(array_key_exists('q',$ops))
	$quiet = true;

if(array_key_exists('v',$ops))
	$verbose = true;

if(array_key_exists('y',$ops))
	$prompt = true;

if(array_key_exists('e',$ops))
	$erase = true;

if(array_key_exists('d',$ops))
	$output = $ops['d'];

if(array_key_exists('o',$ops))
	$operation = $ops['o'];

if(array_key_exists('i',$ops))
	$input = $ops['i'];
else
{
	echo "Error: No input specified\n\n";
	usage();
	return -1;
}
$display = array();
$commands = array();
// Validate operation and do stuff
switch($operation)
{
	case 'GIT':
		git($quiet,$verbose,$erase,$output,$input,$display,$commands);
		break;
	case 'FILE':
		fileop($quiet,$verbose,$erase,$output,$input,$display,$commands);
		break;
	case 'TAR':
		die("Error: Not created yet.\n\n");
		break;
	default:
		echo "Error: Not a valid operation.\n\n";
		usage();
		return -1;
		break;
}

prompt($display);
run($commands);
return;
function git($quiet,$verbose,$erase,$dir,$input,&$display,&$commands)
{
	$commands = array();
	$display = array();
	$outdir = "/tmp/phpdeploy".(time() + rand());
	$display[] = "A git clone will be performed on $input.";
	$display[] = "The output will be placed into $dir.";
	$ops = "";
	if($quiet)
	{
		$ops .= " -q";
		$display[] = "This will be done quietly.";
	}
	if($verbose)
	{
		$ops .= " -v";
		$display[] = "This will be done verbosely.";
	}
	$ops = trim($ops);
	$commands[] = "git clone $ops $input $outdir";
	if($erase)
	{
		$display[] = "\033[0;31m*** DANGER ***";
		$display[] = "The contents of $dir will be erased.\033[0m";
		$commands[] = "rm -rf $dir";
	}
	$ops = "";
	if($quiet)
		$ops .= "q";
	if($verbose)
		$ops .= "v";
	$commands[] = "rsync -aC$ops --exclude \".git\" --exclude \".git/\" $outdir/ $dir";
	$commands[] = "rm -rf $outdir";
}

function fileop($quiet,$verbose,$erase,$dir,$input,&$display,&$commands)
{
	$commands = array();
	$display = array();
	$display[] = "A rsync will be performed from $input to $dir.";
	$ops = "";
	if($quiet)
	{
		$ops .= "q";
		$display[] = "This will be done quietly.";
	}
	if($verbose)
	{
		$ops .= "v";
		$display[] = "This will be done verbosely.";
	}
	if($erase)
	{
		$display[] = "\033[0;31m*** DANGER ***";
		$display[] = "The contents of $dir will be erased.\033[0m";
		$commands[] = "rm -rf $dir";
	}
	$commands[] = "rsync -aC$ops --exclude \".git\" --exclude \".git/\" $input $dir";
}

function run($commands)
{
	global $verbose;
	$junk = array();
	$val = 0;
	foreach($commands as $command)
	{
		if($verbose)
			echo $command."\n";
		passthru($command,$val);
		if($val != 0)
		{
			echo "\n\n\033[0;31mAn error occured while running the following command:\n";
			echo "\t$command\033[0m\n\n";
			return -1;
		}
	}
	echo "\033[0;32mOperations Successful\033[0m\n";
}
function prompt($array)
{
	$txt = implode("\n",$array);
	$val = readline($txt."\n\nDo you want to continue? [yN]: ");
	if(trim(strtolower($val)) != "y")
	{
		exit(0);
	}
	else
	{
		return true;
	}
}

function usage()
{
	global $argv, $argc;
echo "
usage: ${argv[0]}  [-o GIT] [-d OUTPUT_DIR] -i INPUT

This script can be used to deploy code from a directory, tar file, or
git repository into a given directory.

OPTIONS:
-d      Output directory. Defaults to .
-i      Input for the givem operation
         Git URL, File Path, Tar location, etc.
-o      Defines operation used to get files.
         Valid options are GIT, FILE, and TAR
         GIT is the default
-e      Empty output directory before deploying *DANGEROUS*
-y      Bypass prompt to confirm operation
-q      Quiet
-v      Verbose
-h      Display this message
";
}
?>
<file_sep>#!/bin/bash

#rsync -aC --exclude ".git" --exclude ".git/" WOTRK/ WOTRK2/
TMPFOLDER=/tmp/DEPLOY$RANDOM
#git clone http://dev.webapps.brake.local/git/WOTRK.git $TMPFOLDER

#########################################
usage()
{
	cat << EOF
usage: $0 [-o GIT] [-d OUTPUT_DIR] -i INPUT

This script can be used to deploy code from a directory, tar file, or
  git repository into a given directory.

OPTIONS:
-d      Output directory. Defaults to .
-i      Input for the givem operation
          Git URL, File Path, Tar location, etc.
-o      Defines operation used to get files.
          Valid options are GIT, FILE, and TAR
			 GIT is the default
-e      Empty output directory before deploying *DANGEROUS*
-y      Bypass prompt to confirm operation
-q      Quiet
-v      Verbose
-h      Display this message

EOF
}
###
#	Parse Arguments
###
ARGC=$#
OUTPUT='.'
INPUT=""
OPERATION="GIT"
DELETE=0
PROMPT=1
QUIET=0
VERBOSE=0

while getopts “o:i:d:eyqvh” OPTION
do
	case $OPTION in
		h)
			usage
			exit 1
			;;
		o)
			OPERATION=$OPTARG
			;;
		e)
			DELETE=1
			;;
		y)
			PROMPT=0
			;;
		q)
			QUIET=1
			;;
		v)
			VERBOSE=1
			;;
		i)
			INPUT=$OPTARG
			;;
		d)
			OUTPUT=$OPTARG
			;;
		?)
			usage
			exit 1
			;;
	esac
done

# Build the arguments for the operation.
COMMAND=""
DISP=""
case $OPERATION in
	GIT)
			i=0
			OPS=""
			DISP="\n\nUsing git the repo at \"$INPUT\" will be cloned to $TMPFOLDER.\n"
			DISP="${DISP}The files will then be moved to $OUTPUT.\n"
			if [ "$QUIET" = "1" ]; then
				OPS="$OPS -q"
				DISP="${DISP}This will be done quietly.\n"
			fi

			if [ "$VERBOSE" = "1" ]; then
				DISP="${DISP}This will be done verbosly.\n"
				OPS="$OPS -v"
			fi
			COMMAND[$i]="git clone $INPUT $TMPFOLDER"
			i=$i+1
			if [ "$DELETE" = "1" ]; then
				COMMAND[$i]="$COMMAND;rm -rf $OUTPUT"
				i=$i+1
				DISP="${DISP}**DANGER**\n$OUTPUT will be emptied.\n"
			fi
			COMMAND[$i]="$COMMAND && rsync -aC --exclude \".git\" --exclude \".git/\" $TMPFOLDER/ $OUTPUT"
			i=$i+1
			COMMAND[$i]="$COMMAND && rm -rf $TMPFOLDER"
			i=$i+1
		;;
	FILE)
		echo "Error: Not created yet"
		exit -1
		;;
	TAR)
		echo "Error: Not created yet"
		exit -1
		;;
esac

if [ "$PROMPT" -eq "1" ]; then
	echo -e $DISP
	echo -e "\nDo you want to do this? [y/N]"
	VALUE=""
	read VALUE

	if [ "$VALUE" != "y" -a "$VALUE" != "Y" ]; then
		exit
	fi
fi

echo -e "\nCommand:\n$COMMAND"
COMMAND=("${COMMAND[@]}")
for x in ${!COMMAND[*]}; do
	${COMMAND[$x]}
done
