#!/usr/bin/perl

# $Id: datasources,v 1.4 2003-11-28 22:17:23 kiesling Exp $
$VERSION=1.0;

# 
# List data sources and ODBC drivers.
#

use UnixODBC qw(:all);
use Getopt::Long;

my $usage=<<EOH;
Usage: datasources [--help] | [--verbose]
  --help       Print this help and exit.
  --verbose    Print data sources' names and descriptions.
EOH

my $help = '';  # Print help and exit.
my $Verbose = ''; 

GetOptions ('help' => \$help,
	    'verbose' => \$Verbose);

if ($help) {
    print $usage;
    exit 0;
}

my ($envhandle, $sqlresult, $dsnname, $drivername);

$sqlresult = SQLAllocHandle ($SQL_HANDLE_ENV,$SQL_NULL_HANDLE, $envhandle);
if ($sqlresult != $SQL_SUCCESS) {
    &getdiagrec ($SQL_HANDLE_ENV, $envhandle);
    exit 1;
}
$sqlresult = SQLSetEnvAttr ($envhandle, $SQL_ATTR_ODBC_VERSION, 
			    $SQL_OV_ODBC3, 0);      
if ($sqlresult != $SQL_SUCCESS) {
    &getdiagrec ($SQL_HANDLE_ENV, $envhandle);
    exit 1;
}
$sqlresult = SQLDataSources ( $envhandle,$SQL_FETCH_FIRST,
			      $dsnname, 255, $messagelength1,
			      $drivername, 255, $messagelength2 );
print "$dsnname -- $drivername\n" if $sqlresult == $SQL_SUCCESS;
if (($sqlresult != $SQL_SUCCESS) && ($sqlresult != $SQL_NO_DATA)) {
    &getdiagrec ($SQL_HANDLE_ENV, $envhandle);
    exit 1;
}
while ($sqlresult != $SQL_NO_DATA) {
    $sqlresult = SQLDataSources ($envhandle, $SQL_FETCH_NEXT,
				 $dsnname, 255, $messagelength1,
				 $drivername, 255, $messagelength2);
    if (($sqlresult != $SQL_SUCCESS) && ($sqlresult != $SQL_NO_DATA)) {
	&getdiagrec ($SQL_HANDLE_ENV, $envhandle);
	exit 1;
    }
    if ($Verbose) {
	print "$dsnname -- $drivername\n" if $sqlresult == $SQL_SUCCESS;
    } else {
	print "$dsnname\n" if $sqlresult == $SQL_SUCCESS;
    }
}

$sqlresult = SQLFreeHandle ($SQL_HANDLE_ENV, $envhandle);
if ($sqlresult != $SQL_SUCCESS) {
    &getdiagrec ($SQL_HANDLE_ENV, $envhandle);
    exit 1;
}

exit 0;

sub getdiagrec {
    my ($handle_type, $handle) = @_;
    my ($sqlstate, $native, $message_text,$msglength);
    $sqlresult = SQLGetDiagRec ($handle_type, $handle,
				1, $sqlstate, $native,
				$message_text, 255, $msglength);
    if ($sqlresult == 100) { 
	print "result \= $sqlresult\n";
    } elsif (($sqlresult == 1) || ($sqlresult == 0)) { 
	print "result \= $sqlresult, text \= $message_text\n";
    } else { 
	print "sqlresult = $sqlresult\n";
    }
}

=head1 NAME

datasources - Display data sources and descriptions.

=head1 SYNOPSIS

datasources [--help] | [--verbose]

=head1 DESCRIPTION

Datasources lists the data sources that are configured on a host
system, and, optionally, the descriptions for each data source.

=head1 OPTIONS

=head2 --help

Print help message and exit.

=head2 --verbose

Print the description of each data source in addition to the 
data source name.

=head1 VERSION INFORMATION AND CREDITS

Revision: $Revision: 1.4 $

Written by: Robert Allan Kiesling, rkies@cpan.org.

Licensed under the same terms as Perl.  Please refer to the
file "Artistic" for details.

=head1 SEE ALSO

perl(1), UnixODBC(3), UnixODBC::BridgeServer(3).

=cut

