#!/usr/bin/perl -w

use strict;
use Debconf::Client::ConfModule ':all';
use Debconf::Log ':all';

# Preliminaries
version('2.0');
my $capb=capb('backup');
debug "developer" => "Capb returned $capb";

# Put a sentinel exit at the top of the stack to pull us out if the user
# decides to back *right* out of the script.
push(my @STATESTACK, "exit");

# The initial state.
my $STATE="configure_webserver";

# The mechanics of this system are quite simple.  If the state subroutine
# returns the name of a state, we move to that state.  Else we take the
# previous state off the stack and run that one again.
#
# Every possible state should be represented by a subroutine of the same
# name, which returns either the name of the next state to be called, or
# undef if we're to go back.
while ($STATE ne "exit") {
	no strict 'refs';
	my $NEXTSTATE = &$STATE;
	use strict;

	if ($NEXTSTATE) {
		push(@STATESTACK, $STATE);
	} else {
		$NEXTSTATE = pop(@STATESTACK);
	}
	$STATE=$NEXTSTATE;
}

# All done!

sub configure_webserver
{
	input("high", "dokuwiki/system/configure-webserver");
	my @ret = go();
	if ($ret[0] == 30) {
		return undef;
	} else {
		return "restart_webserver";
	}
}

sub restart_webserver
{
        input("medium", "dokuwiki/system/restart-webserver");
        my @ret = go();
        if ($ret[0] == 30) {
                return undef;
        } else {
                return "docroot";
        }
}

sub docroot
{
	input("low", "dokuwiki/system/documentroot");
	my @ret = go();
	if ($ret[0] == 30) {
		return undef;
	} else {
		return "accessible";
	}
}

sub accessible
{
	title("Access Control");
	input("medium", "dokuwiki/system/accessible");
	my @ret = go();
	if ($ret[0] == 30) {
		return undef;
	}
	@ret = get("dokuwiki/system/accessible");
	if ($ret[1] eq "local network") {
		return "localnet";
	} else {
		return "purgepages";
	}
}

sub localnet
{
	title("Local Network specification");
	input("critical", "dokuwiki/system/localnet");
	my @ret = go();
	if ($ret[0] == 30) {
		return undef;
	} else {
		return "purgepages";
	}
}

sub purgepages
{
	title("Purging pages on removal");
	input("high", "dokuwiki/system/purgepages");
	my @ret = go();
	if ($ret[0] == 30) {
		return undef;
	} else {
		return "exit";
	}
}
