#!/usr/bin/env perl
use warnings;
use strict;

use Getopt::Long;
use File::Find::Rule;

# Declarations and stuff...
my (%opt, $host, $port, $lhost, $lport, $dir, $sdir);
my $config = ".vrp";
GetOptions (\%opt, 'help', 'host=s', 'h=s', 'port=s', 'p=s', 'lhost=s', 'lh=s', 'lport=s', 'lp=s', 'dir=s', 'd=s', 'sdir=s', 'sd=s', 'force', 'f');

# Clear the screen
if($^O =~ /Win32/) { system("cls"); }
else { system("clear"); }

# Actually run stuff
&scan4config;
&main;

# By default look for config. If it's not there grab command line parameters
# If the config is there load it. If --force (or --f) is given it will grab
# the command line parameters EVEN if a config does exist.
sub scan4config {
    if(-e $config and -r $config and -w $config) {
        print "Loaded Config...\n";
        if($opt{'force'} or $opt{'f'}) {
            print "Parsing passed in parameters by force!\n";
            print "This will override config data!\n";
            &getparam;
        }
    }
    else {
        print "Config '$config' is either non existent, ";
        print "or it's not readble/writable!\n";
        print "Running INIT...\n";
        &init;
    }
}

# If a config is not located, this is run to create a config, by asking each
# option individually, and recording the answers to a config file.
sub init {
    print "What is the host/ip of the server you will connect to: ";
    $host = <STDIN>; chomp $host;
    print "What is the port of the server you will connect to: ";
    $port = <STDIN>; chomp $port;
    print "What is your local ip (if private, make sure you forward): ";
    $lhost = <STDIN>; chomp $lhost;
    print "What is your local port (if private ip, make sure you forward): ";
    $lport = <STDIN>; chomp $lport;
    print "What is your Shared directory: ";
    $dir = <STDIN>; chomp $dir;
    print "What is your Download directory: ";
    $sdir = <STDIN>; chomp $sdir;

    open(FP, ">.vrp");
    print FP "HOST=$host\n";
    print FP "PORT=$port\n";
    print FP "LOCAL_HOST=$lhost\n";
    print FP "LOCAL_PORT=$lport\n";
    print FP "SHARED_DIR=$dir\n";
    print FP "DOWNLOAD_DIR=$sdir\n";
    close(FP); 
    print "Done!...Starting Engine...\n";
}


# Grab all the given command line parameters and parse them accordingly
# If nothing usefull (coded) is given, direct them to the help section
sub getparam {
    if ( ($opt{'h'} and $opt{'p'}) ) {
        $host = $opt{'host'};
        $port = $opt{'port'};
    }
    elsif ( ($opt{'host'} and $opt{'port'}) or ($opt{'h'} and $opt{'p'}) ) {
        $host = $opt{'host'};
        $port = $opt{'port'};
    }
    elsif ($opt{'help'}) {
        &help;
    }
}

# The main part of the program
sub main {
    print "\n+== VPR Client ==+\n";
    print "------------------\n";

    use Term::ReadLine;
    my $term = new Term::ReadLine 'VPR Client';
    my $prompt = "vpr> ";
    my $OUT = $term->OUT || \*STDOUT;

    while ( defined ($_ = $term->readline($prompt)) ) {
        my $return = "Error! Command not Found!\nType 'help' for info.\n";

        if($_ eq "help") {
            $return = "(c)onnect -> connect to given host on given ip";
        }

        warn $@ if $@;
        print $OUT "\n$return", "\n" unless $@;
        $term->addhistory($_) if /\S/;
    }
}




# Direct people to this sub when they can't provide the correct command line
# arguments for the program. This will end up getting called if they don't have # have a config AND don't provide correct arguments.
sub help {
    print "+== HELP ==+\n";
    print "--------------\n";
    exit;
}
