#!/usr/bin/perl -w

=head1 NAME

dta2csv - command-line tool to convert Stata 8 and Stata 10 .dta files to csv

=head1 SYNOPSIS

=over 8

=item dta2csv file.dta [otherfile.dta ...]

=back

=head1 BUGS

This is a simple command line tool using File::Stata::DtaReader,
with no documentation and poor error management.  All types of
missing values in the .dta file are turned into blank cells in the CSV.

=head1 AUTHOR

Written by Franck Latremoliere. Copyright (c) 2007 Reckon LLP.
http://www.reckon.co.uk/staff/franck/

=head1 LICENCE

This program is free software; you can use, redistribute and/or modify it under the same terms as Perl itself
(Artistic Licence or GNU GPL).

=cut

use warnings;
use strict;
use File::Stata::DtaReader;
use Data::Dumper;

unless (@ARGV) {
    print STDERR "dta2csv: no input files\n";
    print STDERR "Usage: dta2csv [-v] file.dta [otherfile.dta ...]\n";
    print STDERR "Copyright (c) 2007 Reckon LLP";
    print STDERR " (File::Stata::DtaReader version $File::Stata::DtaReader::VERSION)\n";
    exit 1;
}

my $verbose = 0;
for my $file (@ARGV) {
    if ( $file eq '-v' ) {
        $verbose = 1;
        next;
    }
    unless ( -e $file ) {
        warn "$file: file not found";
        next;
    }
    open DTA, '<', $file;
    my $dta = new File::Stata::DtaReader(*DTA);
    if ( $dta->{ds_format} == 114 || $dta->{ds_format} == 113 ) {
        print STDERR "$file: ";
        print STDERR $dta->{ds_format} == 114 ? 'Stata 10' : 'Stata 8';
        print STDERR ", $dta->{nvar} variables, $dta->{nobs} observations\n";
        print STDERR Dumper($dta) if $verbose;
        $file =~ s/(\.dta)?$/.csv/;
        my $no = '';
        $file =~ s/$no\.csv$/++$no . '.csv'/e while -e $file;
        open CSV, '>', $file;
        print CSV join( ',', @{ $dta->{varlist} } ) . "\n";

        while ( my @a = $dta->readRow ) {
            print CSV join(
                ',',
                map {
                    if ( defined $_ ) { s/"/\\"/g; qq%"$_"%; }
                    else { ''; }
                  } @a
              )
              . "\n";
        }
        close CSV;
    }
    else {
        warn "$file: not a Stata 8 or 10 .dta file";
    }
    close DTA;
}
