#!@stdenv_shell@ -e

export NIXPKGS_DF_EXE="@dfExe@"
export NIXPKGS_DF_GAME=1
source @dfInit@

# All potential important files in DF 50 and below.
for path in dwarfort dwarfort.exe df *.so* libs raw data/init/* data/!(init|index|announcement); do
  force_delete=0
  if [[ "$path" == *fmod*.so* ]] && [ "${_NIXPKGS_DF_OPTS[fmod]}" -eq 0 ]; then
    # Delete fmod plugins if we're using SDL_mixer.
    force_delete=1
  elif [[ "$path" == *mixer*.so* ]] && [ "${_NIXPKGS_DF_OPTS[fmod]}" -ne 0 ]; then
    # Delete SDL_mixer plugins if we're using fmod.
    force_delete=1
  fi

  if [ -e "$path" ] && [ "$force_delete" -eq 0 ]; then
    update_path "$path"
  else
    cleanup_path "$path"
  fi
done

# These need to be copied due to read only flags on older versions of DF.
for path in index announcement help dipscript; do
  forcecopy_path "data/$path"
done

# If we're switching back from dfhack to vanilla, cleanup all dfhack
# links so Dwarf Fortress doesn't autoload its leftover libdfhooks.so.
# Otherwise, populate them.
dfhack_files=(
  dfhack
  dfhack-run
  .dfhackrc
  libdfhooks.so
  dfhack-config/default
  dfhack-config/init
  dfhooks_*.ini
  hack/*
  stonesense/*
  *.init *.init-example
)

# May be stale top-level libdfhooks_*.so symlinks in $NIXPKGS_DF_HOME, fix them
shopt -s nullglob
for stale in "$NIXPKGS_DF_HOME"/libdfhooks_*.so; do
  dfhack_files+=( "${stale##*/}" )
done
shopt -u nullglob

if [ "${NIXPKGS_DF_EXE##*/}" == dfhack ]; then
  for i in "${dfhack_files[@]}"; do
    if [ -e "$i" ]; then
      update_path "$i"
    else
      cleanup_path "$i"
    fi
  done
else
  for i in "${dfhack_files[@]}"; do
    cleanup_path "$i"
  done
fi

go "$@"
