#!/usr/bin/env sh

set -eu

(unset CDPATH) >/dev/null 2>&1 && unset CDPATH

mkdir -p "${HOME}"/.local/share/aichat \
  && cd "${HOME}"/.local/share/aichat || exit 1

if [ ! -d "${HOME}"/.local/share/aichat/llm-functions/scripts ]; then
  git clone "https://github.com/sigoden/llm-functions"
fi

cd "${HOME}"/.local/share/aichat/llm-functions || exit 1

cat <<-EOF | tee "${HOME}"/.local/share/aichat/llm-functions/tools.txt
get_current_weather.sh
execute_command.sh
#execute_py_code.py
EOF

cat <<-EOF | tee "${HOME}"/.local/share/aichat/llm-functions/agents.txt
coder
todo
EOF

if command -v argc >/dev/null 2>&1; then
  argc build
  # symlink this repo directory to AIChat's functions_dir
  # ln -s "$(pwd)" "$(aichat --info | grep -w functions_dir | awk '{ $1=""; print substr($0,2) }')"
  # or
  argc install
else
  printf '%s\n' "argc not installed."
  case "$(uname)" in
    Darwin)
      brew install argc
      ;;
    *)
      curl -fsSL https://raw.githubusercontent.com/sigoden/argc/main/install.sh \
        | sh -s -- --to /usr/local/bin
      ;;
  esac
  argc build
  # symlink this repo directory to AIChat's functions_dir
  # ln -s "$(pwd)" "$(aichat --info | grep -w functions_dir | awk '{$1=""; print substr($0,2)}')"
  # or
  argc install
fi
