#!/usr/bin/env bash

DEST=${1:-/etc/skel}

# download a specific theme for lazygit from the Catppuccin repository and set
# up an alias for lazygit that uses this theme.
# catppuccin - https://github.com/catppuccin/lazygit
# see: https://github.com/catppuccin/lazygit/README.md
BASE_THEME_URL="https://raw.githubusercontent.com/catppuccin/lazygit/main/themes-mergable"

# frappe, latte, macchiato, mocha
BASE_THEME="macchiato"

# blue, flamingo, green, lavender, maroon, mauve, peach, pink, red, rosewater,
# sapphire, sky, teal, yellow
THEME_ACCENT="peach"
THEME_NAME="${BASE_THEME}-${THEME_ACCENT}"

URL="${BASE_THEME_URL}/${BASE_THEME}/${THEME_ACCENT}.yml"

LAZYGIT_HOME="${DEST}/.config/lazygit"
mkdir -p ${LAZYGIT_HOME}/themes

wget -c -O ${LAZYGIT_HOME}/themes/${THEME_NAME}.yml ${URL} ||
	{
		echo "Failed to download theme configuration"
		exit 1
	}

# user configuration
mkdir -p ${LAZYGIT_HOME}
cat <<-EOF | tee ${LAZYGIT_HOME}/config.yml
	# Interactive rebase onto another branch - https://github.com/jesseduffield/lazygit/issues/2517
	customCommands:
	  - key: "I"
	    command: "GIT_EDITOR=\"sed -i '1s/^/break \\n /'\" git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash {{.SelectedLocalBranch.Name}}"
	    context: "localBranches"

	# Default editor - https://www.reddit.com/r/neovim/comments/15v6mxy/comment/jwutoh2/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button 
	os:
	  editPreset: 'nvim-remote'
	  edit: "nvr --remote-send '<C-space><cmd>q<CR><Esc><cmd>lua vim.cmd(\"e \" .. {{filename}})<CR>'"
	  editAtLine: "nvr --remote-send '<C-space><cmd>q<CR><Esc><cmd>lua vim.cmd(\"e \" .. {{filename}})<CR>{{line}}G'"
EOF

# Create an alias for lazygit using the theme
mkdir -p ${DEST}/.zsh_aliases.d
cat <<-EOF | tee ${DEST}/.zsh_aliases.d/005_lazygit.zsh
	  # This will instruct lazygit to open both config files, merge them, and then boot.
	  # You can add more config files, delimited by a comma, to this list

	  CONFIG="${LAZYGIT_HOME}/config.yml,${LAZYGIT_HOME}/themes/${THEME_NAME}.yml"
	  alias lazygit='LG_CONFIG_FILE="\${CONFIG}" lazygit'
	  alias lzg='LG_CONFIG_FILE="\${CONFIG}" lazygit'
EOF

# Checkinstall lazygit to produce a deb package
cd /var/tmp || exit

GITHUB_USER=jesseduffield
PROJECT=lazygit
ARCH=$(dpkg --print-architecture)
# RELEASE=$(lastversion --format="tag" "${GITHUB_USER}/${PROJECT}") #$(lastversion --form "${PROJECT}")
LATEST_URL="https://api.github.com/repos/${GITHUB_USER}/${PROJECT}/releases/latest"
RELEASE=$(curl -L -s -H 'Accept: application/json' "${LATEST_URL}" | grep -Po '"tag_name": "v\K[^"]*')
GIT_TAG=$(curl -L -s -H 'Accept: application/json' "${LATEST_URL}" | jq -r .tag_name)
# DOWNLOAD_DIR=/tmp

RSS_FEED="https://github.com/${USER}/${PROJECT}/releases.atom"
# xdg-open ${RSS_FEED}
echo ${RSS_FEED}

# make a containing directory
mkdir -p /var/tmp/${PROJECT}-"${RELEASE}"
cd /var/tmp/${PROJECT}-"${RELEASE}" || exit

cat >./description-pak <<-EOF
	A simple terminal UI for git commands
EOF

cat >./checkinstall_it.sh <<-EOF

	echo "ENTERING CHECKINSTALL"

	BASE_URL='https://raw.githubusercontent.com/'
	BASE_USER=${GITHUB_USER}
	BASE_REPO=${PROJECT}
	LICENSE_URL="${BASE_URL}/${BASE_USER}/${BASE_REPO}"/master/LICENSE
	# wget -c ${LICENSE_URL}

	# VERSION=$(date +%Y-%m-%d_)git
	VERSION=${RELEASE}
	RELEASE="1"
	LICENSE=MIT

	# make a new temporary directory for this use
	BASE_TMP_DIR=~/tmptmp/checkinstall_tmp
	mkdir -p \${BASE_TMP_DIR}

	# do your work
	checkinstall -y --fstrans \
		--exclude=/root/.sudo_as_admin_successful \
		--pkgname=\${PROJECT} \
		--pkgversion=\${VERSION}\
		--pkgrelease="\${RELEASE}" \
		--pkgarch=${ARCH} \
		--pkggroup=development \
		--pkglicense=MIT \
		--pkgsource=${LATEST_URL} \
		--maintainer=cyteen@ring-zero.co.uk \
		--requires=vim,git \
		-D \
		bash ./install.sh
EOF

cat >./install.sh <<-EOF
	#!/bin/bash

	# allow specifying different destination directory
	DIR="${DIR:-"$HOME/.local/bin"}"

	# map different architecture variations to the available binaries
	ARCH=$(uname -m)
	case \$ARCH in
		i386|i686) ARCH=x86 ;;
		armv6*) ARCH=armv6 ;;
		armv7*) ARCH=armv7 ;;
		aarch64*) ARCH=arm64 ;;
	esac

	GITHUB_FILE="${PROJECT}_${RELEASE}_$(uname -s)_\${ARCH}.tar.gz"
	GITHUB_URL="https://github.com/${GITHUB_USER}/${PROJECT}/releases/download/${GIT_TAG}/\${GITHUB_FILE}"


	curl -Lo ${PROJECT}.tar.gz \$GITHUB_URL

	tar xf ${PROJECT}.tar.gz ${PROJECT}
	install -Dm 755 ${PROJECT} -t /usr/local/bin
EOF

bash ./checkinstall_it.sh
