about summary refs log tree commit diff
path: root/pkgs/games/dwarf-fortress/wrapper/dwarf-fortress-init.in
blob: 1c739a1afbbf0d5db53f22cbccb3e6debd33fb11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!@stdenv_shell@ -e
set -euo pipefail
shopt -s extglob

export NIXPKGS_DF_ENV="@env@"

### BEGIN: Default DF options
declare -A _NIXPKGS_DF_OPTS
_NIXPKGS_DF_OPTS[fmod]=0    # Don't use fmod by default.
_NIXPKGS_DF_OPTS[debug]=0   # No debugging output by default.
### END: Default DF options

# Read NIXPKGS_DF_OPTS.
if [[ ! -v NIXPKGS_DF_OPTS ]]; then
  NIXPKGS_DF_OPTS=''
fi
IFS=',' read -ra options <<< "$NIXPKGS_DF_OPTS"
for option in ${options[@]+"${options[@]}"}; do
  key="${option%=*}"
  value="${option##*=}"
  if [ -n "$key" ]; then
    if [ -z "$value" ] || [ "$key" == "$value" ]; then
      value=1
    fi
    _NIXPKGS_DF_OPTS["$key"]="$value"
  fi
done

# Rebuild the canonical option string from the read options.
NIXPKGS_DF_OPTS=''
for key in "${!_NIXPKGS_DF_OPTS[@]}"; do
  value="${_NIXPKGS_DF_OPTS["${key}"]}"
  NIXPKGS_DF_OPTS="$NIXPKGS_DF_OPTS$key=$value,"
done
NIXPKGS_DF_OPTS="${NIXPKGS_DF_OPTS%,}"

# Echoes a log.
# $@: log messages
log() {
  for msg in "$@"; do
    echo "[nixpkgs] $msg" >&2
  done
}

# Echoes a log if NIXPKGS_DF_OPTS includes debug.
# $@: log messages
debug() {
  if [ "${_NIXPKGS_DF_OPTS[debug]}" -ne 0 ]; then
    log "$@"
  fi
}

# Updates a path in $NIXPKGS_DF_HOME from $NIXPKGS_DF_ENV.
# $1: The environment path.
update_path() {
  local path="$1"
  local orig="$NIXPKGS_DF_ENV/$path"
  local final="$NIXPKGS_DF_HOME/$path"

  # If user has replaced these data directories, let them stay.
  @mkdir@ -p "$(dirname -- "$final")"
  if [ ! -e "$final" ] || [ -L "$final" ]; then
    debug "Linking: $final -> $orig"
    @rm@ -f "$final"
    @ln@ -s "$orig" "$final"
  else
    debug "Not updating: $final"
  fi
}

# Cleans up a path in $NIXPKGS_DF_HOME that may or may not be in $NIXPKGS_DF_ENV.
# $1: The environment path.
cleanup_path() {
  local path="$1"
  local final="$NIXPKGS_DF_HOME/$path"

  # Let them stay if not a link.
  if [ ! -e "$final" ] || [ -L "$final" ]; then
    debug "Cleaning up: $final"
    @rm@ -f "$final"
  else
    debug "Not cleaning: $final"
  fi
}

# Force copies a path in $NIXPKGS_DF_HOME that may or may not be in $NIXPKGS_DF_ENV.
# $1: The environment path.
forcecopy_path() {
  local path="$1"

  if [ -z "$NIXPKGS_DF_ENV" ] || [ -z "$path" ]; then
    # Avoid producing "/" for any `rm -rf`
    return
  fi

  local orig="$NIXPKGS_DF_ENV/$path"
  local final="$NIXPKGS_DF_HOME/$path"

  if [ -e "$orig" ]; then
    debug "Force copying: $orig -> $final"
    @mkdir@ -p "$(dirname -- "$final")"
    @rm@ -rf "$final"
    @cp@ -rL --no-preserve=all "$orig" "$final"
  else
    debug "Removing: $final"
    @rm@ -rf "$final"
  fi
}

# Runs the final executable. Expects NIXPKGS_DF_HOME and NIXPKGS_DF_EXE to be set.
go() {
  cd "$NIXPKGS_DF_HOME"
  debug "Executing: $NIXPKGS_DF_HOME/$NIXPKGS_DF_EXE"

  # If we make it past here, we want to log.
  # shellcheck disable=SC2093
  exec -a "$NIXPKGS_DF_EXE" "$NIXPKGS_DF_HOME/$NIXPKGS_DF_EXE"
  log "Execution of $NIXPKGS_DF_HOME/$NIXPKGS_DF_EXE failed!"
  exit 1
}

os_name="$(@uname@)"
os_rev="$(@uname@ -r)"

if [ "$os_name" == Linux ]; then
  df_dir="df_linux"
elif [ "$os_name" == Darwin ]; then
  df_dir="df_osx"
else
  log "Unknown platform: $os_name"
  exit 1
fi

if [[ -v DF_DIR ]] && [ -n "$DF_DIR" ] && { [[ ! -v NIXPKGS_DF_HOME ]] || [ -z "$NIXPKGS_DF_HOME" ]; }; then
  # Compatibility for users that were using DF_DIR, since the dfhack script clobbers this variable.
  export NIXPKGS_DF_HOME="$DF_DIR"
fi

if [[ ! -v NIXPKGS_DF_HOME ]] || [ -z "$NIXPKGS_DF_HOME" ]; then
  export NIXPKGS_DF_HOME="${XDG_DATA_HOME:-$HOME/.local/share}/$df_dir"
fi

# Compatibility.
export DF_DIR="$NIXPKGS_DF_HOME"

@mkdir@ -p "$NIXPKGS_DF_HOME"

@cat@ <<EOF >&2
/------------------------------------------------------------------------------\\
| Hello from the nixpkgs Dwarf Fortress wrapper!                               |
|                                                                              |
| Using the following Dwarf Fortress overlay directory as NIXPKGS_DF_HOME:     |
| $(@printf@ '% -76s' "$NIXPKGS_DF_HOME") |
|                                                                              |
| If you make any changes in it, don't forget to clean it when updating the    |
| game version! We detect changes if data directories are symbolic links.      |
|                                                                              |
| Even though we do our best on our own, this script may miss some. Submit a   |
| pull request if there are any that become a problem.                         |
|                                                                              |
| We started with the following nixpkgs launch options as NIXPKGS_DF_OPTS:     |
| $(@printf@ '% -76s' "$NIXPKGS_DF_OPTS") |
|                                                                              |
| If you want to try fmod over SDL_mixer, set NIXPKGS_DF_OPTS=fmod.            |
\\------------------------------------------------------------------------------/
EOF

cd "$NIXPKGS_DF_ENV"

# 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" == libfmod*.so* ]] && [ "${_NIXPKGS_DF_OPTS[fmod]}" -eq 0 ]; then
    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

# Handle library paths on Darwin.
if [ "$os_name" == Darwin ]; then
  if [ "${os_rev%%.*}" -ge 11 ]; then
    export DYLD_LIBRARY_PATH="$NIXPKGS_DF_ENV/libs"
    export DYLD_FRAMEWORK_PATH="$NIXPKGS_DF_ENV/libs"
  else
    export DYLD_FALLBACK_LIBRARY_PATH="$NIXPKGS_DF_ENV/libs"
    export DYLD_FALLBACK_FRAMEWORK_PATH="$NIXPKGS_DF_ENV/libs"
  fi
fi