#!/usr/bin/perl

#
# build obo file if needed
#

use strict;
use Time::Local;

use constant OBO2OBO => "/share/go/bin/oboedit/current/obo2obo";
use constant LOGFILE => "/share/go/logs/build_obo2obo.log";

# OBO10MAIN is the output, the OBO v1.0 file
# OBO10COPY is a copy of the OBO v1.0 file
# OBO12MAIN is the input, the OBO v1.2 file
# OBO12COPY is a copy of the OBO v1.2 file
# OBOEDITOR is the source file for OBO12MAIN, the file edited by the GO Editors

my %files =
    (
     OBO10COPY  => "/share/ftp/pub/go/ontology/gene_ontology.obo",
     OBO10MAIN  => "/share/ftp/pub/go/ontology/obo_format_1_0/gene_ontology.1_0.obo",
     OBO12COPY  => "/share/ftp/pub/go/ontology/gene_ontology_edit.obo",
     OBO12MAIN  => "/share/ftp/pub/go/ontology/obo_format_1_2/gene_ontology.1_2.obo",
     OBOEDITOR  => "/share/ftp/pub/go/ontology/editors/gene_ontology_write.obo",
     STRIPSCRIPT => "/share/go/bin/oboedit/current/docs/examplescripts/strip_disjoint_and_replaced_by_tags.osl",
    );

my $rebuild = 0;

open (LOG, ">>" . LOGFILE) || die "Cannot open log file, " . LOGFILE . "\n";

#
# select LOGFILE so that obo2obo and cvs output is captured
#
select (LOG); $| = 1;

print "--------------------\n";
print scalar localtime, "\n";
print "--------------------\n\n";

#  index to the array from stat() in next line:
#  $dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks
#  we'll use the "last modify time" of the file, index 9

my $lastoboedittime = timelocal(localtime((stat($files{OBOEDITOR}))[9]));
my $obo12maintime = timelocal(localtime((stat($files{OBO12MAIN}))[9]));
	
if ($obo12maintime == 0)
{
    die "Required file, $files{OBO12MAIN}, is missing!\n";
}

if ($obo12maintime < $lastoboedittime)
{
    print "$files{OBOEDITOR} is NEWER than $files{OBOEDITOR}\n";
    $rebuild = 1;
}
else
{
    print "$files{OBOEDITOR} is OLDER than $files{OBOEDITOR}\n";
}

#
# if OBOEDITOR (source v1.2 file) is newer than the OBO12MAIN (v1.2)
# file, the OBO v1.0 files need to be rebuilt
#

if ($rebuild)
{
    # clone LOG filehandler to STDERR
    open (STDOUT, ">&LOG") || die "Cannot dup LOG filehander for STDOUT: $!\n";
    open (STDERR, ">&LOG") || die "Cannot dup LOG filehander for STDERR: $!\n";

    my $dstr = `date +%Y%m%d`;
    chomp $dstr;

    print "Copy of OBO v1.2 files beginning.\n";

    my $cmd = "/usr/bin/cp $files{OBOEDITOR} $files{OBO12MAIN}";
    my $status = system ($cmd);

    &checkstatus($status,$cmd,$!);

    # commit updated obo file
    my $cmd = "/usr/bin/csh -c 'ulimit; /tools/gnu/bin/cvs -d /share/go/cvs commit -m $dstr $files{OBO12MAIN}'";
    my $status = system ($cmd);

    &checkstatus($status,$cmd,$!);

    my $cmd = "/usr/bin/cp $files{OBOEDITOR} $files{OBO12COPY}";
    my $status = system ($cmd);

    &checkstatus($status,$cmd,$!);

    # commit updated obo file
    my $cmd = "/usr/bin/csh -c 'ulimit; /tools/gnu/bin/cvs -d /share/go/cvs commit -m $dstr $files{OBO12COPY}'";
    my $status = system ($cmd);
 
    &checkstatus($status,$cmd,$!);

    print "Rebuild of " . $files{OBO10MAIN} . " starting.\n";

    # rebuild obo file
    my $cmd = OBO2OBO . " -formatversion OBO_1_0 $files{OBO12MAIN} -writecomments -o $files{OBO10MAIN} -runscript $files{STRIPSCRIPT} \\\;";
    my $status = system ($cmd);

    &checkstatus($status,$cmd,$!);

    # commit updated obo file
    my $cmd = "/usr/bin/csh -c 'ulimit; /tools/gnu/bin/cvs -d /share/go/cvs commit -m $dstr $files{OBO10MAIN}'";
    my $status = system ($cmd);

    &checkstatus($status,$cmd,$!);

    print "Copying of OBO v1.0 files beginning.\n";

    my $cmd = "/usr/bin/cp $files{OBO10MAIN} $files{OBO10COPY}";
    my $status = system ($cmd);

    &checkstatus($status,$cmd,$!);

    # commit updated obo file
    my $cmd = "/usr/bin/csh -c 'ulimit; /tools/gnu/bin/cvs -d /share/go/cvs commit -m $dstr $files{OBO10COPY}'";
    my $status = system ($cmd);

    &checkstatus($status,$cmd,$!);

}
else
{
    print "no update is needed for OBO v1.0 and v1.2 files\n";
}

print "\n";

close LOG;

exit 0;

sub checkstatus
{
    my $status = $_[0];
    my $cmd = $_[1];
    my $error = $_[2];

    if ($status)
    {
	die "FATAL: $cmd failed: $error\n";
    }
}
