#!/bin/sh
#
# Version 1.0
# 2007-01-10
# Hanspeter Niederstrasser
#
# Link to the native OSX-Thread:
# http://dcgui.berlios.de/forum/viewtopic.php?t=1478
#
# based on original install script from ravemax,
# and NYZeta's HowTo at
# http://www.stud.ntnu.no/~chrisj/download/howto/valknut_howto.html
#
# Here is a script that creates an .app after "make all" 
# no "make install" is required:
#

QTMACDIR=${QTDIR}
TS_TO_QM=${QTDIR}/bin/lrelease 

if [ ! -x "${TS_TO_QM}" ]; then
	echo "Couldn't find >> lrelease << to prepare the translation files."
	echo "Are you sure ${TS_TO_QM} is the correct location?"
	echo "Also check that \$QTDIR points to where it should."
	echo "This script thinks \$QTDIR = ${QTDIR}."
	echo ""
	echo "Exiting.  Fix the above error and try again."
	exit 1
fi

PROG_NAME=valknut
BUNDLE_NAME=Valknut
APP_DIR=${BUNDLE_NAME}.app

APP_VERSION=0.3.8
APP_SERIAL=20061213

TS_DIR=${PROG_NAME}/ts
TRANS_TARGET=${APP_DIR}/translation

SOUNDS_DIR=${PROG_NAME}/sounds
SOUNDS_TARGET=${APP_DIR}/sounds

PLUGIN_DIR=${PROG_NAME}/plugin
PLUGIN_TARGET=${APP_DIR}/plugin

ICONS_DIR=${PROG_NAME}/icons
ICONS_TARGET=${APP_DIR}/icons

CNT_TARGET=${APP_DIR}/Contents

MACOS_TARGET=${CNT_TARGET}/MacOS
RSC_TARGET=${CNT_TARGET}/Resources

DMG_TARGET="${BUNDLE_NAME} ${APP_VERSION}"

# Build .app bundle
echo "Creating application bundle       ${APP_DIR}"
/usr/bin/install -d -m 755 ${APP_DIR}

# Translation files 
echo "Creating translations target      ${TRANS_TARGET}"
/usr/bin/install -d -m 755  ${TRANS_TARGET}

echo "Copying translations from         ${TS_DIR}"
for i in ${TS_DIR}/*.ts; do 
	${TS_TO_QM} $i; 
	/bin/mv ${TS_DIR}/$(basename $i ts)qm ${TRANS_TARGET}
done 
echo "Installed translation files to    ${TRANS_TARGET}"

# Sounds
echo "Creating sounds target            ${SOUNDS_TARGET}"
/usr/bin/install -d -m 755  ${SOUNDS_TARGET}/default

# Plugin
echo "Creating plugins target           ${PLUGIN_TARGET}"
/usr/bin/install -d -m 755  ${PLUGIN_TARGET}

# Icons
echo "Creating icons target             ${ICONS_TARGET}"
/usr/bin/install -d -m 755 ${ICONS_TARGET}/appl/default
/usr/bin/install -d -m 755 ${ICONS_TARGET}/emot/default
/usr/bin/install -d -m 755 ${ICONS_TARGET}/user/default

echo "Copying icons from                ${ICONS_DIR}"
/usr/bin/install -m 644 ${ICONS_DIR}/*.xpm ${ICONS_TARGET}/appl/default
/usr/bin/install -m 644 ${ICONS_DIR}/*.xml ${ICONS_TARGET}/appl/default

/bin/mv ${ICONS_TARGET}/appl/default/emoticons.xml ${ICONS_TARGET}/emot/default
/bin/mv ${ICONS_TARGET}/appl/default/emoticons.xpm ${ICONS_TARGET}/emot/default

/bin/mv ${ICONS_TARGET}/appl/default/usericons.xml ${ICONS_TARGET}/user/default
/bin/mv ${ICONS_TARGET}/appl/default/usericons.xpm ${ICONS_TARGET}/user/default

# Contents 
echo "Creating Contents target          ${CNT_TARGET}"
/usr/bin/install -d -m 755 ${CNT_TARGET}

echo "Installing Info.plist."
/usr/bin/sed -e "s|%VERS%|$APP_VERSION|g;s|%SERIAL%|$APP_SERIAL|g" Info.plist.in > ${CNT_TARGET}/Info.plist

echo "Installing PkgInfo."
/usr/bin/install -m 644 PkgInfo.in ${CNT_TARGET}/PkgInfo

echo "Creating MacOS target             ${MACOS_TARGET}"
/usr/bin/install -d -m 755 ${MACOS_TARGET}

echo "Copying executable from           ${PROG_NAME}/"
/usr/bin/install -m 755 ${PROG_NAME}/${PROG_NAME} ${MACOS_TARGET}/${PROG_NAME}

echo "Stripping executable              ${PROG_NAME}"
/usr/bin/strip ${MACOS_TARGET}/${PROG_NAME}

# Copy the .dylibs that are linked by the program
# This loop is only needed if you want to build a standalone ,app.
echo "Copying the shared libraries to   ${MACOS_TARGET}"
for i in `otool -L ${MACOS_TARGET}/${PROG_NAME} | grep version | cut -f 1 -d ' ' | grep -v \/System\/Library | grep -v \/usr\/lib` ; do
	/bin/cp -p ${i} ${MACOS_TARGET};
	echo "Copied `basename ${i}`"
	/usr/bin/install_name_tool -change ${i} @executable_path/`basename ${i}` ${MACOS_TARGET}/${PROG_NAME};
	# now check paths for other libraries linked by the copied libraries.
	for j in `otool -L ${i} | grep version | cut -f 1 -d ' '| grep -v ${i} | grep -v \/System\/Library | grep -v \/usr\/lib` ; do
		/usr/bin/install_name_tool -change ${j} @executable_path/`basename ${j}` ${MACOS_TARGET}/`basename ${i}`
	done
done

# Copy .APP Icon
echo "Creating Resources target         ${RSC_TARGET}"
/usr/bin/install -d -m 755 ${RSC_TARGET}

echo "Copying icon from                 ${ICONS_DIR}"
/usr/bin/install -m 644 ${ICONS_DIR}/icon_128x128.icns ${RSC_TARGET}/${BUNDLE_NAME}.icns

# Make the distribution disk image
echo "Making the final disk image."
/usr/bin/hdiutil create -type SPARSE -size 20m -fs HFS+ -volname "${DMG_TARGET}" -attach ${PROG_NAME}-${APP_VERSION}.sparseimage

#/usr/bin/hdiutil attach ${PROG_NAME}-${APP_VERSION}.sparseimage

echo "Copying files into the disk image."

/bin/cp -R ${BUNDLE_NAME}.app /Volumes/"${DMG_TARGET}"
/bin/cp ${PROG_NAME}/icons/ValknutVolumeIcon.icns /Volumes/"${DMG_TARGET}"/.VolumeIcon.icns
/Developer/Tools/SetFile -a C /Volumes/"${DMG_TARGET}"/.VolumeIcon.icns /Volumes/"${DMG_TARGET}"

/usr/bin/hdiutil unmount /Volumes/"${DMG_TARGET}"

/usr/bin/hdiutil convert -imagekey zlib-level=9 -format UDZO ${PROG_NAME}-${APP_VERSION}.sparseimage -o ${PROG_NAME}-${APP_VERSION}.dmg

/bin/rm -f ${PROG_NAME}-${APP_VERSION}.sparseimage

echo ""
echo "Done building a distributable disk image for ${BUNDLE_NAME}."
echo "The disk image can be found at the root of the"
echo "build directory `pwd`"
echo "as '${PROG_NAME}-${APP_VERSION}.dmg'."
