#!/usr/local/bin/perl

use profxp::command;
use profxp::screen;
use profxp::tcp;
use profxp::config;

$version = 'v3.0';
$firsttime = 0;

# set STDOUT to autoflush
$| = 1;

# chdir to ~/.profxp (working) directory
if (!(chdir($ENV{"HOME"} || $ENV{"LOGDIR"} || (getpwuid($<))[7]))) {
  error("cannot chdir to your home directory !");
  exit(0);
};
MAKECONFIGDIR: if (!(chdir(".profxpv3"))) {
  if (!(mkdir(".profxpv3",0700))) {
    error("can't CWD to /.profxpv3, and cannot create it either");
    exit(0);
  }
  else {
    $firsttime = 1;
  };
  # repeat 
  goto MAKECONFIGDIR;
};

# load config
if (!cfg_load()) {
  error("cannot read or create default config file '~/.profxpv3/config'");
  exit(0);
};

welcome();

if ($firsttime) {
  info('',1);
  info("-/- Welcome ! -/-",1);
  info('',1);
  info("It looks like this is your first time starting profxp. I have some",1);
  info("very helpful hints for you:",1);
  info('',1);
  info("1. $c{red}READ THE DOCS !$c{reset} (this is a pretty hardcore program)",1);
  info("2. start commands with CAPS to send them RAW, e.g. 'Site who'.",1);
  info("3. profxp uses quoteless command lines, DO NOT USE QUOTES FOR PATHS",1);
  info("4. get familiar with the alias function, it rocks.",1);
  info('',1);
  info("Type 'he' for help. Please use 'cf' to see a list of configuration",1);
  info("flags, set your local interface IP ('cf localif <IPADDR>') if you",1);
  info("want to use active transfer modes.",1);
  info('',1);
};

MAIN: while (1) {
  do_command(prompt());
};
