#!/usr/bin/env bash
set -eu

# Served from github.com/owahltinez/bootstrap
REPO_RAW="https://bash.wtz.app"

# Use wget or curl, whichever is available (but prefer curl)
if command -v curl >/dev/null 2>&1; then
    download() {
        curl -fsSL "$1"
    }
elif command -v wget >/dev/null 2>&1; then
    download() {
        wget -qO- "$1"
    }
else
    echo >&2 "wget or curl not available"
    exit 1
fi

install_file() {
    source_path="$1"
    destination="$2"
    temporary="${destination}.tmp"
    download "$REPO_RAW/$source_path" > "$temporary"
    mv "$temporary" "$destination"
}

install_script() {
    install_file "$1" "$2"
    chmod +x "$2"
}

# VIM customizations
mkdir -p "$HOME/.vim/autoload" "$HOME/.vim/bundle"
download https://tpo.pe/pathogen.vim > "$HOME/.vim/autoload/pathogen.vim"

# Various dotfiles
install_file dotfiles/.npmrc "$HOME/.npmrc"
install_file dotfiles/.bunfig.toml "$HOME/.bunfig.toml"
install_file dotfiles/.screenrc "$HOME/.screenrc"
install_file dotfiles/.tmux.conf "$HOME/.tmux.conf"
install_file dotfiles/.vimrc "$HOME/.vimrc"
install_file dotfiles/AGENTS.md "$HOME/AGENTS.md"
CODEX_CONFIG_DIR="${CODEX_HOME:-$HOME/.codex}"
mkdir -p "$HOME/.gemini/config" "$CODEX_CONFIG_DIR"
ln -sf "$HOME/AGENTS.md" "$HOME/.gemini/config/AGENTS.md"
ln -sf "$HOME/AGENTS.md" "$CODEX_CONFIG_DIR/AGENTS.md"

mkdir -p "$HOME/.config/uv"
install_file dotfiles/uv.toml "$HOME/.config/uv/uv.toml"

# Git config, ignore, and hooks
mkdir -p "$HOME/.config/git/hooks"
install_file dotfiles/git/config "$HOME/.config/git/config"
install_file dotfiles/git/ignore "$HOME/.config/git/ignore"
install_script dotfiles/git/hooks/commit-msg "$HOME/.config/git/hooks/commit-msg"

# Download bash profile into separate file and overwrite contents
install_file common/profile.bash "$HOME/.profile.bash"

BASH_RC="$HOME/.bashrc"
if [ "$(uname -s)" = "Darwin" ]; then
    BASH_RC="$HOME/.bash_profile"
fi
[ -e "$BASH_RC" ] || touch "$BASH_RC"

# Bash profile (add ref to existing one)
PROFILE_REF='source ~/.profile.bash'
if ! grep -Fqx "$PROFILE_REF" "$BASH_RC"; then
    cp "$BASH_RC" "${BASH_RC}-$(date '+%Y-%m-%d-%s')"
    echo "$PROFILE_REF" >> "$BASH_RC"
fi

# Create local bin dir and move some utils there
BIN_DIR="$HOME/.local/bin"
mkdir -p "$BIN_DIR"

# Remove commands installed by older versions of initscripts.
rm -f "$BIN_DIR/fakesudo" "$BIN_DIR/apti" "$BIN_DIR/pkg-install" "$BIN_DIR/aidevenv"
if [ -d "$HOME/.local/lib/node_modules/npm" ]; then
    PATH="$BIN_DIR:$PATH" npm uninstall -g npm
fi

# Common scripts.
install_script common/dl.sh "$BIN_DIR/dl"
install_script common/secrets_sync.sh "$BIN_DIR/secrets-sync"
install_script common/skill_sync.sh "$BIN_DIR/skill-sync"
install_script common/agy-user.sh "$BIN_DIR/agy-user"
install_script common/workdir.sh "$BIN_DIR/workdir"
install_script common/as-root.sh "$BIN_DIR/as-root"
install_script common/pkgi.sh "$BIN_DIR/pkgi"
install_script common/update.sh "$BIN_DIR/update"
install_script common/ssh-tmux.sh "$BIN_DIR/ssh-tmux"

# Personal agent skills, linked into each agent
SKILLS_DIR="$HOME/.agents/skills"
for skill in travel-agent aislop; do
    mkdir -p "$SKILLS_DIR/$skill"
    install_file "skills/$skill.md" "$SKILLS_DIR/$skill/SKILL.md"
done
"$BIN_DIR/skill-sync"

# Output success
echo "Profile set. Run the following command to update your current shell:"
echo "exec bash --login"
