#!/usr/bin/env bash
#
#   Common utils for goto scripts in bash
#   
#########################################


function prompt {
    # Asks a y|n question and returns true or false.
    local question="$1"
    local REPLY

    read -p "$question" REPLY
    if [[ $REPLY =~ ^[Yy.*]$ ]]; then
        return 0
    else
        return 1
    fi
}


function check_status {
    # Checking exit status of prev cmd:
    # see  https://stackoverflow.com/questions/26675681/how-to-check-the-exit-status-using-an-if-statement-using-bash
    local exit_status="$?"
    if [ $exit_status -ne 0 ]; then
        echo "ERROR: Installation step failed with exit code $exit_status"
        if [[ "$#" -ne 2 ]]; then # pass an argument to this and we won't exit
            exit $exit_status
        else
            return $exit_status
        fi
    fi
}

function load_gotopath {
    # GOTOPATH is global
    if [[ -z "$GOTOPATH" ]]; then
        GOTOPATH="${HOME}/.goto"
    fi
}

function find_rc_file {
    local INVOKING_SHELL
    local RCFILE
    INVOKING_SHELL="$1"

    # supports ~/.bashrc ~/.bash_profile and ~/.zshrc
    if [[ "$INVOKING_SHELL" == *zsh* ]]; then
        RCFILE="${HOME}/.zshrc"

    elif [ -n "$BASH_VERSION" ]; then

        if [ -f "${HOME}/.bash_profile" ]; then
            RCFILE="${HOME}/.bash_profile"
        elif [ -f "${HOME}/.bashrc" ]; then
            RCFILE="${HOME}/.bashrc"
        else
            echo "Ah hoy! Failed to determine rcfile. please install manually" >&2
            return 1
        fi
    else
        echo "Error: only supporting ZSH and BASH" >&2
        return 1
    fi

    echo "$RCFILE"
}


function write_init_command_to_rcfile {
    # Handles writing the right thing to rcfile
    local INVOKING_SHELL
    local RCFILE
    INVOKING_SHELL="$1"
    RCFILE=$(find_rc_file "$INVOKING_SHELL")

    if [ -z "$RCFILE" ]; then
        echo "Error finding RCfile" >&2 && exit 1
    fi

    if prompt "Add goto startup script to $RCFILE? [y|n]: "; then
        echo
        echo -e "# GOTO" >> "$RCFILE" || check_status
        echo -e "if start_goto 2>/dev/null; then" >> "$RCFILE" || check_status
        echo -e "  source start_goto" >> "$RCFILE" || check_status
        echo -e "fi" >> "$RCFILE" || check_status
    else
        echo   
        echo "If you want to do this manually add this to your $RCFILE:" >&2
        echo "  if start_goto 2> /dev/null; then" >&2
        echo "      source start_goto" >&2
        echo "  fi" >&2
        exit 1
    fi

}


function create_goto_folders {
    local FOLDER="$1"

    if [[ -z "$FOLDER" ]]; then
        echo "Fatal error: GOTOPATH is empty (install directory)" >&2
        exit 1
    fi

    if [[ ! -d "$FOLDER" ]]; then
        mkdir "${FOLDER}" || check_status
        mkdir "${FOLDER}/projects" || check_status
        touch "${FOLDER}/active-project" || check_status

        echo "Created goto folder in '${FOLDER}'"
    fi
}


function detect_unmigrated_data {
    jfiles=(`find "$GOTOPATH/projects" -maxdepth 1 -type f -name "*.json"`)
    
    if [ ${#jfiles[@]} -eq 0 ]; then 
        # no jfiles, so not detecting unmigrated data
        return 1
    else
        return 0
    fi
}

