#!/bin/bash

function set_cwd() {
    cd "$(dirname "$0")"
    cd ..
}

function check_tools(){
    which awk > /dev/null 2>&1 || (echo "please install awk";exit 1)
    which curl > /dev/null 2>&1 || (echo "please install curl";exit 1)
}

function update() {
    git pull
}

function install_nvm() {
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
}

function help(){
    me=$(basename $(readlink -nf $0))
    cat <<EOF >/dev/stdout

------------------update-------------
${me} update
-------------------------------------

------------------deskop-------------
${me} dbeaver OR ${me} 1
${me} drawio OR ${me} 2
${me} filezilla OR ${me} 3
${me} filefox OR ${me} 4
${me} sublime_text OR ${me} 5
${me} vscode OR ${me} 6
${me} pycharm OR ${me} 7
${me} joplin OR ${me} 8
${me} qq OR ${me}  9
${me} qqmusic OR ${me} 10
${me} redisInsight OR ${me} 11
${me} hoppscotch OR ${me} 12
-------------------------------------

------------------cli----------------
${me} docker-compose
${me} etcd
${me} gh
${me} go
${me} hugo
${me} java
${me} protoc
${me} syncthing
${me} tigervnc
${me} upx
${me} nvm
-------------------------------------
EOF
}

function main() {
    case $1 in
        update)
            update
        ;;
        dbeaver | 1)
            exec bash dbeaver.sh $2
        ;;
        drawio | 2)
            exec bash drawio.sh $2
        ;;
        filezilla | 3)
            exec bash filezilla.sh $2
        ;;
        firefox | 4)
            exec bash firefox.sh $2
        ;;
        sublime_text | 5)
            exec bash sublime_text.sh $2
        ;;
        vscode | 6)
            exec bash vscode.sh $2
        ;;
        pycharm | 7)
            exec bash pycharm.sh $2
        ;;
        joplin | 8)
            exec bash joplin.sh $2
        ;;
        qq | 9)
            exec bash qq.sh $2
        ;;
        qqmusic | 10)
            exec bash qqmusic.sh $2
        ;;
        redisInsight | 11)
            exec bash redisInsight.sh $2
        ;;
        hoppscotch | 12)
            exec bash hoppscotch.sh $2
        ;;

        # cli
        docker-compose)
            exec bash docker-compose.sh $2
        ;;
        etcd)
            exec bash etcd.sh $2
        ;;
        gh)
            exec bash gh.sh $2
        ;;
        go)
            exec bash go.sh $2
        ;;
        hugo)
            exec bash hugo.sh $2
        ;;
        java)
            exec bash java.sh $2
        ;;
        protoc)
            exec bash protoc.sh $2
        ;;
        syncthing)
            exec bash syncthing.sh $2
        ;;
        tigervnc)
            exec bash tigervnc.sh $2
        ;;
        upx)
            exec bash upx.sh $2
        ;;
        nvm)
            exec install_nvm
        ;;
        help)
        help
        ;;
        *)
        help
        ;;
    esac
}

check_tools
set_cwd
main $@