#!/usr/bin/perl
use strict; use warnings;
=pod

=head1 NAME

vox-clean - delete vote-server data

=head1 SYNOPSIS

vox-clean

=cut


    sub clean()
    {
        traces(); # traces of trust network
        votes(); # voter input to the polls
        counts(); # compiled counts

      # Ordinarily there is no need to clean these data.
      # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      # geocode_cache(); # cache of geocode data
      # store_wap(); # ephemeral storage of unimportant data

      # - - -
        do_database_listing(); # show what's left in there
    }


=pod

=head1 DESCRIPTION

Deletes a vote-server's variable data.  Variable data include trust network traces, voter
input and vote counts.  Configuration files are unaffected.  Outputs a database listing of
the remaining tables and schemata.

=cut

    our $home;

    our $user;


{
    use Pod::Usage qw( pod2usage );
    scalar @ARGV and pod2usage( -verbose => 1 ); # and exits

    $home = $ENV{'HOME'}; $home or die;
    $user = $ENV{'USER'}; $user or die;
    $user eq 'v' and die "vox-clean - guarded for online vote-server 'v'";

    clean();
}


# ----------------------------------------------------------------------------------------


    sub counts()
    {
        my $dir = $home . '/votorola/out/vocount';
        -d $dir or return; # nothing to clean

        print( "removing directory: $dir\n" );
        system( "rm --recursive $dir" ) and die;

        my $schema = 'out_count';
        print( "removing database schema: $schema\n" ); # mounted counts
        system( "psql --command 'drop schema $schema cascade'" ); # fails if there is none
    }



    sub do_database_listing()
    {
        print( "\n" );
        system( 'psql --command "\dn"' ) and die;
        system( 'psql --command "\dt"' ) and die;
    }



    sub geocode_cache()
    {
        my $table = 'geocode';
        print( "removing geocode cache table: $table\n" );
        system( "psql --command 'drop table $table'" ); # fails if there is none
    }



    sub store_wap()
    {
        my $table = 'store_wap';
        print( "removing ephemeral storage table: $table\n" );
        system( "psql --command 'drop table $table'" ); # fails if there is none
    }



    sub traces()
    {
        my $dir = $home . '/votorola/out/votrace';
        -d $dir or return; # nothing to clean

        print( "removing directory: $dir\n" );
        system( "rm --recursive $dir" ) and die;

        my $schema = 'out_trace';
        print( "removing database schema: $schema\n" ); # mounted lists
        system( "psql --command 'drop schema $schema cascade'" ); # fails if there is none
    }



    sub votes()
    {
        my $table = 'in_vote';
        print( "removing database table: $table\n" );
        system( "psql --command 'drop table $table'" ); # fails if there is none
    }