#!/usr/bin/perl -w
# -*- mode: perl-mode -*-

use strict;
use Cwd;

if (@ARGV > 1 || @ARGV == 1 && $ARGV[0] !~ /^(dev|devtest|dev7|test|test2|testload|prod|sbx|sbxdevtest)$/) {
    die "Usage: $0 [type]\n";
}

my ($wd) = getcwd();# =~ m!^(/.*?/www/[^/]+)!;
if (!defined $wd || !-d "$wd/protected") {
    die "incorrect working directory";
}

my $local = "$wd/protected/SettingsLocal.pm";

if (!-e $local) {
    print "SettingsLocal.pm does not exists\n";
} elsif (!-l $local) {
    die "SettingsLocal is not symlink";
} else {
    my $link = readlink $local;
    print "old link: $link\n";
}

if (@ARGV) {
    unlink($local);
    if ($ARGV[0] ne 'prod') {
        $ARGV[0] =~ s/dev/Dev/g;
        $ARGV[0] =~ s/test/Test/g;
	$ARGV[0] =~ s/sbx/Sandbox/g;
        $ARGV[0] =~ s/load/Load/g;
        symlink("$wd/protected/Settings$ARGV[0].pm", $local);
    }
    print "new configuration: $ARGV[0]\n";
    system("$wd/apache/init.sh restart");
}
