summaryrefslogtreecommitdiff
path: root/makepak.sh (plain)
blob: 1681cd6b779f10022b9798c153a9d45c87545bd7
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
#!/bin/bash

if [ "$BASH_VERSION" ] && [ "${BASH_VERSION:0:1}" == "2" ]; then
  echo "ERROR: too old bash version"
  if [ -f /msys.bat ]; then
    echo -n "Download a msysCORE update from <http://sf.net/projects/mingw>"
  fi
  exit 1
fi

zip2pak () {   # usage: zip2pak file.zip >file.pak
  # .pak files are actually simple .zip archives. to make it harder to
  # open them and prevent spoiling resourceful players, this obfuscates
  # them by replacing some bytes at the beginning and in the EOCD header.
  # (NOTE: this assumes there's no zip comment.)
  echo -n $'ULT\4'; tail -c+5 "$1" | head -c-22
  echo -n $'UL\5T'; tail -c18 "$1"
}
pak2zip () {   # usage: pak2zip file.pak >file.zip
  echo -n $'PK\3\4'; tail -c+5 "$1" | head -c-22
  echo -n $'PK\5\6'; tail -c18 "$1"
}
makepak () {   # usage: makepak file.info [--zip]
  local B; B="${1##*/}"; B="${B%.*}"
  echo "Creating ${1%.*}.pak"
  rm -f "${1%.*}.pak"
  local oldwd="`pwd`"; cd "`dirname "$1"`"
  read line < "$B.info" || return 1
  if [ "${line#-- FILES }" != "$line" ]; then eval "FILES=(${line#-- FILES })"
  else FILES=(*); fi
  ${ZIP:-zip -r -D} "$B.pak" "$B.info" "${FILES[@]}"
  if [ "$2" != "--zip" ]; then
    mv "$B.pak" "$B.pak~" && zip2pak "$B.pak~" >"$B.pak" && rm "$B.pak~"; fi
  cd "$oldwd"
}

[ -f "$1" ] && [ ! "$2" ] && set - --makepak "$@"
if [ "$1" != "${1#--}" ] && [ "`type -t "${1#--}"`" == "function" ]; then
  fn=${1#--}; shift; "$fn" "$@"
elif [ "$1" == "" ]; then
  [ -f ../Makefile -a ! -d data ] && dir=../data || dir=data
  find "$dir" -name '*.info' -exec "$0" {} \;
else
  echo "Usage: $0 [ [--makepak] input.info | --zip2pak input.zip >output.pak | --pak2zip input.pak >output.zip ]"
fi