#!/bin/csh
# Make a port GNU-Darwin-like

if ( -f pkg-plist.old ) then
	goto patch-fix
endif
echo Darwinizing...
echo 'removing shared libraries from packing list'
grep -v 'lib\/lib' pkg-plist>dplist1
grep 'lib\/lib' pkg-plist | grep -v '\.so' > dplist2
echo 'adding any dynamic libraries to packing list'
grep 'lib\/lib' pkg-plist | sed s/'\.a'/'.*'/ | grep -v '\.so' | awk '{ print "/bin/ls -1 /usr/X11R6/"$1"|/usr/bin/sed s/'.usr.X11R6.'//| grep dylib >>dplist2" }' > getlibs
source getlibs
grep 'lib\/lib' pkg-plist | sed s/'\.a'/'.*'/ | grep -v '\.so' | awk '{ print "/bin/ls -1 /usr/local/"$1"|/usr/bin/sed s/'.usr.local.'//| grep dylib >>dplist2" }' > getlibs
source getlibs 
grep -v '\.so' dplist2  >>dplist1
#grep  '\.so' dplist2 |\
#grep -v "\.so.." |\
#sed s/'\.so'/'.a'/ >>dplist1
cp pkg-plist pkg-plist.old
sed s/'%%RUBY_SITELIBDIR%%'/'lib\/ruby\/site_ruby\/1.6'/ dplist1 |\
sed s/'%%RUBY_DOCDIR%%'/'share\/doc\/ruby'/ |\
sed s/'%%RUBY_ARCHLIBDIR%%'/'lib\/ruby\/1.6\/powerpc-darwin6.3'/ |\
sed s/'%%RUBY_LIBDIR%%'/'lib\/ruby\/1.6'/ |\
sed s/'%%_RUBY_SUFFIX%%'/'16'/ |\
sed s/'%%RUBY_NAME%%'/'ruby'/ |\
sed s/'%%RUBY_ELISPDIR%%'/'lib\/ruby\/elisp'/ |\
sed s/'%%PERL_VER%%'/'5.6.0'/ |\
sed s/'%%PERL_ARCH%%'/'darwin'/ |\
sed s/'%%GNOME:%%'/''/ |\
sed s/'%%DOC%%'/''/ |\
sed s/'%%X11%%'/''/ |\
sed s/'%%LOCALBASE%%'/''/ |\
sed s/'%%X11BASE%%'/''/ |\
sed s/'%%INFODIR%%'/'info'/ |\
sed s/'%%DATADIR%%'/'share'/ |\
sed s/'%%RUBY_EXAMPLESDIR%%'/'share\/examples\/ruby\/gdk_pixbuf'/ |\
sed s/'%%RUBY_SITEARCHLIBDIR%%'/'lib\/ruby\/site_ruby\/1.6\/powerpc-darwin6.3'/ |\
sed s/'%%PYTHON_VERSION%%'/'python2.2'/ |\
sed s/'%%PYTHON_SITELIBDIR%%'/'lib\/python2.2\/site-packages'/ |\
sed s/'%%PORTDOCS%%'// |grep -v 'ruby\/gdk_pixbuf' > dplist3
grep -v 'lib\/ruby' dplist3 |grep -v 'ruby\/gdk_pixbuf' |\
grep -v 'lib\/perl' > pkg-plist
grep 'lib\/ruby' dplist3| sed s/'\.so'/'.bundle'/>> pkg-plist
grep 'lib\/perl' dplist3| sed s/'\.so'/'.bundle'/>> pkg-plist
rm dplist1 dplist2 getlibs dplist3
rm -f work/.PLIST.mktmp

patch-fix:
if ( -e .patch-fixed ) then
	goto statickize
endif
if ( -e files ) then
	echo Patching the patches
	foreach file ( files/* ) 
		cat $file | sed 's/__FreeBSD__/__APPLE__/g' > $file.tmp
		cat $file.tmp | sed -e 's/+[a-zA-Z0-9/$_-]*ltconfig/+#/' > $file
		rm $file.tmp
	end
	touch .patch-fixed
endif

statickize:
if ( -f Makefile.old ) then
	exit
endif
echo "Darwininzing Makefile..."
cp Makefile Makefile.old
grep -v CONFIGURE_TARGET Makefile.old | sed -e 's/CPPFLAGS="/CPPFLAGS="-no-cpp-precomp /' > Makefile
#echo "removing WRKSRC..."
#rm -rf work
