#!/bin/sh

set -e

if ! whoami &> /dev/null; then
  if [ -w /etc/passwd ]; then
    echo "${USER_NAME:-user}:x:$(id -u):0:${USER_NAME:-user} user:${HOME}:/bin/bash" >> /etc/passwd
    echo "${USER_NAME:-user}:x:$(id -u):" >> /etc/group
  fi
fi

ls -la /neovim/

export MACHINE_EXEC_PORT=3333
nohup /neovim/bin/machine-exec --url "127.0.0.1:${MACHINE_EXEC_PORT}" &

ldd_output=$(ldd /bin/ls 2>/dev/null || true)
case "$ldd_output" in
  *musl*)
    echo "[INFO] musl libc detected, using musl binaries"
    NVIM_DIR=/neovim/neovim-linux-musl
    export LD_LIBRARY_PATH="${NVIM_DIR}/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
    ;;
  *)
    echo "[INFO] glibc detected, using libc binaries"
    NVIM_DIR=/neovim/neovim-linux-libc
    ;;
esac

export PATH="${NVIM_DIR}/bin:/neovim/bin:${PATH}"
export TERM=xterm-256color
if [ -d /neovim/lib64-zsh ]; then
  cat > "$HOME/.zshenv" << 'ZSHENV'
module_path=( /neovim/lib64-zsh/5.8 $module_path )
fpath=( /neovim/share-zsh/5.8/functions /neovim/share-zsh/site-functions $fpath )
ZSH_DISABLE_COMPFIX=true
ZSHENV
  echo "[INFO] wrote .zshenv for zsh module paths"
fi

if [ -n "$DOTFILES_REPO" ]; then
  DOTFILES_TMP=$(mktemp -d)
  CLONE_URL="$DOTFILES_REPO"
  if [ -n "$DOTFILES_TOKEN" ]; then
    CLONE_URL=$(echo "$DOTFILES_REPO" | sed "s|https://|https://${DOTFILES_TOKEN}@|")
  fi
  echo "[INFO] cloning dotfiles from $DOTFILES_REPO"
  git clone --depth 1 --recurse-submodules --shallow-submodules "$CLONE_URL" "$DOTFILES_TMP" 2>&1 || echo "[WARN] dotfiles clone failed"

  if [ -d "$DOTFILES_TMP/.config" ]; then
    cp -r "$DOTFILES_TMP/.config" "$HOME/"
    echo "[INFO] dotfiles .config applied"
  fi

  for f in "$DOTFILES_TMP"/dot_*; do
    [ -e "$f" ] || continue
    name=$(basename "$f" | sed 's/^dot_/\./')
    cp -r "$f" "$HOME/$name"
    echo "[INFO] dotfiles: $name"
  done

  rm -rf "$DOTFILES_TMP"

  # rename chezmoi-prefixed files in copied dotfiles (executable_, dot_, readonly_)
  find "$HOME" -maxdepth 8 \( -name 'executable_*' -o -name 'dot_*' -o -name 'readonly_*' -o -name 'empty_*' \) 2>/dev/null | while read -r chezmoi_file; do
    dir=$(dirname "$chezmoi_file")
    base=$(basename "$chezmoi_file")
    newname=$(echo "$base" | sed -e 's/^executable_//' -e 's/^readonly_//' -e 's/^empty_//' -e 's/^dot_/./')
    [ "$base" = "$newname" ] && continue
    target="$dir/$newname"
    [ -e "$target" ] && continue
    cp -r "$chezmoi_file" "$target"
    case "$base" in executable_*) chmod +x "$target" ;; esac
  done
fi


if [ -n "$NVIM_DISTRO" ] && [ ! -d "$HOME/.config/nvim" ]; then
  /neovim/bootstrap-distro.sh
fi

if [ -d "$HOME/.config/nvim" ]; then
  echo "[INFO] running headless plugin sync"
  "${NVIM_DIR}/bin/nvim" --headless "+Lazy! sync" +qa 2>&1 || echo "[WARN] plugin sync failed"
  echo "[INFO] plugin sync done"
fi

WORKDIR="${PROJECT_SOURCE:-/projects}"
mkdir -p "$WORKDIR"
cd "$WORKDIR"

TTYD_OPTS="-W -p 7681 -t fontSize=14"
if [ -f /neovim/neovim-index.html ]; then
  TTYD_OPTS="$TTYD_OPTS -I /neovim/neovim-index.html -t fontFamily='JetBrainsMono NF Mono, monospace'"
else
  TTYD_OPTS="$TTYD_OPTS -t fontFamily=monospace"
fi

NEOCHE_SHELL="${NEOCHE_SHELL:-}"
if [ -n "$NEOCHE_SHELL" ] && command -v "$NEOCHE_SHELL" > /dev/null 2>&1; then
  echo "[INFO] launching ttyd with $NEOCHE_SHELL"
  eval exec /neovim/bin/ttyd $TTYD_OPTS "$NEOCHE_SHELL" -c \"nvim .\"
else
  eval exec /neovim/bin/ttyd $TTYD_OPTS nvim .
fi
