#!/bin/sh

usage() {
	echo "usage: $(basename $0) [-al] [-n ns] [pkg...]" >&2
}

# Fix missing shared libs
shlibfix() {
	find $1/bin $1/libexec -executable -type f -exec ldd {} + 2>/dev/null \
		|awk '/=>/{print $3}/^\s+\//{print $1}' \
		|sed 's,/usr,,'|sort|uniq|cpio --quiet -updL $1
}

# https://claude.ai/install.sh
install_claude() {
	d="$1"
	u="https://downloads.claude.ai/claude-code-releases"
	a="linux-x64"
	v=$(curl -sSfL "$u/latest")
	flock $l curl -sSfL "$u/$v/$a/claude" -o $root/bin/claude &
	spinner -p "Install claude ($v)" $l
	chmod +x $d/bin/claude
}

# https://developer.hashicorp.com/terraform/install
install_terraform() {
	z=$(mktemp --suffix .zip)
	u="https://releases.hashicorp.com/terraform"
	a="linux_amd64"
	v=$(curl -s "https://developer.hashicorp.com/terraform/install"|sed -rn 's/.* ([0-9.]*) \(latest\).*/\1/p')
	flock $l curl -sSfL "$u/$v/terraform_${v}_${a}.zip" -o $z &
	spinner -p "Install terraform ($v)" $l
	unzip -q -o -d $root/bin $z terraform
	rm -f $z
}

# https://gameoftrees.org/portable.html
install_got() {
	d="$1"
	tmp=$(mktemp -d)
	u="https://gameoftrees.org/releases/portable"
	v=$(curl -sSfL "https://gameoftrees.org/portable.html"|sed -rn '/Latest Release/s/.*>([0-9.]+).*/\1/p')
	flock $l sh <<-EOF &
	curl -sSfL "$u/got-portable-${v}.tar.gz" | tar -xz -C $tmp
	cd "$tmp/got-portable-${v}"
	./configure -q CPPFLAGS='-DGOT_DIAL_PATH_SSH=\"/bin/ssh\"' --prefix='' >/dev/null
	make -j $(nproc) CC=clang DESTDIR="$d" install >/dev/null 2>&1
	EOF
	spinner -p "Install got (${v})" $l
	rm -rf $tmp
}

# https://www.brain-dump.org/projects/abduco
install_abduco() {
	d="$1"
	tmp=$(mktemp -d)
	u="https://github.com/martanne/abduco/releases/download"
	v="0.6"
	flock $l sh <<-EOF &
	curl -sSfL "$u/v${v}/abduco-${v}.tar.gz" | tar -xz -C $tmp
	cd "$tmp/abduco-${v}"
	make -j $(nproc) CC=clang DESTDIR="$d" PREFIX= install >/dev/null 2>&1
	EOF
	spinner -p "Install abduco (${v})" $l
	rm -rf $tmp
}

# https://github.com/martanne/vis
install_vis() {
	d="$1"
	tmp=$(mktemp -d)
	u="https://github.com/martanne/vis"
	a="https://api.github.com/repos/martanne/vis"
	v=$(curl -sSfL "$a/commits" 2>/dev/null|grep -m1 -Eo '"sha": "[0-9a-f]+"'|cut -b9-48)
	flock $l sh <<-EOF &
	curl -sSfL "$u/archive/${v}.tar.gz" | tar -xz -C $tmp
	cd "$tmp/vis-${v}"
	./configure --enable-static >/dev/null
	make -j $(nproc) DESTDIR="$d" PREFIX= install >/dev/null 2>&1
	EOF
	sha=$(echo $v|cut -c 1-7)
	spinner -p "Install vis ($sha)" $l
	rm -rf $tmp
}

install_nodejs() {
	d="$1"
	tmp=$(mktemp -d)
	v=
	u="https://nodejs.org/dist"
	v=$(curl -sSfL $u/latest/SHASUMS256.txt|sed -rn '/linux-x64.tar.gz/s/.*(v[0-9.]+).*/\1/p')
	flock $l sh <<-EOF &
	curl -sSfL "$u/$v/node-$v-linux-x64.tar.gz" | tar -xz -C $tmp
	rsync -a $tmp/node-${v}-linux-x64/bin $tmp/node-${v}-linux-x64/lib $root/
	EOF
	spinner -p "Install nodejs (${v})" $l
	rm -rf $tmp
}

# https://jqlang.org
install_jq() {
	d="$1"
	u="https://github.com/jqlang/jq/releases/download"
	a="linux-amd64"
	v=$(curl -sSfL "https://jqlang.org"|awk '/Download jq [0-9.]+/{print $3}')
	curl -sSfL "$u/jq-${v}/jq-${a}" -o $root/bin/jq
	chmod +x $root/bin/jq
}

list_pkg() {
	sed -rn 's/^install_(.*)\(\) \{/\1/p' "$0"|sort
}

pkg=
nsname=$(ns -l|head -n1)
while getopts "ahln:" OPT; do
	case $OPT in
	a) pkg=$(list_pkg);;
	l) list_pkg; exit 0;;
	n) nsname="$OPTARG";;
	h) usage; exit 0;;
	*) usage; exit 1;;
	esac
done
shift $((OPTIND-1))

: ${pkg:=$@}

if [ -z "$pkg" ] && [ $# -lt 1 ]; then
	usage
	exit 1
fi

root=$(ns -i $nsname|awk '/^root:/{print $2}')
for p in $pkg; do
	if ! list_pkg|grep -q "^${p}\$"; then
		printf '%s: Not in package list\n' "$p" >&2
		exit 1
	fi
done

l=$(mktemp)
trap "rm -f $l" EXIT INT TERM QUIT
for p in $pkg; do install_$p $root; done
shlibfix $root
