#!/bin/csh

# Attempt to find the architecture.
# First look through the command line args.
set arch=unknown
set link_cmd=(`cat link_command`)
while ( $#link_cmd > 0 )
	if ( "$link_cmd[1]" == "-arch" ) then
		set arch=$link_cmd[2]
	endif
	shift link_cmd
end

# look for an explicit arch file
if ( "$arch" == "unknown" ) then
	if ( -e arch ) then
		set arch=`cat arch`
	endif
endif

if ( "$arch" == "unknown" ) then
echo "***** Unable to determine architecture."
exit 1
endif 

# Create .dylibs for each file in the dylib_stubs directory.
if ( -e dylib_stubs ) then
	set files=`cd dylib_stubs ; echo *`
	mkdir -p dylibs
	foreach file ($files)
		if ( ! -e dylibs/$file ) then
			clang -arch $arch -c -fno-builtin -o tmp_object.o -x c dylib_stubs/$file
			ld -arch $arch -dylib -macosx_version_min 10.1 -no_version_load_command -o dylibs/$file tmp_object.o
		endif
	end
endif

# Create .frameworks for each file in the framework_stubs directory.
if ( -e framework_stubs ) then
	set files=`cd framework_stubs ; echo *`
	foreach file ($files)
		if ( ! -e frameworks/$file.framework ) then
			clang -arch $arch -c -fno-builtin -o tmp_object.o -x c framework_stubs/$file
			mkdir -p frameworks/$file.framework
			ld -arch $arch -dylib -macosx_version_min 10.1 -no_version_load_command -o frameworks/$file.framework/$file tmp_object.o
		endif
	end
endif

# Clean up.
rm -f tmp_object.o
