#!/usr/bin/perl -w -T
use IO::Socket::INET;
use IPC::Open2;
use Gtk2 -init;
use strict;

# copyright Wulf Coulmann <scripts at gpl.coulmann dot de>
# GNU GPL
# http://www.gnu.org/licenses/gpl.html
# Download me here: http://cnc/dxf-printer.html



$ENV{'PATH'}= '';

# --- config ------

my $myport = 12000;
my $scale = 25.40009;
my $convert = "/usr/bin/pstoedit -xscale $scale -yscale $scale -f dxf_s "; 

chdir '/home/';

# --- go for it ---

my $pserve=IO::Socket::INET->new(LocalPort => $myport,Type=>SOCK_STREAM,Reuse=>1,Listen=>1) or die "can't do that $!\n";

while (my $pjob=$pserve->accept()) {

  my $file;
  my $d = Gtk2::FileChooserDialog->new ("file to store dxf", undef, "save", 
                                        'gtk-cancel' => 'cancel',
                                        'gtk-save' => 'ok',
                                        );
  $d->set_default_response ('ok');
  
  if ('ok' eq $d->run ()) {
         $file = $d->get_filename();
  }
  $d->destroy;
  #sleep 2;

  next unless length($file) > 0;

  $file =~ m/^(.*\/).*?$/;
  my $dir = $1;
  print "$dir\n"; 
  chdir $dir;
  
  print "now we print \n";
  open FILTER, "| $convert - $file";  
  my $input;
  while (<$pjob>) {
    $input .= "$_";
  }
  print FILTER $input;


  close FILTER ; 

  close $pjob;
  print "done\n";
}

