#!/bin/sh
# Copyright (C) 2018 Kovid Goyal <kovid at kovidgoyal.net>
#
# Distributed under terms of the GPLv3 license.

{ \unalias command; \unset -f command; } >/dev/null 2>&1


die() { printf "\033[31m%s\033[m\n\r" "$*" > /dev/stderr; exit 1; }

delete_lock_dir() {
    trap '' EXIT INT QUIT TERM
    [ -n "$lock_dir" ] && {
        command rm -rf "$lock_dir"
        lock_dir=""
    }
}

exec_kitty() {
    delete_lock_dir
    [ -n "$kitty_exe" ] && exec "$kitty_exe" "$@"
    die "Failed to execute kitty"
}


is_wrapped_kitten() {
    wrapped_kittens="clipboard icat hyperlinked_grep ask hints unicode_input ssh themes diff show_key"
    [ -n "$1" ] && {
        case " $wrapped_kittens " in
            *" $1 "*) printf "%s" "$1" ;;
        esac
    }
}

test "(" "$1" = "+kitten" -a -n "$(is_wrapped_kitten "$2")" ")" -o "(" "$1" = "+" -a "$2" = "kitten" -a "$(is_wrapped_kitten "$3")" ")" && {
    if [ "$1" = "+kitten" ]; then shift "1"; else shift "2"; fi
    exec kitten "$@"
}

lock_dir=""
script_path="$(command readlink -f "$0" 2> /dev/null)"
[ $? = 0 ] || script_path="$0"
script_dir="$(command dirname "$script_path")"
install_dir="$(command dirname "$script_dir")/install"
remote_kitty_version_file="$script_dir/../version"
local_kitty_version_file="$install_dir/installed-kitty-version"
kitty_exe="$install_dir/bin/kitty"
local_kitty_version=""

[ -f "$kitty_exe" -a -x "$kitty_exe" -a "$1" != "+update-kitty" ] && exec_kitty "$@"

case "$(command uname)" in
    'Linux') OS="linux";;
    'Darwin') OS="macos";;
    *) die "kitty pre-built binaries are not available for the $(command uname) operating system";;
esac

if command -v curl 2> /dev/null > /dev/null; then
    fetch() {
        command curl -fL "$1"
    }
    fetch_quiet() {
        command curl -fsSL "$1"
    }
elif command -v wget 2> /dev/null > /dev/null; then
    fetch() {
        command wget -O- "$1"
    }
    fetch_quiet() {
        command wget --quiet -O- "$1"
    }
else
    die "Neither curl nor wget available, cannot download kitty"
fi

if [ "$OS" = "linux" ]; then
    case "$(command uname -m)" in
        amd64|x86_64) arch="x86_64";;
        aarch64*) arch="arm64";;
        armv8*) arch="arm64";;
        i386) arch="i686";;
        i686) arch="i686";;
        *) die "Unknown CPU architecture $(command uname -m)";;
    esac
fi

release_version=$(fetch_quiet "https://sw.kovidgoyal.net/kitty/current-version.txt")
[ $? -ne 0 -o -z "$release_version" ] && {
    [ -n "$local_kitty_version" ] && exec_kitty "$@"
    die "Could not get kitty latest release version"
}

if [ "$OS" = "linux" ]; then
    url="https://github.com/kovidgoyal/kitty/releases/download/v$release_version/kitty-$release_version-$arch.txz"
else
    url="https://github.com/kovidgoyal/kitty/releases/download/v$release_version/kitty-$release_version.dmg"
fi

lock_dir="$script_dir/kitty-install-lock"
if ! command mkdir "$lock_dir" 2> /dev/null; then
    ed="$lock_dir"
    lock_dir="";
    die "Failed to create lock dir another instance of the kitty bootstrap script is running. If you are sure that is not the case delete: $ed";
fi
trap 'delete_lock_dir' EXIT INT QUIT TERM

printf "\033[33mkitty needs to be installed\033[m\n\n"
command rm -rf "$install_dir"
command mkdir -p "$install_dir"
printf "Downloading kitty from: \033[32m%s\033[m\n\n" "$url"

if [ "$OS" = "linux" ]; then
    old_umask=$(umask)
    umask 000
    fetch "$url" | command tar -C "$install_dir" -xJof -
    umask "$old_umask"
    [ $? = 0 ] || die "Failed to download and install kitty"
else
    tdir=$(command mktemp -d "$install_dir/tmp-for-dmg-XXXXXXXXXXXX")
    [ $? = 0 ] || die "Creating temp directory failed"
    fetch "$url" > "$tdir/kitty.dmg"
    command mkdir "$tdir/mp"
    command hdiutil attach "$tdir/kitty.dmg" "-mountpoint" "$tdir/mp" || die "Failed to mount kitty.dmg"
    command ditto -v "$tdir/mp/kitty.app" "$install_dir/kitty.app"
    rc="$?"
    command hdiutil detach "$tdir/mp"
    command rm -rf "$tdir"
    [ "$rc" != "0" ] && die "Failed to copy kitty.app from mounted dmg"
    command mkdir "$install_dir/bin"
    command ln -sf "$install_dir/kitty.app/Contents/MacOS/kitty" "$install_dir/bin/kitty"
fi
command "$kitty_exe" +runpy "from kitty.constants import str_version; print(end=str_version)" > "$local_kitty_version_file"
exec_kitty "$@"
