#!/usr/bin/env bash 


MF_ORIGIN_URL=https://github.com/musescore/muse_framework.git
MF_FORK_URL=""
TASK=""
BRANCH=""

while [[ "$#" -gt 0 ]]; do
    case $1 in
        -f|--fork) MF_FORK_URL="$2"; shift ;;
        -t|--task) TASK="$2"; shift ;;
        -b|--branch) BRANCH="$2"; shift ;;
        *) echo "Unknown parameter passed: $1"; exit 1 ;;
    esac
    shift
done

if [ -z "$MF_FORK_URL" ]; then MF_FORK_URL=$(cat mf_fork_url.local); fi
if [ -z "$MF_FORK_URL" ]; then echo "error: not set MF_FORK_URL"; exit 1; fi

if [ -z "$TASK" ]; then echo "error: not set TASK"; exit 1; fi

echo "MF_FORK_URL: $MF_FORK_URL"
echo "TASK: $TASK"

function use_fork() {
    git submodule set-url muse $MF_FORK_URL
    cd muse
    git remote set-url origin $MF_FORK_URL
    git remote remove upstream
    git remote add upstream $MF_ORIGIN_URL
    git remote -v
    cd ..
}

function use_origin() {
    git submodule set-url muse $MF_ORIGIN_URL
    git submodule update --init
    cd muse
    git remote set-url origin $MF_ORIGIN_URL
    git remote remove upstream
    git remote -v
    cd ..
}

function make_branch() {
    name=$1

    # switch to origin
    use_origin

    # update
    git checkout main 
    git fetch upstream
    git rebase upstream/main
    git submodule update --init --remote ./muse

    # switch to fork
    use_fork

    # create branch
    git checkout -b $name
    cd muse
    git checkout -b $name
    git status 

    cd ..
    git status
}

case "$TASK" in
    "use-fork")
        use_fork
        ;;
    "make-branch")
        if [ -z "$BRANCH" ]; then echo "error: not set BRANCH"; exit 1; fi
        make_branch $BRANCH
        ;;
    "use-origin")
        use_origin
        ;;
    "sync-origin")
        use_origin
        git submodule update --init --remote ./muse
        ;;    
    *) echo "Unknown task: $TASK"; exit 1 ;;
esac

