#!/bin/sh

dir="$(cd "$(dirname "$0")" >/dev/null 2>&1 || exit ; pwd -P)"

OS=$(uname -s)
ARCH=$(uname -m)
EXT=

# Detect OS
case $OS in
    Linux)
        GOOS=linux
        ;;
    Darwin)
        GOOS=darwin
        ;;
    Windows*|CYGWIN*)
        GOOS=windows
        EXT=.exe
        ;;
    *)
        echo "Unsupported OS: ${OS}"
        exit 1
        ;;
esac

# Detect architecture
case $ARCH in
    x86_64)
        GOARCH=amd64
        ;;
    x86)
        GOARCH=386
        ;;
    arm64)
        GOOS=arm64
        ;;
    *)
        echo "Unsupported CPU architecture: ${ARCH}"
        exit 1
        ;;
esac

executable="$dir/lefthook_${GOOS}_${GOARCH}/lefthook${EXT}"

if [ -x "$executable" ]; then
    exec "$executable" "$@"
else
    echo "Lefthook wasn't build for ${OS} ${ARCH}"
    exit 1
fi
