#!/bin/bash

expensive=False
remove_ecmascript=False

function usage
{
    retval="$1"
    case "$retval" in
        0)
            ;;
        *)
            exec 1>&2
            ;;
    esac

    # echo "Usage: $0 --expensive --remove-ecmascript --help"
    echo "Usage: $0 --expensive --help"

    exit "$retval"
}

function fix_node_path
{
    # This no longer works: "npm bin" doesn't exist.
    if [ "$#" != 1 ]
    then
        echo "$0: fix_node_path: bad number of arguments" 1>&2
        exit 1
    fi
    script="$1"
    file="$(npm bin --global)/$script"
    if ! head -1 "$file" | grep -q "^#!/usr/bin/env $node\$"
    then
        sudo sed --in-place --follow-symlinks "s%#!/usr/bin/env .*\$%#!/usr/bin/env $node%1" "$file"
    fi
}

while [ "$#" -ge 1 ]
do
    case "$1" in
        --expensive)
            expensive=True
            ;;
#        --remove-ecmascript)
#            remove_ecmascript=True
#            ;;
        -h|--help)
            usage 0
            ;;
        *)
            echo "$0: Unrecognized option: $1" 1>&2
            usage 1
            ;;
    esac
    shift
done

# Install jedi for 3.x.  We used to do this for 2.x, but no more.
./install-via-pip --python-path python3 --module-to-import jedi --pip-package jedi
# I suspect the jedi vim plugin wants this in /usr/bin/python3, not "just' the first python3 on $PATH
if cat /usr/bin/python3 > /dev/null 2>&1
then
	./install-via-pip --python-path /usr/bin/python3 --module-to-import jedi --pip-package jedi
fi

./install-package --deb-packages python3-venv

# Tested on Debian and Mint.  Should work on Ubuntu.  Might work on RHEL/CentOS/Amazon Linux
if ! my-pylint --version > /dev/null 2>&1
then
    # This one is packaged up as a deb
    ./install-package --deb-packages pylint --rpm-packages pylint
fi
if ! my-pylint3 --version > /dev/null 2>&1
then
    # This one, at least on Debian 8.9, is not a deb and must come from pip3
    ./install-via-pip --python-path python3 --module-to-import pylint --pip-package pylint
fi

./install-via-pip --python-path python3 --module-to-import pycodestyle --pip-package pycodestyle
./install-via-pip --python-path python3 --module-to-import pydocstyle --pip-package pydocstyle
# Ruff has obviated pyflakes
# ./install-via-pip --python-path python3 --module-to-import pyflakes --pip-package pyflakes
./install-via-pip --python-path python3 --module-to-import flake8 --pip-package flake8
./install-via-pip --python-path python3 --module-to-import mccabe --pip-package mccabe

./install-via-pip --python-path /usr/bin/python3 --pip-package neovim --module-to-import neovim
# ./install-via-pip --python-path /usr/bin/python --pip-package neovim --module-to-import neovim

if ! tidy --version > /dev/null 2>&1
then
    ./install-package --deb-packages tidy --rpm-packages tidy
fi

if ! javac -help > /dev/null 2>&1
then
    # --rpm-packages untested.
    # 9 may not be current forever, but I do not see a way of getting it without specifying a version.
    ./install-package --deb-packages default-jdk --rpm-packages default-jdk
fi

if ! shellcheck --version > /dev/null 2>&1
then
    # --rpm-packages untested.
    ./install-package --deb-packages shellcheck --rpm-packages shellcheck --brew-packages shellcheck
fi

if ! luarocks --version > /dev/null 2>&1
then
    ./install-package --deb-packages luarocks  --rpm-packages luarocks --brew-packages luarocks
fi
if ! type luac > /dev/null 2>&1
then
    sudo luarocks install luac
fi
if ! type luacheck > /dev/null 2>&1
then
    sudo luarocks install luacheck
fi

# Install golang checkers.
# --rpm-packages untested in both.
# golint has been discontionued upstream
# ./install-package --deb-packages golint --rpm-packages golint
# ./install-package --deb-packages golang-go --rpm-packages golang-go

case "$remove_ecmascript" in
    True)
        rm -f ~/.npmrc ~/package.json
        rm -rf ~/.npm-packages ~/node_modules
        ;;
    False)
        # Good, nothing to do
        ;;
    *)
        echo "$0: Internal error: \$remove_ecmascript has a strange value: $remove_ecmascript" 1>&2
        exit 1
        ;;
esac

# It seems to work a lot better to use a nightly of rust/rustup/cargo/clippy
# ./install-package --deb-packages cargo --rpm-packages cargo

case "$expensive" in
    True)
        echo "$0: Note: I recommend running this in gnome-terminal or similar" 1>&2
        if ! type nodejs > /dev/null 2>&1
        then
            ./install-package --deb-packages nodejs --rpm-packages nodejs
            ./install-package --deb-packages vpm --rpm-packages vpm
        fi
        if ! type npm > /dev/null 2>&1
        then
            # --rpm-packages not tested.
            ./install-package --deb-packages npm --rpm-packages npm
        fi
        if ! [ -f ~/.npmrc ]
        then
            # I banged my head against putting node packages in my homedir for hours, but no dice.
            # So instead, we put things in /usr/local .
            # Mon Oct 16 16:36:29 PDT 2017 That was probably because of the sed below not following symlinks.
            # But now that it's working under /usr/local, I think I'll leave it; it's less custom.
            #
            # Tell npm where to find packages, but only if the file does not yet exist.
            # Shades of autoinstall's add-line and foo.original here.
            # {
            # echo "prefix = $NPM_PACKAGES"
            # Didn't help.
            # echo "bin = $HOME/.npm-packages/bin"
            # } > ~/.npmrc
            :
        fi
        if ! [ -f ~/package.json ]
        then
            # Note that ~/packages.* can be in other formats.
            cd "$HOME" || exit 1
            # I'm tempted to feed yes '' into this.... I don't like it asking unnecessary questions.
            sudo npm init --global
        fi
        if ! [ -e "$(npm bin --global)/eslint" ]
        then
            cd "$HOME" || exit 1
            sudo npm install --global eslint
            eslint --init
            # This is supposed to enable checking ecmascript inside html files.
            # npm install --save-dev eslint-plugin-html
            sudo npm install --global eslint-plugin-html

            sudo npm install --global eslint-config-google

        fi

        # For typescript, which is a manifestly-typed ECMAScript
        if ! [ -e ~/.npm/typescript ]
        then
            sudo npm install --global typescript
        fi

        if ! [ -e "$(npm bin --global)/tslint" ]
        then
            sudo npm install --global tslint
        fi

        if ! [ -e "$(npm bin --global)/tsc" ]
        then
            sudo npm install --global tsc
            # Without this, nodejs types seem to not exist - EG readline and process.
            sudo npm install --save-dev @types/node
        fi

        # Find node executable
        if type node > /dev/null 2>&1
        then
            node=node
        elif type nodejs > /dev/null 2>&1
        then
            node=nodejs
        else
            echo "$0: Error: node or nodejs not found" 1>&2
            exit 1
        fi

        # This is weird. Why doesn't npm know our node executable is called "nodejs"?
        # fix_node_path eslint
        # fix_node_path tslint
        # fix_node_path tsc

#       if ! [ -e ~/.eslintrc.json ]
#       then
#           # instead we copy an .eslintrc.json with eslint-plugin-html in it, to get ecmascript checking within html files
#           eslint --init
#       fi
        ;;
    False)
        # Fine, no expensive stuff
        ;;
    *)
        echo "$0: Internal error: \$expensive has a strange value: $expensive" 1>&2
        exit 1
        ;;
esac