#!/bin/bash

# This script primarily grabs the version number from xLightsVersion.h or xlights_build_version.h
# and uses that to set the CFBundleVersion strings that are needed for the app store upload.
# This really just prevents having to adjust the xcodeproject version numbers every build


SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
if [ -f "${SCRIPTDIR}/../xLights/xLights/xlights_build_version.h" ]; then
    XLVERSION=$(grep xlights_version_string ${SCRIPTDIR}/../xLights/xLights/xlights_build_version.h | tr "\"" "\n" | grep 20)
else
    XLVERSION=$(grep xlights_version_string ${SCRIPTDIR}/../xLights/xLights/xLightsVersion.h | tr "\"" "\n" | grep 20)
fi

# iff need to manually force a version number
#XLVERSION=2020.46.1

SHORTVER="${XLVERSION//./0}"
SHORTVERPFX=${SHORTVER:2:5}
SHORTVERPO=${SHORTVER:8}

if [ "x${SHORTVERPO}" == "x" ]; then
    SHORTVERPO="0"
fi
SHORTVER="${SHORTVERPFX}${SHORTVERPO}"

/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $XLVERSION" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $SHORTVER" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"


#if [ -z "${CODE_SIGN_IDENTITY}" ] ; then
#    exit 0
#fi

#if [ "$CONFIGURATION" != "Debug" ]; then
#    cd $TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/Frameworks
#    ls -1 *.dylib | xargs -n 1  /usr/bin/codesign  -f -v -s "$EXPANDED_CODE_SIGN_IDENTITY"
#fi


