#!/usr/bin/perl

use strict;
use warnings;

main();

sub main {
    my $srcroot = $ENV{'SRCROOT'};
    my $build_dir = $ENV{'TARGET_BUILD_DIR'};
    
	my $main_plist = "$srcroot/Support Files/Info-Pacifist.plist";
    my $plugin_plist = "$srcroot/Auxiliary Sources/QuickLook Plug-in/PacifistQL-Info.plist";
    my $helper_plist = "$srcroot/Auxiliary Sources/Privileged Helper Tool/HelperTool-Info.plist";
    my $helper_plist_10_6 = "$srcroot/Auxiliary Sources/Privileged Helper Tool/HelperTool10.6-Info.plist";
    
    my $app_bundle = "$build_dir/Pacifist.app";
    my $plugin_bundle = "$build_dir/Pacifist.qlgenerator";
    my $helper = "$build_dir/com.charlessoft.pacifist.helper";
    my $helper_10_6 = "$build_dir/com.charlessoft.pacifist.helper_10.6";
    
    my $vers_string = `defaults read \"$main_plist\" CFBundleShortVersionString`;
    
    $vers_string =~ s/^\s*(.*)\s*$/$1/;
    
	my $build_num = build_num_from_plist($main_plist);
    
	$build_num++;
    
    my $is_beta_build = is_beta_build($srcroot);

	write_build_num_to_plist($build_num, $vers_string, $main_plist, $is_beta_build);
    write_build_num_to_plist($build_num, $vers_string, $plugin_plist, $is_beta_build);
    write_build_num_to_plist($build_num, $vers_string, $helper_plist, $is_beta_build);
    write_build_num_to_plist($build_num, $vers_string, $helper_plist_10_6, $is_beta_build);
    
    # to make sure Xcode is forced to remake the info.plist for each
    
    delete_info_for_bundle($app_bundle);
    delete_info_for_bundle($plugin_bundle);
    
    if (-e "$helper") {
        unlink("$helper");
    }
    
    if (-e "$helper_10_6") {
        unlink("$helper_10_6");
    }
}

sub build_num_from_plist {
	my $plist = $_[0];
    
	my $build_num = `defaults read "$plist" BuildNumber`;
    
	return $build_num;
}

sub write_build_num_to_plist {
	my $build_num = $_[0];
    my $vers_string = $_[1];
	my $plist = $_[2];
    my $is_beta_build = $_[3];
    
    my $dmg_version;
    my $hex_version = sprintf("%X", $build_num);
    
    if ($is_beta_build) {
        $dmg_version = $hex_version;
    } else {
        if (defined($vers_string) && length($vers_string) != 0) {
            $dmg_version = $vers_string;
        } else {
            $dmg_version = $hex_version;
        }
    }
    
    system("defaults write \"$plist\" BuildNumber -integer $build_num");
	system("defaults write \"$plist\" CFBundleVersion -string \"$build_num\"");
    system("defaults write \"$plist\" CFBundleShortVersionString -string \"$vers_string\"");
    system("defaults write \"$plist\" DMGVersion -string \"$dmg_version\"");
}

sub delete_info_for_bundle {
    my $bundle_path = $_[0];
    my $info_path = "$bundle_path/Contents/Info.plist";
    
    if (-e "$info_path") {
        unlink("$info_path");
    }
}

sub is_beta_build {
    my $srcroot = $_[0];
    my $status_header = "$srcroot/Support Files/Beta Status.h";
    my $src_path = "/tmp/pacifist_beta_status_test.m";
    my $obj_path = "/tmp/pacifist_beta_status_test";
    my $status_code = "#import \"$status_header\"\n" .
    "int main(int argc, const char **argv) {\n" .
    "#ifdef BETA_BUILD\n" .
    "return 0;\n" .
    "#else\n" .
    "return 1;\n" .
    "#endif\n" .
    "}\n";
    
    if (-e $src_path) {
        unlink($src_path);
    }
    
    open(my $src_hand, ">$src_path") or die "couldn't open $src_path";
    print $src_hand $status_code;
    close($src_hand);
    
    system("clang $src_path -o $obj_path");
    unlink($src_path);
    
    my $exit_code = system($obj_path);
    unlink($obj_path);
    
    return ($exit_code >> 8) == 0;
}
