# build pk3 on the fly

Import qw( INSTALL_DIR BUILD_DIR CONFIG_DIR CC CXX LINK );

use Data::Dumper;

$env = new cons(); # the env on which we will be working for all pk3s (NOTE: maybe we need several ctually)

$hcf_do_exec = 1;
sub do_command($)
{
  printf("@_[0]\n");
  if ($hcf_do_exec)
  {
    system("@_[0]");
  }
}

sub build_pk3 {

	sub launch {
		#print "In launch\n";		
		$Data::Dumper::Indent = 2;
		#print Dumper(@_);
		$tmpdir = "/tmp/pk3-builder$$";
		do_command("rm -rf $tmpdir");
		
		($target, $sets) = @_;
		$base=`basename $target`; chomp($base);
		$dirname=`dirname $target`; chomp($dirname);
		
		foreach (@{$sets})
		{
			($sourcepath, $destpath, $file) = @{$_};
			#print "source: $sourcepath dest: $destpath file: $file\n";
			do_command("mkdir -p $tmpdir/$destpath");
			if ($sourcepath =~ /#.*/)
			{
				#print "$sourcepath is absolute\n";
				$sourcepath =~ s/#//;
				if (ref($file))
				{
					foreach(@{$file})
					{
						do_command("cp $sourcepath/$_ $tmpdir/$destpath/$_");
					}
				}
				else
				{
					do_command("cp $sourcepath/$file $tmpdir/$destpath/$file");
				}
			}
			else
			{
				#print "$sourcepath in linked dir\n";
				if (ref($file))
				{
					foreach(@{$file})
					{
						do_command("cp $BUILD_DIR/$sourcepath/$_ $tmpdir/$destpath/$_");
					}
				}
				else
				{
					do_command("cp $BUILD_DIR/$sourcepath/$file $tmpdir/$destpath/$file");
				}
			}			
		}
		
		do_command("cd $tmpdir ; zip -r $base *");
		do_command("mkdir -p $BUILD_DIR/$dirname");
		do_command("cp $tmpdir/$base $BUILD_DIR/$target");
		do_command("rm -rf $tmpdir");
		
		return 1;
	}
	
	# extract the parameters
	($target, $sets) = @_;
	
	$base=`basename $target`; chomp($base);
	$dirname=`dirname $target`; chomp($dirname);
	
	# the build command is stored and called later on by cons
	# this makes it impossible to have several build_pk3 working together
	# there is probably a cleaner solution than this hack, but this works
	$target_uniquename="target_$base";
	$target_uniquename=~s/\.//g;
	eval("\$$target_uniquename=\$target");
	$sets_uniquename="sets_$base";
	$sets_uniquename=~s/\.//g;
	eval("\$$sets_uniquename=\$sets");
	#print "name: $target_uniquename after the hack: $target_pak8pk3";
	
	# don't pass @{@_} .. since this will be called during the process
	$command = "[perl] &launch( \$$target_uniquename, [ \@{\$$sets_uniquename} ] )";
	#print "$command\n";
		
	foreach(@{$sets})
	{
		($sourcepath, $destpath, $file) = @{$_};
		if (ref($file))
		{
			foreach(@{$file})
			{
				Depends $env $target, $sourcepath . '/' . $_;
			}
		}
		else
		{
			Depends $env $target, $sourcepath . '/' . $file;
		}
	}
	Command $env $target, $command;
	Install $env $INSTALL_DIR . "/$dirname", $target;
}

# quick rundown on the syntax:
# <target file>, [ <fileset1>, <fileset2>, ... ]
# where <fileset>: [ <source directory>, <directory in zip>, <file1>, <file2>, .. ]

build_pk3('baseq3/pak8.pk3', 
	[ [ '#../../../media/Q3Media-1.32/baseq3/menu/art', 'menu/art', 'pblogo.tga' ],
	[ '../Q3/q3_ui/q3_ui', 'vm', 'ui.qvm' ],
	[ '../Q3/cgame/cgame', 'vm', 'cgame.qvm' ],
	[ '../Q3/game/game', 'vm', 'qagame.qvm' ] ]
);

build_pk3('missionpack/pak3.pk3',
	[	[ '#../ui', 'ui', [ 'createserver.menu', 'joinserver.menu', 'filter.menu', 'punkbuster.menu', 'menus.txt' ] ],
	[ '../TA/ui/ui', 'vm', 'ui.qvm' ],
	[ '../TA/cgame/cgame', 'vm', 'cgame.qvm' ],
	[ '../TA/game/game', 'vm', 'qagame.qvm' ],
	[ '#../../../media/Q3Media-1.32/missionpack/scripts', 'scripts', 'models2.shader' ] ]
);
