#!/usr/bin/env perl

# DO NOT EDIT the /cluster/bin/scripts copy of this file --
# edit ~/kent/src/hg/utils/automation/doEnsGeneUpdate.pl instead.

# $Id: ensVersions,v 1.5 2008/04/03 23:55:20 hiram Exp $

use Getopt::Long;
use warnings;
use strict;
use FindBin qw($Bin);
use lib "$Bin";
use EnsGeneAutomate;
use HgAutomate;
use File::Basename;


# Option variable names, both common and peculiar to this script:
use vars @HgAutomate::commonOptionVars;
use vars qw/
    $opt_db
    /;

my $base = $0;
$base =~ s/^(.*\/)?//;

# Globals:
my ($ensVersion);
# Command line argument:
my $CONFIG;

sub checkOptions {
  # Make sure command line options are valid/supported.
  my $ok = GetOptions( @HgAutomate::commonOptionSpec,
		      "db=s",
		      );
  &usage(1) if (!$ok);
  &usage(0, 1) if ($opt_help);
  &HgAutomate::processCommonOptions();
}

sub usage {
  # Usage / help / self-documentation:
  my ($status, $detailed) = @_;
  # Basic help (for incorrect usage):
  print STDERR "usage: $base <ensembl version> [-db=<ucsc db>]
options:
";
    my (@versionList) = &EnsGeneAutomate::ensVersionList();
    my $versionListString = join(", ", @versionList);
  print STDERR <<_EOF_
    <ensembl version> - currently can run $versionListString or "any" meaning find all
    [-db=<ucsc db>] - when requested check this one db only,
	default is to check all ucsc databases
_EOF_
  ;
  exit $status;
}

#  check for one db/version combination
sub checkOne($$) {
    my ($db, $ver) = @_;
    my ($ensGtfUrl, $ensPepUrl, $ensMySqlUrl, $ensVersionDateReference) =
	&EnsGeneAutomate::ensGeneVersioning($db, $ver );
    if (defined($ensGtfUrl)) {
	$ensGtfUrl =~ s#.*/gtf/##;
	$ensGtfUrl =~ s#.gtf.gz##;
	$ensGtfUrl =~ s#.*/##;
	print "UCSC:$db:\tensembl:$ver\t$ensVersionDateReference\t$ensGtfUrl\n";
    } else {
	print "UCSC:$db:\tensembl:$ver\tNOT FOUND\n";
    }
}

#########################################################################
# main

# Prevent "Suspended (tty input)" hanging:
&HgAutomate::closeStdin();

# Make sure we have valid options and exactly 1 argument:
&checkOptions();
&usage(1) if (scalar(@ARGV) != 1);

($ensVersion) = @ARGV;

# Force debug and verbose until this is looking pretty solid:
# $opt_debug = 1;
# $opt_verbose = 3 if ($opt_verbose < 3);

my @dbList;
if (defined($opt_db)) {
    $dbList[0] = $opt_db;
} else {
    @dbList = split('\n',
    `hgsql -N -e "select name from dbDb order by orderKey;" hgcentraltest`);
}

if (defined($opt_db)) {
&HgAutomate::verbose(2,
	"\n db: $opt_db, ensembl version: '$ensVersion'\n");
} else {
&HgAutomate::verbose(2,
	"\n db: all databases, ensembl version: '$ensVersion'\n");
}

if ($ensVersion =~ m/any/i) {
    my (@versionList) = &EnsGeneAutomate::ensVersionList();
    if (scalar(@versionList) > 1) {
	for (my $i = 0; $i < scalar(@versionList); ++$i) {
	    print STDERR "version: $versionList[$i]\n";
	    for (my $j = 0; $j < scalar(@dbList); ++$j) {
		checkOne($dbList[$j], $versionList[$i]);
	    }
	}
    } else {
	die "ERROR: ensVersion: can not find any version list in EnsGeneAutomate.pm\n";
    }
} elsif (defined($opt_db)) {
    checkOne($opt_db, $ensVersion);
} else {
    for (my $i = 0; $i < scalar(@dbList); ++$i) {
	checkOne($dbList[$i], $ensVersion);
    }
}
