#!/bin/sh
set -e -x

# Do nothing for installhdrs
[ "$ACTION" == "installhdrs" ] && exit 0

ver=5.4

# check if we're building for the simulator
if [ "${RC_ProjectName%_Sim}" != "${RC_ProjectName}" ] ; then
	for lib in libform.${ver}.dylib libmenu.${ver}.dylib libncurses.${ver}.dylib libpanel.${ver}.dylib ; do
		install_name_tool -id /usr/lib/${lib} ${DSTROOT}/usr/lib/${lib}
		for lib2 in libform.${ver}.dylib libmenu.${ver}.dylib libncurses.${ver}.dylib libpanel.${ver}.dylib ; do
			install_name_tool -change ${SDKROOT}/usr/lib/${lib} /usr/lib/${lib} ${DSTROOT}/usr/lib/${lib2}
		done
	done
fi

for link in libform.dylib libmenu.dylib libncurses.dylib libpanel.dylib ; do
	ln -s $(basename -s .dylib $link).${ver}.dylib "$DSTROOT/usr/lib/$link"
done

for link in libcurses.dylib libtermcap.dylib ; do
	ln -s libncurses.${ver}.dylib $DSTROOT/usr/lib/$link
done

# rdar://problem/11542731
ln -s libncurses.${ver}.dylib "$DSTROOT"/usr/lib/libncurses.5.dylib

# Create symlinks for TBD files.
TBD_UPPER=`echo ${GENERATE_TEXT_BASED_STUBS} | tr a-z A-Z`
if [ ${TBD_UPPER} = "YES" ] || [ ${TBD_UPPER} = "TRUE" ] || [ ${TBD_UPPER} = "1" ]; then
  for link in libform.tbd libmenu.tbd libncurses.tbd libpanel.tbd ; do
    ln -s $(basename -s .tbd $link).${ver}.tbd "$DSTROOT/usr/lib/$link"
  done
  for link in libcurses.tbd libtermcap.tbd ; do
    ln -s libncurses.${ver}.tbd $DSTROOT/usr/lib/$link
  done
  ln -s libncurses.${ver}.tbd "$DSTROOT"/usr/lib/libncurses.5.tbd
fi
