#
#    Project Name    : Visual Python
#    Description     : GUI-based Python code generator
#    File Name       : visualpy
#    Author          : Black Logic - LJ
#    Note            : Control Visual Python for Mac/Linux
#    License         : GPLv3 (GNU General Public License v3.0)
#    Date            : 2021. 08. 14
#    Change Date     :
#

#=============================================================================
# Check Arguments
#   - set VP_OPTION & PIP_T
#=============================================================================
if [ $# -eq 1 ]; then
    VP_OPTION=$1
elif [ $# -eq 2 ]; then
    ARG1=$(echo $1 | cut -c1-2)
    if [ ${ARG1} = "--" ]; then
        VP_OPTION=$2
        PIP_T=$(echo $1 | cut -c3-)
    else
        VP_OPTION=$1
        PIP_T=$(echo $2 | cut -c3-)
    fi  
else
    VP_OPTION=''
fi

#=============================================================================
# Set variable
#=============================================================================
PIP=pip
which ${PIP_T} > /dev/null 2>&1 && PIP=${PIP_T}

JP_NB='jupyter nbextension'

VP_NAME='visualpython'
VP_BIND='visualpython/src/main'

PIP_UNINST=${PIP}' uninstall '${VP_NAME}
PIP_UPGRAD=${PIP}' install '${VP_NAME}' --upgrade'

#=============================================================================
# main function
#=============================================================================
f_main() {
    echo "Package install command: ${PIP}"
    
    case ${VP_OPTION} in
        enable    | -E  | -e  | \
        disable   | -D  | -d  | \
        install   | -I  | -i  | \
        uninstall | -UN | -un | \
        upgrade   | -UP | -up)
            PATH_SRC=`f_get_string_pipshow Location`
            PATH_DST=`f_get_extension_path` ;;
    esac

    case ${VP_OPTION} in
        enable    | -E  | -e)  f_enable ;;
        disable   | -D  | -d)  f_disable ;;
        install   | -I  | -i)  f_install ;;
        uninstall | -UN | -un) f_uninstall ;;
        upgrade   | -UP | -up) f_upgrade ;;
        version   | -V  | -v)  f_version ;;
        help      | -H  | -h)  f_help ;;
        *)                     f_help;;
    esac
}

#=============================================================================
# Install Visual Python
#=============================================================================
f_install() {
    RES=`f_check_extension`
    # 1 = Jupyter Extension is not actived
    # 2 = visualpython does not exist
    # 3 = visualpython exists

    mkdir -p ${PATH_DST}/${VP_NAME}

    if [ ${RES} -eq 1 ]; then
        f_print_not_extension
    elif [ ${RES} -eq 2 ]; then
        f_copy_files
        f_enable
    elif [ ${RES} -eq 3 ]; then
        # overwrite
        f_print_line1
        echo "Already exists Visual Python."
        f_print_line1

        f_disable
        f_remove_files
        f_copy_files
        f_enable
    fi
}

#=============================================================================
# Uninstall Visual Python
#=============================================================================
f_uninstall() {
    RES=`f_check_extension`
    # 1 = Jupyter Extension is not actived
    # 2 = visualpython does not exist
    # 3 = visualpython exists

    if [ ${RES} -eq 2 ]; then
        f_print_line2
        ${PIP_UNINST}
        f_print_line2
    elif [ ${RES} -eq 3 ]; then
        f_print_line1
        f_disable
        f_print_line2
        f_remove_files
        f_print_line2
        ${PIP_UNINST}
        f_print_line1
    fi
}

#=============================================================================
# Upgrade Visual Python
#=============================================================================
f_upgrade() {
    f_print_line1
    echo "Running upgrade Visual Python"
    f_print_line2

    # Get Visual Python version
    VP_VERSION=`f_get_string_pipshow Version`

    ${PIP_UPGRAD}

    # Get Visual Python new version
    VP_VERSION_NEW=`f_get_string_pipshow Version`

    if [ ${VP_VERSION} = ${VP_VERSION_NEW} ]; then
        f_print_line2
        echo "Already installed last Visual Python version."
        f_print_line2
        echo "Installed version    : "${VP_VERSION}
        echo "Last Release version : "${VP_VERSION_NEW}
        f_print_line1
    else
        f_print_line2
        f_disable
        f_remove_files
        f_copy_files
        f_enable
        f_print_line1
    fi
}

#=============================================================================
# Enable Visual Python
#=============================================================================
f_enable() {
    RES=`f_check_extension`
    if  [ ${RES} -eq 3 ]; then
        ${JP_NB} enable ${VP_BIND}
    fi
}

#=============================================================================
# Disable Visual Python
#=============================================================================
f_disable() {
    RES=`f_check_extension`
    if  [ ${RES} -eq 3 ]; then
        ${JP_NB} disable ${VP_BIND}
    fi
}

#=============================================================================
# Visual Python version
#=============================================================================
f_version() {
    VP_VERSION=`f_get_string_pipshow Version`

    echo "Visual Python "${VP_VERSION}
}

#=============================================================================
# Help messages               
#=============================================================================
f_help() {
    echo ""
    echo "usage: visualpy [option] [--pip3]"
    echo ""
    echo "optional arguments:"
    echo "  -h,  help       show this help message and exit"
    echo "  -e,  enable     enable Visual Python"
    echo "  -d,  disable    disable Visual Python"
    echo "  -i,  install    install Visual Python extensions"
    echo "  -ui, uninstall  uninstall Visual Python packages"
    echo "  -up, upgrade    upgrade Visual Python Package"
    echo "  -v,  version    show Visual version and exit"
    echo ""
    echo "  --pip3          use pip3 [default: pip]"
    echo ""
}

#=============================================================================
# Copy Visual Python files
#=============================================================================
f_copy_files() {
    f_print_line1
    echo "Copy visualpyhthon extension files ..."
    f_print_line2
    echo "Source Dir : "  ${PATH_SRC}/${VP_NAME}
    echo "Target Dir : "  ${PATH_DST}/${VP_NAME}
    f_print_line1
    cp -Rp ${PATH_SRC}/${VP_NAME} ${PATH_DST}/.
}

#=============================================================================
# Remove Visual Python files
#=============================================================================
f_remove_files() {
    f_print_line1
    if [ -e ${PATH_DST}/${VP_NAME} ]; then
        echo "Remove Visual Python Directories."
        echo "Target Dir : "  ${PATH_DST}
        \rm -rf ${PATH_DST}/${VP_NAME}
    else
        echo "not exists visualpython dir"
    fi
}

#=============================================================================
# Check Visual Python files
#   1 = Jupyter Extension is not actived
#   2 = visualpython does not exist
#   3 = visualpython exists
#=============================================================================
f_check_extension() {
    if [ ! -e ${PATH_DST} ]; then
        echo 1
    elif [ ! -e ${PATH_DST}/${VP_NAME} ]; then
        echo 2
    else
        echo 3
    fi
}

#=============================================================================
# Get string(Version or Location) from pip show
#   $1 = Version or Location
#=============================================================================
f_get_string_pipshow() {
    RESULT="EMPTY"
    if [ $# -ge 1 ]; then
        RESULT=`${PIP} show ${VP_NAME} | grep $1 | awk -F':' '{print $2}' | tr -d ' '`
    fi  
    echo ${RESULT}
}

#=============================================================================
# Get string(Jupyter nbextension path) from conda-env or jupyter
#=============================================================================
f_get_extension_path() {
    RESULT="EMPTY"
    if which conda-env > /dev/null 2>&1; then
        RESULT=`conda-env list | grep "*" | awk -F'*' '{print $2}'|tr -d ' '`/share/jupyter/nbextensions
    else
        RESULT=`jupyter --data-dir`/nbextensions
    fi
    echo ${RESULT}
}

#=============================================================================
# Print extension is not installed
#=============================================================================
f_print_not_extension() {
    echo "Jupyter nbextension is not activated"
    echo "Please install Jupyter nbextension"
    f_print_line1
    echo "for conda env"
    echo "conda install -c conda-forge jupyter_contrib_nbextensions"
    echo "jupyter contrib nbextension install --user"
    f_print_line2
    echo "for pip"
    echo ${PIP}" install -e jupyter_contrib_nbextensions"
    echo "jupyter contrib nbextension install --user"
    f_print_line1
}

#=============================================================================
# Print line
#=============================================================================
f_print_line1() {
    echo "============================================================================================"
}

f_print_line2() {
    echo "--------------------------------------------------------------------------------------------"
}

#=============================================================================
# Execute main function
#=============================================================================
f_main

exit 0

# End of file
