#!/usr/bin/perl -w

sub revision_branch {
    my $info = `svn info $srcroot`;
    my $rv = $? >> 8;

    if ( $? != 0 ){
        print STDERR "BSbuildid: `svn info` failed; trying `git svn info`\n";
        $info = `cd $srcroot && git svn info`;
        if ( $? != 0 ){
            print STDERR "BSbuildid: `git svn info` failed as well!\n";
            die "BSbuildid: could not determine SVN information\n";
        }
    }
    ($revision) = $info =~ /Last Changed Rev: (\d+)/;
    ($branch) = $info =~ /build\/(.*?)\/vertica/;

    print STDERR "BSBuildid: rev = $revision branch = $branch\n";

    return ($revision, $branch);
}

#+++++
#
#  Description:
#
#   This script captures information about how and when the designated
#   program was linked.
#
#  Call:
#   BSbuildid program_name 
#-----
#   $Source$
{
    require 'ctime.pl';

    $prgm = $ARGV[0] or
    die "BSbuildid: argument 1 must be the program_name\n";
    $brandname = $ARGV[1] or
    die "BSbuildid: argument 2 must be the brand name\n";
    $brandversion = $ARGV[2] or
    die "BSbuildid: argument 3 must be the product version\n";
    $srcroot = $ARGV[3] or
    die "BSbuildid: argument 4 must be the root of the product source\n";
    $codename = "";
    if ($#ARGV >= 4) {
        $codename = $ARGV[4]
    }

    $author = (getpwuid($>))[0];
    chop($date = &ctime(time));
    $type = $ENV{'BUILD'} || 'Experimental';
    chop($machine = `uname -n`);

    # also include the svn revision 
    my ($revision, $branch) = revision_branch();

    # compute hash of SDK files and include in build info
    use Digest::MD5;

    $sdk_dir = "$srcroot/VerticaSDK/include";
    $java_sdk_dir = "$srcroot/VerticaSDK/com/vertica/sdk";
    my @sdk_files = sort(glob("$sdk_dir/*.h $sdk_dir/*.cpp"));
    my @java_sdk_files = sort(glob("$java_sdk_dir/*.java"));
    @sdk_files = grep { $_ !~ /BuildInfo\.h/ } @sdk_files;
    @java_sdk_files = grep { $_ !~ /BuildInfo\.java/ } @java_sdk_files;
    my $md5 = Digest::MD5->new();
    
    foreach my $file (@sdk_files) {
        open(FILE, $file) or die "Can't open '$file': $!";
        binmode(FILE);
        $md5->addfile(FILE);
    }
    foreach my $javafile (@java_sdk_files) {
        open(FILE, $javafile) or die "Can't open '$javafile': $!";
        binmode(FILE);
        $md5->addfile(FILE);
    }
    $digest = $md5->hexdigest;



    if ($prgm eq 'VerticaSDK') { # Generate build info for VerticaSDK
    print <<EOS;
/* 
 * Copyright (c) 2014 Vertica Systems, an HP Company
 *
 * Description: Support code for UDx subsystem
 *
 * Create Date: $date
 */
/* Link-time identification for $prgm */
    static const char *VERTICA_BUILD_ID_Brand_Name       = "$brandname";
    static const char *VERTICA_BUILD_ID_Brand_Version    = "$brandversion";
    static const char *VERTICA_BUILD_ID_Codename         = "$codename";
    static const char *VERTICA_BUILD_ID_Date             = "$date";
    static const char *VERTICA_BUILD_ID_Machine          = "$machine";
    static const char *VERTICA_BUILD_ID_Branch           = "$branch";
    static const char *VERTICA_BUILD_ID_Revision         = "$revision";
    static const char *VERTICA_BUILD_ID_Checksum         = "$digest";
/* end of this file */
EOS
    }
    elsif ($prgm eq 'VerticaSDKJava') {
    print <<EOS;
/* 
 * Copyright (c) 2014 Vertica Systems, an HP Company
 *
 * Description: Support code for UDx subsystem
 *
 * Create Date: $date
 */
/* Build-time identification for $prgm */
package com.vertica.sdk;
public class BuildInfo {
    public static final String VERTICA_BUILD_ID_Brand_Name       = "$brandname";
    public static final String VERTICA_BUILD_ID_Brand_Version    = "$brandversion";
    public static final String VERTICA_BUILD_ID_Codename         = "$codename";
    public static final String VERTICA_BUILD_ID_Date             = "$date";
    public static final String VERTICA_BUILD_ID_Machine          = "$machine";
    public static final String VERTICA_BUILD_ID_Branch           = "$branch";
    public static final String VERTICA_BUILD_ID_Revision         = "$revision";
    public static final String VERTICA_BUILD_ID_Checksum         = "$digest";

    public static VerticaBuildInfo get_vertica_build_info() {
        VerticaBuildInfo vbi = new VerticaBuildInfo();
        vbi.brand_name      = BuildInfo.VERTICA_BUILD_ID_Brand_Name;
        vbi.brand_version   = BuildInfo.VERTICA_BUILD_ID_Brand_Version;
        vbi.codename        = BuildInfo.VERTICA_BUILD_ID_Codename;
        vbi.build_date      = BuildInfo.VERTICA_BUILD_ID_Date;
        vbi.build_machine   = BuildInfo.VERTICA_BUILD_ID_Machine;
        vbi.branch          = BuildInfo.VERTICA_BUILD_ID_Branch;
        vbi.revision        = BuildInfo.VERTICA_BUILD_ID_Revision;
        vbi.checksum        = BuildInfo.VERTICA_BUILD_ID_Checksum;
        return vbi;
    }
}
/* end of this file */
EOS
    }
    else {
    print <<EOS;
/* Link-time identification for the program '$prgm' */
    const char *VERTICA_BUILD_ID_Brand            = "$brandname $brandversion";
    const char *VERTICA_BUILD_ID_Brand_Name       = "$brandname";
    const char *VERTICA_BUILD_ID_Brand_Version    = "$brandversion";
    const char *VERTICA_BUILD_ID_Program_Name     = "$prgm";
    const char *VERTICA_BUILD_ID_Default_Log_Name = "$prgm.log";
    const char *VERTICA_BUILD_ID_Codename         = "$codename";
    const char *VERTICA_BUILD_ID_Type             = "$type";
    const char *VERTICA_BUILD_ID_Author           = "$author";
    const char *VERTICA_BUILD_ID_Date             = "$date";
    const char *VERTICA_BUILD_ID_Machine          = "$machine";
    const char *VERTICA_BUILD_ID_Branch           = "$branch";
    const char *VERTICA_BUILD_ID_Revision         = "$revision";
    const char *VERTICA_BUILD_ID_Checksum         = "$digest";
    const char *VERTICA_BUILD_ID_Tag              = "$prgm($type) built by $author\@$machine from $branch\@$revision on '$date' \$BuildId\$";
    const char *VERTICA_BUILD_BRAND_ID_Tag        = "$brandname $brandversion \$BrandId\$";
/* end of this .c file */
EOS
    }
}
