#!/bin/bash

BUILD_DIR="${1}"
if [ -n "${BUILD_DIR}" ]; then
    BUILD_DIR=`(cd "${BUILD_DIR}"; pwd)`
fi

INSTALL_DIR="${2}"
if [ -n "${INSTALL_DIR}" ]; then
    INSTALL_DIR=`(cd "${INSTALL_DIR}"; pwd)`
fi

echo BUILD DIR: "${BUILD_DIR}"
echo INSTALL DIR: "${INSTALL_DIR}"

if [ ! -e "${BUILD_DIR}" -o -z "${BUILD_DIR}" -o \
     ! -e "${INSTALL_DIR}" -o -z "${INSTALL_DIR}" ]; then
    echo USAGE: ${0} build_dir install_dir
    echo AS IN: ${0} ~/crcns/build ~/crcns
    echo OR IN: ${0} /Developer/crcns /Developer/crcns
    exit 1;
fi

#----------------------------------------------------------------
# TS
# 
# global time stamp variable
# 
export TS=`date +"%Y%m%d-%H%M"`
export OS=`system_profiler SPSoftwareDataType | grep 'System Version' | cut -d: -f2  | cut -dX -f2 | cut -d' ' -f2`

#----------------------------------------------------------------
# get_arch
# 
# $1 -- filepath
# 
get_arch()
{
    FILE="${1}"
    A=""
    for i in `file ${FILE} | grep 'executable' | rev | cut -d' ' -f1 | rev`; do
	A="${A}-${i}"
    done
    echo "${A}"
}

#----------------------------------------------------------------
# quiet_pushd
# 
# $1 -- directory path
# 
quiet_pushd()
{
    DIR="${1}"
    
    pushd "${DIR}" >> /dev/null
    ERR=$?
    if [ ${ERR} != 0 ]; then
	echo ERROR: "could not pushd ${DIR}, error code: ${ERR}"
	exit ${ERR}
    fi
    
    echo PWD: $PWD
    return ${ERR}
}

#----------------------------------------------------------------
# quiet_popd
# 
quiet_popd()
{
    popd >> /dev/null
    ERR=$?
    
    return ${ERR}
}

#----------------------------------------------------------------
# build_libs
# 
build_libs()
{
    echo
    echo build_libs

    quiet_pushd "${BUILD_DIR}"/the
    make -j2 install
    ERR=$?
    if [ ${ERR} != 0 ]; then 
	echo "ERROR: build failed, error code: ${ERR}"
	exit ${ERR}
    fi;
    
    quiet_popd
    return ${ERR}
}

#----------------------------------------------------------------
# build_tools
# 
build_tools()
{
    echo
    echo build_tools

    quiet_pushd "${BUILD_DIR}"/ir-tools
    make -j2 install
    
    ERR=$?
    if [ ${ERR} != 0 ]; then 
	echo "ERROR: build failed, error code: ${ERR}"
	exit ${ERR}
    fi;

    quiet_popd
    quiet_pushd "${INSTALL_DIR}"

    APP_ARCH=`get_arch bin/ir-assemble`
    ERR=$?
    if [ ${ERR} != 0 ]; then
	echo "ERROR: could not determine architecture of bin/ir-assemble, error code: ${ERR}"
	exit ${ERR}
    fi;
    
    echo strip bin/ir-*
    strip bin/ir-*
    
    echo zip -yr9 ~/Desktop/ir-tools-OSX-"${OS}${APP_ARCH}-${TS}".zip bin/ir-*
    zip -yr9 ~/Desktop/ir-tools-OSX-"${OS}${APP_ARCH}-${TS}".zip bin/ir-*
    ERR=$?
    quiet_popd
    
    return ${ERR}
}

#----------------------------------------------------------------
# resolve_symlink
# 
# $1 -- file path
# 
resolve_symlink()
{
    SRC="${1}"
    DST=`readlink "${SRC}"`
    if [ -z "${DST}" ]; then
	echo "${SRC}"
    else
        REL_DIR=`dirname "${SRC}"`
	ABS_DIR=`(cd "${REL_DIR}"; pwd)`
	resolve_symlink "${ABS_DIR}/${DST}"
    fi
}

#----------------------------------------------------------------
# deploy_app
# 
# $1 -- app name
# $2 -- file to deploy
# 
deploy_app()
{
    APP="${1}"
    BIN="${2}"
    
    echo
    echo deploy_app \"${APP}\" \"${BIN}\", pwd: $PWD
    
    APP_PATH="${APP}.app/Contents/MacOS"
    APP_EXEC="${APP_PATH}/${APP}"
    otool -L "${BIN}" \
	| grep -v "${APP_EXEC}" \
	| grep -v "${BIN}" \
	| grep -v /lib/Qt \
	| grep -v /Frameworks/Qt \
	| grep -v /System/Library \
	| grep -v /usr/lib \
	| rev \
	| cut -d'(' -f2 \
	| rev \
	| while read i; do
	RELATIVE=`echo "${i}" | grep @executable_path/ | cut -d/ -f2-`
	FRAMEWORK=`echo "${i}" | grep '\.framework/'`
	echo
	if [ -n "${RELATIVE}" ]; then
	    echo RELATIVE: ${RELATIVE}
	    # find the source of this dependency:
	    SRC=/`echo "${RELATIVE}" | cut -d/ -f2-`
	    if [ ! -e "${SRC}" ]; then
		echo "ERROR: does not exist: ${SRC}"
		exit 1;
	    fi
	    
	    DST=`dirname "${APP_PATH}/..${SRC}"`
	    if [ ! -e "${DST}" ]; then
 		echo mkdir -p "${DST}"
		mkdir -p "${DST}"
	    fi
	    
	    FN=`basename "${SRC}"`
	    if [ ! -e "${DST}/${FN}" ]; then
		echo cp -f "${SRC}" "${DST}/${FN}"
		cp -f "${SRC}" "${DST}/${FN}"
		deploy_app "${APP}" "${DST}/${FN}"
	    fi
	else
	    echo ABSOLUTE: ${i}
	    BIN_ROOT="${APP}.app/Contents"
	    DST="Frameworks"
	    if [ -z "${FRAMEWORK}" ]; then
		DST="Auxiliaries"
	    fi
	    
	    FN_DST=""
	    VERSIONS=`echo "${i}" | grep /Versions/`
	    if [ -n "${VERSIONS}" ]; then
		FN_DST=`echo "${i}" | rev | cut -d/ -f-4 | rev`
	    else
		FN_DST=`echo "${i}" | rev | cut -d/ -f-2 | rev`
	    fi
	    
	    DST_DIR=`dirname "${BIN_ROOT}/${DST}/${FN_DST}"`
	    
	    if [ ! -e "${DST_DIR}" ]; then
		echo mkdir -p "${DST_DIR}"
		mkdir -p "${DST_DIR}"
	    fi
	    
	    FILE="${BIN_ROOT}/${DST}/${FN_DST}"
	    NAME="@executable_path/../${DST}/${FN_DST}"
	    
	    if [ ! -e "${FILE}" ]; then
		echo cp "${i}" "${FILE}"
		cp "${i}" "${FILE}"

		echo install_name_tool -id "${NAME}" "${FILE}"
		install_name_tool -id "${NAME}" "${FILE}"

		echo install_name_tool -change "${i}" "${NAME}" "${BIN}"
		install_name_tool -change "${i}" "${NAME}" "${BIN}"

		deploy_app "${APP}" "${FILE}"
	    fi
	fi
    done
}

#----------------------------------------------------------------
# build_app
# 
# $1 -- app name
# 
build_app()
{
    APP="${1}"

    echo
    echo build_app ${APP}

    quiet_pushd "${BUILD_DIR}"/"${APP}";
    rm -rf "${APP}".app
    make -j2
    
    ERR=$?
    if [ ${ERR} != 0 ]; then 
	echo "ERROR: ${APP} build failed, error code: ${ERR}"
	exit ${ERR}
    fi;
    APP_EXEC="${APP}.app/Contents/MacOS/${APP}"
    APP_ARCH=`get_arch "${APP_EXEC}"`
    ERR=$?
    if [ ${ERR} != 0 ]; then
	echo "ERROR: could not determine architecture of ${APP_EXEC}, error code: ${ERR}"
	exit ${ERR}
    fi;

    strip "${APP_EXEC}"
    deploy_app "${APP}" "${APP}.app/Contents/MacOS/${APP}"
    macdeployqt "${APP}".app -dmg
    mv "${APP}".dmg ~/Desktop/"${APP}"-OSX-"${OS}${APP_ARCH}-${TS}".dmg
    ERR=$?
    quiet_popd

    return ${ERR}
}

build_libs && \
build_app ir-tweak && \
build_app ir-mosaic && \
build_app iv && \
build_tools
