#!/usr/local/bin/perl
# PB2Makefile
#
#    Copyright (c) 1995  Markus Felten <markus@arlac.rhein-main.de>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Markus Felten <markus@arlac.rhein-main.de>
#
# converts a PB.project file into a Makefile
# usage: PB2Makefile pbfile makefile
# $Id: PB2Makefile,v 1.1 1995/10/27 14:08:02 markus Exp $

require PropertyList;

local($pb,$file,$pl,$filestable);
local($linew);

$linew	= 54;

sub plist {
	local($name,$list,$prefix)=@_;
	local($v,$l,$ls);

	return '' if ! @$list;
	$ls	= ' ' x (length($name) + 1);
	$l	= $ls;
	$str	= $name;
	foreach $v (@$list) {
	  $l += length($v);
	  if($l > $linew) {
	    $str	.= "\\\n$ls$prefix$v";
	    $l = $ls; }
 	  else {
	    $str	.= " $prefix$v"; }
         }

	return $str . "\n\n";
}

sub phash {
	local($name,$hash,$prefix)=@_;
	local($v,$l,$d,$ls);

	$ls	= ' ' x (length($name) + 1);
	$l	= $ls;
	$str	= $name;
	while(($v,$d) = each %$hash) {
	  $l += length($v);
	  if($l > $linew) {
	    $str	.= "\\\n$ls$prefix$v";
	    $l = $ls; }
 	  else {
	    $str	.= " $prefix$v"; }
         }

	return $str . "\n\n";
}

$pb	= shift @ARGV;
$pl	= PropertyList::readFromFileNamed($pb);

$file	= shift @ARGV;
if($file) {
  open(FILE,">$file"); }
else {
  $file	= "Makefile";
  open(FILE,">$file"); }

$filestable	= $pl->{'FILESTABLE'};

print FILE "#\n";
print FILE "# Generated by PB2Makefile form $pb\n";
print FILE "#\n";
print FILE "\n";
print FILE "NAME = " . $pl->{'PROJECTNAME'} . "\n";
print FILE "\n";
print FILE "PROJECTVERSION = " . $pl->{'PROJECTVERSION'} . "\n";
print FILE "LANGUAGE = " . $pl->{'LANGUAGE'} . "\n";
print FILE "\n";
print FILE "APPICON = " . $pl->{'APPICON'} . "\n" if($pl->{'APPICON'});
print FILE phash('LOCAL_RESOURCES =',$pl->{'LOCALIZABLE_FILES'});

push(@l,@{$filestable->{'IMAGES'}});
push(@l,@{$filestable->{'OTHER_RESOURCES'}});

print FILE plist('GLOBAL_RESOURCES =',\@l);
print FILE plist('CLASSES =',$filestable->{'CLASSES'});
print FILE plist('HFILES =',$filestable->{'H_FILES'});
print FILE plist('MFILES =',$filestable->{'M_FILES'});
print FILE plist('SUBPROJECTS =',$filestable->{'SUBPROJECTS'});
print FILE plist('OTHERSRCS =',$filestable->{'OTHER_SOURCES'});


print FILE "MAKEFILEDIR = /NextDeveloper/Makefiles/app\n";

if($pl->{'PROJECTTYPE'} eq 'Palette') {
  $type	= 'palette'; }
elsif($pl->{'PROJECTTYPE'} eq 'Application') {
  $type	= 'app'; }

print FILE "MAKEFILE = $type.make\n";

print FILE "INSTALLDIR = " . $pl->{'INSTALLDIR'} . "\n";
print FILE "INSTALLFLAGS = -c -s -m 755\n";
print FILE "SOURCEMODE = 444\n";
print FILE "\n";
print FILE "\n";
print FILE "\n";

if($type eq 'app') {
  print FILE "ICONSECTIONS =	-sectcreate __ICON app " . $pl->{'APPICON'} . "\n";

  print FILE plist('LIBS =',$filestable->{'OTHER_LIBS'},'-l');
  print FILE 'DEBUG_LIBS = $(LIBS)' . "\n";
  print FILE 'PROF_LIBS = $(LIBS)' . "\n"; }

print FILE "\n";
print FILE "\n";

print FILE "-include Makefile.preamble\n";
print FILE "\n";
print FILE 'include $(MAKEFILEDIR)/$(MAKEFILE)' . "\n";
print FILE "\n";
print FILE "-include Makefile.postamble\n";
print FILE "\n";
print FILE "-include Makefile.dependencies\n";
