#! /bin/sh

#####################################
# PREPARE variables:
#
app=$1
app_vcomp="$app"Contents/vcomponents

install_vreports=1

#####################################
# COPY files of vcomponents into APP.
#####################################

if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

# VSHARED
if [ ! -e "$app_vcomp"/libvshared_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvshared_fat_release.dylib 	"$app_vcomp"
fi

# VKERNEL
if [ ! -e "$app_vcomp"/libvkernel_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvkernel_fat_release.dylib 	"$app_vcomp"
fi

# VCLIENT
#if [ ! -e "$app_vcomp"/libvclient_fat_release.dylib ] ; then
#	cp /usr/local/lib/vcomponents/libvclient_fat_release.dylib 	"$app_vcomp"
#fi

# WX (required for VREPORT only)
#if [ ! -e "$app_vcomp"/libwx_mac.dylib ] ; then
#	cp /usr/local/lib/vcomponents/libwx_mac.dylib 				"$app_vcomp"
#fi

# VREPORT
#if [ ! -e "$app_vcomp"/libvreport_fat_release.dylib ] ; then
#	cp /usr/local/lib/vcomponents/libvreport_fat_release.dylib 	"$app_vcomp"
#fi

# VSDK - v1 engine - Copy as recursive folder:
if [ ! -d "$app_vcomp"/VSDK.framework ] ; then
	cp -R /usr/local/lib/vcomponents/VSDK.framework				"$app_vcomp"
fi

# vresources
cp -R /usr/local/lib/vcomponents/vresources 					"$app_vcomp"

#ICU library
cp -R /usr/lib/libicucore.A.dylib 								"$app_vcomp"


#####################################
# CHANGE executable_path for engine dylibs:
#####################################

	# change id of VSHARED
	install_name_tool -id "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvshared_fat_release.dylib

	# change id of VKERNEL
	install_name_tool -id "@executable_path/../vcomponents/libvkernel_fat_release.dylib" "$app_vcomp"/libvkernel_fat_release.dylib

	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib"   "@executable_path/../vcomponents/libvshared_fat_release.dylib" 	"$app_vcomp"/libvkernel_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/VSDK.framework/Versions/A/VSDK" "@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" 	"$app_vcomp"/libvkernel_fat_release.dylib
	install_name_tool -change "/usr/lib/vcomponents/libicucore.A.dylib" 				  "@executable_path/../vcomponents/libicucore.A.dylib" 				"$app_vcomp"/libvkernel_fat_release.dylib

	# change id of VCLIENT
	#install_name_tool -id "@executable_path/../vcomponents/libvclient_fat_release.dylib" "$app_vcomp"/libvclient_fat_release.dylib
	#install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvclient_fat_release.dylib

	# change id of WX
	#install_name_tool -id "@executable_path/../vcomponents/libwx_mac.dylib" "$app_vcomp"/libwx_mac.dylib

	# change id of VREPORT
	#install_name_tool -id "@executable_path/../vcomponents/libvreport_fat_release.dylib" "$app_vcomp"/libvreport_fat_release.dylib
	#install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvreport_fat_release.dylib
	#install_name_tool -change "/usr/local/lib/vcomponents/libwx_mac.dylib" "@executable_path/../vcomponents/libwx_mac.dylib" 							"$app_vcomp"/libvreport_fat_release.dylib


	# change id of VSDK.framework
    install_name_tool -id "@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" "$app_vcomp"/VSDK.framework/Versions/A/VSDK
    

	# change id of ICU dylib
    install_name_tool -id "@executable_path/../vcomponents/libicucore.A.dylib" "$app_vcomp"/libicucore.A.dylib

    
#####################################
# now we need update yet App/Contents/Frameworks/V4RB.rbx_0_xxx.dylib
# REALBasic can assign any digits xxx, so we use globbing (regex) here to locate file.
#####################################
app_frmws="$app"Contents/Frameworks

for file in "$app_frmws"/V4RB*
do
	v4rb_dylib=$file
	break
done

	#
	# now we can change paths In the V4RB.dylib
	#
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" 	"@executable_path/../vcomponents/libvshared_fat_release.dylib" 		"$v4rb_dylib"
	install_name_tool -change "/usr/local/lib/vcomponents/libvkernel_fat_release.dylib" 	"@executable_path/../vcomponents/libvkernel_fat_release.dylib" 		"$v4rb_dylib"
#	install_name_tool -change "/usr/local/lib/vcomponents/libvclient_fat_release.dylib" 	"@executable_path/../vcomponents/libvclient_fat_release.dylib" 		"$v4rb_dylib"
#	install_name_tool -change "/usr/local/lib/vcomponents/libvreport_fat_release.dylib" 	"@executable_path/../vcomponents/libvreport_fat_release.dylib" 		"$v4rb_dylib"

#	install_name_tool -change "/usr/local/lib/vcomponents/VSDK.framework/Versions/A/VSDK" 	"@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" 	"$v4rb_dylib"
