#!/bin/bash

#############
# CONSTANTS #
############# 

# base config filename
export KIWI_CONF_NAME="kiwi.conf"
# base install dir 
KIWI_BASEDIR="${HOME}/.cache/kiwi-config"
# repository uri
KIWI_REPO="https://github.com/ldericher/kiwi-config"

########
# MAIN #
########

# use latest version by default
export KIWI_VERSION="master"

# check if pwd is a kiwi folder
if [ -f "./${KIWI_CONF_NAME}" ]; then
    # determine needed kiwi-config version
    export KIWI_VERSION=$(source "./${KIWI_CONF_NAME}" && echo "${VERSION}")
fi

# install if kiwi-config not found
if [ ! -x "${KIWI_BASEDIR}/${KIWI_VERSION}/bin/main.sh" ]; then
    echo -n "Installing kiwi-config v${KIWI_VERSION} into ${KIWI_BASEDIR} ... "

    ### production version ###

    # # switch to temp dir
    # workdir=$(pwd)
    # tmpdir=$(mktemp -d)
    # cd "${tmpdir}"

    # # download archive
    # wget "${KIWI_REPO}/archive/${KIWI_VERSION}.zip"
    # unzip "${KIWI_VERSION}.zip"

    # # read archive version tag
    # cd "kiwi-config-${KIWI_VERSION}"
    # export KIWI_VERSION=$(cat ./version-tag)

    # # install archive
    # mkdir -p "${KIWI_BASEDIR}"
    # mv ./src "${KIWI_BASEDIR}/${KIWI_VERSION}"

    # # discard temp dir
    # cd "${workdir}"
    # rm -rf "${tmpdir}"

    ### development version ###

    # read this version tag
    export KIWI_VERSION=$(cat ./version-tag)

    # install this
    mkdir -p "${KIWI_BASEDIR}"
    ln -s "$(readlink -f ./src)" "${KIWI_BASEDIR}/${KIWI_VERSION}"

    echo "OK"
fi

export KIWI_ROOT="${KIWI_BASEDIR}/${KIWI_VERSION}"

# run main script
exec "${KIWI_ROOT}/bin/main.sh" "${@}"