#!/usr/bin/perl
use Cwd qw/abs_path/;
use strict;

my $ENV_DIR = shift;
if (!defined $ENV_DIR) {
    die "Please specify an environment directory to write configs into\n";
}
$ENV_DIR = abs_path($ENV_DIR);

# Shell
open C, '>', "$ENV_DIR/etc/env.sh";
print C <<EOD;
if [ -z "\$PATH" ]; then
    PATH="$ENV_DIR/bin"
else
    PATH="\$PATH:$ENV_DIR/bin"
fi
export PATH

if [ -z "\$PERL5LIB" ]; then
    PERL5LIB="$ENV_DIR/lib/perl5"
else
    PERL5LIB="\$PERL5LIB:$ENV_DIR/lib/perl5"
fi
export PERL5LIB

BLERG_HOME="$ENV_DIR"
export BLERG_HOME

BLERG_PATH="\$BLERG_HOME/data"
export BLERG_PATH
EOD
close C;

# nginx FastCGI params
open C, '>', "$ENV_DIR/etc/nginx-fastcgi-vars.conf";
print C <<EOD;
fastcgi_param BLERG_HOME "$ENV_DIR";
fastcgi_param BLERG_PATH "$ENV_DIR/data";
fastcgi_param PERL5LIB "$ENV_DIR/lib/perl5";
EOD
close C;

# Apache SetEnv
open C, '>', "$ENV_DIR/etc/apache-setenv.conf";
print C <<EOD;
SetEnv BLERG_HOME "$ENV_DIR"
SetEnv BLERG_PATH "$ENV_DIR/data"
SetEnv PERL5LIB "$ENV_DIR/lib/perl5"
EOD
close C;
