#!/usr/bin/env perl
#
# Copyright (c) 2015 Hypertriton, Inc. 
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# Generate an application or library bundle for the given target platform.
# Invoked by "make bundle" target of  or .
#
my $type = $ARGV[0];
my $tgt = $ARGV[1];
my $prog = '';
my $InstallProg = '';
unless ($type && $tgt && $type =~ /^(prog|lib)$/) {
	print STDERR "Usage: $0 [prog|lib] [target-platform]\n";
	exit(1);
}
if ($type eq 'prog') {
	$prog = $ENV{'PROG'};
	$InstallProg = $ENV{'INSTALL_PROG'};
	unless ($prog && $InstallProg) {
		print STDERR "Missing PROG/INSTALL_PROG\n";
		exit(1);
	}
}
my $App = $prog.'.app';
my $Contents = '';
my $Plist = '';
my @dirs = ();
if ($tgt eq 'OSX') {	
	$Contents = $App.'/Contents';
	$Plist = $Contents.'/Info.plist';
	@dirs = ($App, $Contents, $Contents.'/Resources', $Contents.'/MacOS');
} elsif ($tgt eq 'iOS') {
	$Plist = $App.'/Info.plist';
	@dirs = ($App);
}
foreach my $dir (@dirs) {
	if (! -e $dir) {
		print "mkdir $dir\n";
		mkdir($dir) || die "$dir: $!";
	}
}
if ($tgt eq 'OSX' || $tgt eq 'iOS') {
	my $BundleVersion = 1;
	if (-e $Plist) {
		if (open(PLIST, $Plist)) {
			my $vers = 0;
			LINE: foreach $_ () {
				if (/\s*CFBundleVersion\s*<\/key>/) {
					$vers = 1;
				}
				if ($vers && /\s*([\d]+)\.0\.0\s*<\/string>/) {
					print "Updating bundle version: $1+1\n";
					$BundleVersion = $1 + 1;
					last LINE;
				}
			}
			close(PLIST);
		}
	}
	open(PLIST, ">$Plist") || die "$Plist: $!";
	print PLIST << 'EOF';
EOF
	my $InfoSignature = $ENV{'PROG_SIGNATURE'};
	if ($prog =~ /^(\w{4})/) {
		$InfoSignature = $1;
	} else {
		$InfoSignature = 'xxxx';
	}
	my $ProgDisplayName = $ENV{'PROG_DISPLAY_NAME'};
	my $ProgIdentifier = $ENV{'PROG_IDENTIFIER'};
	print PLIST << "EOF";
  CFBundleDevelopmentRegion
  English
  CFBundleDisplayName
  $ProgDisplayName
  CFBundleExecutable
  $prog
  CFBundleIdentifier
  $ProgIdentifier
  CFBundleInfoDictionaryVersion
  6.0
  CFBundlePackageType
  APPL
  CFBundleVersion
  ${BundleVersion}.0.0
EOF
	if ($tgt eq 'OSX') {
		my $ProgSysVersion = $ENV{'PROG_OSX_VERSION'};
		my $PrincipalClass = $ENV{'PROG_PRINCIPAL_CLASS'};
		my $Copyright = $ENV{'PROG_COPYRIGHT'};
		my $ProgVersion = $ENV{'PROG_VERSION'};
		my $ProgCategory = $ENV{'PROG_CATEGORY'};
		print PLIST << "EOF";
  CFBundleName
  $prog
  CFBundleSignature
  $InfoSignature
  CFBundleGetInfoString
  $prog $ProgVersion
  CFBundleShortVersionString
  1.0.0
  LSApplicationCategoryType
  $ProgCategory
  LSMinimumSystemVersion
  $ProgSysVersion
  NSHumanReadableCopyright
  $Copyright
  NSPrincipalClass
  $PrincipalClass
  CFBundleIconFile
  ${prog}.icns
EOF
		print "$InstallProg $prog $Contents/MacOS\n";
		system("$InstallProg $prog $Contents/MacOS");
	} elsif ($tgt eq 'iOS') {
		my $RequiredCaps = $ENV{'PROG_REQUIRED_CAPABILITIES'};
		print PLIST "  UIRequiredDeviceCapabilities\n";
		print PLIST "    \n";
		foreach my $cap (split(' ', $RequiredCaps)) {
			print PLIST "      $cap\n";
		}
		print PLIST "    \n";
		print PLIST << "EOF";
  LSRequiresIPhoneOS
  YES
  
  CFBundleIconFiles
  
    ${prog}
  
EOF
		print "$InstallProg $prog $App\n";
		system("$InstallProg $prog $App");
	}
	if (my $ProgInfoExtra = $ENV{'PROG_INFO_EXTRA'}) {
		print PLIST "$ProgInfoExtra\n";
	}
	print PLIST "\n";
	print PLIST "\n";
	close(PLIST);
}