#!/usr/bin/env -S bash -e
# Setup the repository and local system for development

cd "$(dirname "$0")/.."

HELPER=./contrib/ci/fwupd_setup_helpers.py
HELPER_ARGS="-y"

rename_branch() {
    OLD=master
    NEW=main
    if git log $OLD >/dev/null 2>&1 &&
        git remote get-url origin 2>&1 | grep fwupd/fwupd.git >/dev/null 2>&1; then
        echo ""
        read -p "Rename existing $OLD branch to $NEW? (y/N) " question
        if [ "$question" = "y" ]; then
            git branch -m $OLD $NEW
            git fetch origin
            git branch -u origin/$NEW $NEW
            git remote set-head origin -a
        fi
    fi
}

setup_deps() {
    if [ -z "$CI" ]; then
        read -p "Install build dependencies? (y/N) " question
    else
        question=y
    fi
    if [ "$question" = "y" ]; then
        python3 $HELPER install-dependencies $HELPER_ARGS -y
    fi
}

setup_run_wrappers() {
    BASE=../../contrib/
    BIN=venv/bin/
    TEMPLATE=${BASE}/launch-venv.sh
    # launch wrappers
    for F in fwupdtool fwupdmgr fwupd; do
        rm -f ${BIN}/${F}
        ln -s $TEMPLATE ${BIN}/${F}
    done
    # build wrapper
    rm -f ${BIN}/build-fwupd
    ln -s ${BASE}/build-venv.sh ${BIN}/build-fwupd
    rm -f ${BIN}/test-fwupd
    ln -s ${BASE}/test-venv.sh ${BIN}/test-fwupd
}

setup_vscode() {
    # Add default vscode settings and debug launcher
    SOURCED=./contrib/vscode
    TARGETD=./.vscode
    SETTINGS_F=settings.json
    LAUNCH_F=launch.json
    TASK_F=tasks.json
    for f in $SETTINGS_F $LAUNCH_F $TASK_F; do
        TEMPLATE=${SOURCED}/${f}
        TARGETF=${TARGETD}/${f}
        mkdir -p ${TARGETD}
        echo "Copy ${TEMPLATE} to ${TARGETF}."
        cp "${TEMPLATE}" "${TARGETF}"
    done
}

setup_git() {
    if [ -z "$CI" ]; then
        echo "Configuring git environment"
        git config include.path ../.gitconfig
    fi
}

install_pip() {
    package=$1
    args=$2
    if ! python3 -m pip install $package $args; then
        python3 $HELPER install-pip $HELPER_ARGS -y
    fi
    #try once more
    python3 -m pip install $package
}

setup_virtualenv() {
    echo "Setting up virtualenv"
    if ! which virtualenv >/dev/null 2>&1; then
        install_pip virtualenv
    fi
    virtualenv --system-site-packages venv --prompt fwupd
    source venv/bin/activate
    cat >>venv/bin/activate <<EOF
echo "To build or rebuild fwupd within development environment run:"
echo ""
echo "# build-fwupd"
echo ""
echo "To run the test suite run:"
echo ""
echo "# test-fwupd"
echo ""
echo "To run any tool under gdbserver add DEBUG=1 to env, for example:"
echo ""
echo "# DEBUG=1 fwupdtool get-devices"
echo ""
echo "To leave fwupd development environment run:"
echo ""
echo "# deactivate"
. data/bash-completion/fwupdtool
. data/bash-completion/fwupdmgr
export MANPATH=./venv/dist/share/man:
EOF
}

setup_precommit() {
    if [ -z "$CI" ]; then
        echo "Configuring pre-commit hooks"
        install_pip pre-commit
        pre-commit install
    fi
}

setup_prepush() {
    if [ -z "$CI" ]; then
        echo ""
        read -p "Run tests locally before pushing to remote branches? THIS WILL SLOW DOWN EVERY PUSH but reduce the risk of failing CI. (y/N) " question
    else
        question=y
    fi
    if [ "$question" = "y" ]; then
        pre-commit install -t pre-push
    else
        pre-commit uninstall -t pre-push
    fi
}

check_markdown() {
    python3 $HELPER test-markdown
}

check_jinja2() {
    python3 $HELPER test-jinja2
}

check_meson() {
    python3 $HELPER test-meson
}

detect_os() {
    for i in "$@"; do
        case $i in
        --os=*)
            OS="${i#*=}"
            shift
            ;;
        --debug)
            DEBUG=1
            shift
            ;;
        *) ;;
        esac
    done
    if [ -z $OS ]; then
        OS=$(python3 $HELPER detect-profile)
        if [ -z "$OS" ]; then
            install_pip distro
            OS=$(python3 $HELPER detect-profile)
        fi
        echo "Using OS profile $OS to setup"
    fi
    if [ -n "$OS" ]; then
        HELPER_ARGS="$HELPER_ARGS --os $OS"
    fi
    if [ -n "$DEBUG" ]; then
        set -x
        HELPER_ARGS="$HELPER_ARGS --debug"
    fi
}

howto() {
    echo ""
    echo "To enter fwupd development environment run:"
    echo ""
    echo "# source venv/bin/activate"
    echo ""
}

#already setup
if [ -f venv/bin/build-fwupd ]; then
    echo "$0 has already been run"
    howto
    exit 0
fi

PYTHON_VERSION=$(python3 --version 2>/dev/null)
if [ -z "$PYTHON_VERSION" ]; then
    echo "Install python3 to run this script" >&2
    exit 1
fi

#needed for arguments for some commands
detect_os "$@"

#if interactive install build deps and prepare environment
if [ -t 2 ]; then
    case $OS in
    debian | ubuntu | arch | fedora | darwin | freebsd)
        setup_deps
        ;;
    esac
    rename_branch
fi
setup_virtualenv
setup_run_wrappers
check_markdown
check_jinja2
setup_vscode
setup_git
check_meson
setup_precommit

#needs to be after pre-commit is sourced
if [ -t 2 ]; then
    setup_prepush
fi

howto
