#!/usr/bin/env bash
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------
#
# Docs: https://github.com/microsoft/vscode-dev-containers/blob/master/script-library/docs/go.md
#
# Syntax: ./go-debian.sh [Go version] [GOROOT] [GOPATH] [non-root user] [Add GOPATH, GOROOT to rc files flag] [Install tools flag]

TARGET_GO_VERSION=${1:-"latest"}
TARGET_GOROOT=${2:-"/usr/local/go"}
TARGET_GOPATH=${3:-"/go"}
USERNAME=${4:-"automatic"}
UPDATE_RC=${5:-"true"}
INSTALL_GO_TOOLS=${6:-"true"}

set -e

if [ "$(id -u)" -ne 0 ]; then
	echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
	exit 1
fi

# Determine the appropriate non-root user
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
	USERNAME=""
	POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
	for CURRENT_USER in ${POSSIBLE_USERS[@]}; do
		if id -u ${CURRENT_USER} >/dev/null 2>&1; then
			USERNAME=${CURRENT_USER}
			break
		fi
	done
	if [ "${USERNAME}" = "" ]; then
		USERNAME=root
	fi
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} >/dev/null 2>&1; then
	USERNAME=root
fi

function updaterc() {
	if [ "${UPDATE_RC}" = "true" ]; then
		echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..."
		echo -e "$1" | tee -a /etc/bash.bashrc >>/etc/zsh/zshrc
	fi
}

export DEBIAN_FRONTEND=noninteractive

# Install curl, tar, git, other dependencies if missing
if ! dpkg -s curl ca-certificates tar git g++ gcc libc6-dev make pkg-config >/dev/null 2>&1; then
	if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
		apt-get update
	fi
	apt-get -y install --no-install-recommends curl ca-certificates tar git g++ gcc libc6-dev make pkg-config
fi

# Get latest version number if latest is specified
if [ "${TARGET_GO_VERSION}" = "latest" ] || [ "${TARGET_GO_VERSION}" = "current" ] || [ "${TARGET_GO_VERSION}" = "lts" ]; then
	TARGET_GO_VERSION=$(curl -sSL "https://golang.org/VERSION?m=text" | sed -n '/^go/s///p')
fi

# Install Go
GO_INSTALL_SCRIPT="$(
	cat <<EOF
    set -e
    echo "Downloading Go ${TARGET_GO_VERSION}..."
    curl -sSL -o /tmp/go.tar.gz "https://golang.org/dl/go${TARGET_GO_VERSION}.linux-amd64.tar.gz"
    echo "Extracting Go ${TARGET_GO_VERSION}..."
    tar -xzf /tmp/go.tar.gz -C "${TARGET_GOROOT}" --strip-components=1
    rm -f /tmp/go.tar.gz
EOF
)"
if [ "${TARGET_GO_VERSION}" != "none" ] && ! type go >/dev/null 2>&1; then
	mkdir -p "${TARGET_GOROOT}" "${TARGET_GOPATH}"
	chown -R ${USERNAME} "${TARGET_GOROOT}" "${TARGET_GOPATH}"
	su ${USERNAME} -c "${GO_INSTALL_SCRIPT}"
else
	echo "Go already installed. Skipping."
fi

# Install Go tools
GO_TOOLS_WITH_MODULES="\
    golang.org/x/tools/gopls \
    honnef.co/go/tools/... \
    golang.org/x/tools/cmd/gorename \
    golang.org/x/tools/cmd/goimports \
    golang.org/x/tools/cmd/guru \
    golang.org/x/lint/golint \
    github.com/mdempsky/gocode \
    github.com/cweill/gotests/... \
    github.com/haya14busa/goplay/cmd/goplay \
    github.com/sqs/goreturns \
    github.com/josharian/impl \
    github.com/davidrjenni/reftools/cmd/fillstruct \
    github.com/uudashr/gopkgs/v2/cmd/gopkgs \
    github.com/ramya-rao-a/go-outline \
    github.com/acroca/go-symbols \
    github.com/godoctor/godoctor \
    github.com/rogpeppe/godef \
    github.com/zmb3/gogetdoc \
    github.com/fatih/gomodifytags \
    github.com/mgechev/revive \
    github.com/go-delve/delve/cmd/dlv"
if [ "${INSTALL_GO_TOOLS}" = "true" ]; then
	echo "Installing common Go tools..."
	export PATH=${TARGET_GOROOT}/bin:${PATH}
	mkdir -p /tmp/gotools
	cd /tmp/gotools
	export GOPATH=/tmp/gotools
	export GOCACHE=/tmp/gotools/cache

	# Go tools w/module support
	export GO111MODULE=on
	(echo "${GO_TOOLS_WITH_MODULES}" | xargs -n 1 go get -v) 2>&1

	# gocode-gomod
	export GO111MODULE=auto
	go get -v -d github.com/stamblerre/gocode 2>&1
	go build -o gocode-gomod github.com/stamblerre/gocode

	# golangci-lint
	curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ${TARGET_GOPATH}/bin 2>&1

	# Move Go tools into path and clean up
	mv /tmp/gotools/bin/* ${TARGET_GOPATH}/bin/
	mv gocode-gomod ${TARGET_GOPATH}/bin/
	rm -rf /tmp/gotools
	chown -R ${USERNAME} "${TARGET_GOPATH}"
fi

# Add GOPATH variable and bin directory into PATH in bashrc/zshrc files (unless disabled)
updaterc "export GOPATH=\"${TARGET_GOPATH}\"\nexport GOROOT=\"${TARGET_GOROOT}\"\nexport PATH=\"\${GOROOT}/bin:\${GOPATH}/bin:\${PATH}\""
echo "Done!"
