#!/usr/bin/perl

use constant LIB_PATH => "/usr/lib/perlbox-voice";
use strict;
my $cp_str;

if(!(-d $ENV{'HOME'}."/.perlbox-voice")){
   #then create this dir
   mkdir($ENV{'HOME'}."/.perlbox-voice");
   mkdir($ENV{'HOME'}."/.perlbox-voice/tmp");


   $cp_str="cp -R ".LIB_PATH."/commands $ENV{'HOME'}/.perlbox-voice/";
   system($cp_str);

   $cp_str="cp -R ".LIB_PATH."/dtplugins $ENV{'HOME'}/.perlbox-voice/";
   system($cp_str);

}

if(!(-e $ENV{'HOME'}."/.perlbox-voice/Listener.conf")){
   #first, attempt to locate sphinx paths
   my $sphinx2_bin="";
   my $hmm_path="";
   if(-e("/usr/local/bin/sphinx2-continuous")){
      $sphinx2_bin="/usr/local/bin/sphinx2-continuous";
   }
   else{
      $sphinx2_bin="/usr/bin/sphinx2-continuous";
   }

   if(-d("/usr/local/share/sphinx2/model/hmm/6k")){
      $hmm_path="/usr/local/share/sphinx2/model/hmm/6k";
   }
   else{
      $hmm_path="/usr/share/sphinx2/model/hmm/6k";
   }

   open(CONF,">".$ENV{'HOME'}."/.perlbox-voice/Listener.conf");
   print CONF
"
[path_section]
task=$ENV{'HOME'}/.perlbox-voice/commands
agent_bin=".LIB_PATH."/PerlboxListener.pl
dict=$ENV{'HOME'}/.perlbox-voice/commands/current.dic
language_model=$ENV{'HOME'}/.perlbox-voice/commands/current.lm
hmm=$hmm_path
perlbox_lib=".LIB_PATH."/Perlbox/
sent=$ENV{'HOME'}/.perlbox-voice/commands/current.sent
s2continuous=$sphinx2_bin

[option_section]
perlbox-voice-version=.09
verbosity_level=3
playsound=1
help_browser=mozilla
desktop_plugin=none
magic=listen
use_magic=0
magic_interval=5


[command_section]
night=say sleep well
console=konsole
morning=say and good morning to you
web=mozilla
terminal=xterm
movie=totem
editor=kate
music=xmms
mail=kmail
home=konqueror

";


}

use Getopt::Long;
#============================================================
#Command line options
#============================================================
#first things first: get opt
my $nogui_opt = undef;
my $silent_opt = undef;
my $help_opt = undef;
my $usage_opt = undef;
my $opts = GetOptions  ("silent" => \$silent_opt,    # numeric
                        "nogui"   =>   \$nogui_opt,
                        "help"  => \$help_opt,
                        "usage" => \$usage_opt); 
if(defined( $help_opt )){
   print "
          Options:
          --nogui   :No graphical user interface
          --silent  :Minimal printing
";
   exit;
}

if(defined($usage_opt)){
   print "perlbox-voice [--nogui] [--silent]\n";
   exit;
} 

my $optstring = "";

$optstring += " --nogui" if defined($nogui_opt);
$optstring += " --silent" if defined($silent_opt);
if( defined($nogui_opt) ){
   system(LIB_PATH."/pbox-voice --nogui");
}
else{
   system(LIB_PATH."/pbox-voice")
}



