summaryrefslogtreecommitdiffstats
path: root/games/nblood/extract-blood-data.sh
blob: 374d2a13e558bedab0154b61e199572240b3e724 (plain)
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
#!/bin/bash

# extract-blood-data.sh - extract the game data from a mounted Blood
# CD-ROM or (not mounted) .iso file in the current directory.

# By B. Watson <yalhcru@gmail.com>, released under the WTPL: Do WTF you
# want with this.

if [ "$( id -u )" != "0" ]; then
  echo "$(basename $0) has to run as root because it needs to mount ISO images"
  exit 1
fi

# Copy the game data here
DEST=${1:-./blood}
DEST="$( readlink -f "$DEST" )"

# Deal with filenames case-insensitively
shopt -s nocaseglob

# Return true if directory $1 is a valid copy of the game.
contents_ok() {
  local mntpnt="$1"

  [ -e "$mntpnt/blood.ini"* ] && \
  [ -e "$mntpnt/data.z"* ] && \
  [ -e "$mntpnt/readme.txt"* ] && \
  head -n1 "$mntpnt/readme.txt"* | grep -q '^One Unit: WHOLE BLOOD(TM) v 1.21'

  return $?
}

# Try to find and mount a Blood ISO image, in the current directory.
find_iso() {
  local mntpnt="$1"
  local iso

  mkdir -p "$mntpnt"

  for iso in *; do
    [ -e "$iso" ] || continue
    file -L --mime -b "$iso" | grep -qi 'iso.*9660' || continue

    echo -n "Trying '$iso'... "
    if /sbin/mount -o ro,loop -t iso9660 "$iso" "$mntpnt"; then
      if contents_ok "$mntpnt"; then
        echo "found One Unit Whole Blood ISO."
        return 0
      fi
      echo "mounted, but not a Blood ISO."
      /sbin/umount "$mntpnt"
    else
      echo "couldn't mount."
    fi
  done

  /sbin/umount "$mntpnt" &> /dev/null
  echo "Couldn't find One Unit Whole Blood ISO in $( pwd )"
  return 1
}

# Extract the data we need. We might be including a little more
# that necessary here.
extract_data() {
  local src="$1"
  local dst="$2"
  local tmpdir

  echo "Extracting and copying data from $src to $dst"

  mkdir -p "$dst"
  tmpdir="$( mktemp -d ${TMP:-/tmp}/bloodtmp.XXXXXX )"

  # most of the stuff we need is in data.z
  isextract x "$src/data.z"* "$tmpdir"
  cd "$tmpdir"
    cp -a *.rff* \
          *.dem* \
          *.art* \
          *.dat* \
          "$dst"
  cd -

  # ...but not all of it
  cp -a "$src/movie"* "$src/cryptic/"* "$src/blood.ini"* "$src/readme.txt"* "$dst"

  # get rid of unnecessary cruft not used by nblood
  rm -rf "$dst"/cryptic.exe*   \
         "$dst"/movie/directx* \
         "$dst"/movie/amovie* \
         "$dst"/movie/_* \
         "$dst"/movie/*.exe* \
         "$dst"/movie/*.ins*

  find "$dst" -type f -exec chmod 644 {} \+
  chmod 755 "$dst/movie"*

  rm -rf "$tmpdir"
}

# main()
if ! which isextract &>/dev/null; then
  echo "Can't find isextract on PATH. Please install it and re-run this script."
  exit 1
fi

# Try to find a mounted CD
CDROM=""
cat /proc/mounts | while read line; do
  t="$( echo "$line" | cut -d' ' -f3 )"
  m="$( echo "$line" | cut -d' ' -f2 )"
  if [ "$t" = "iso9660" ]; then
    echo -n "Trying mount point '$m'..."
    if contents_ok "$m"; then
      echo OK
      CDROM="$m"
      break
    fi
    echo "not a Blood CD"
  fi
done

if [ -n "$CDROM" ]; then
  echo "Found One Unit Whole Blood CD-ROM mounted on $CDROM"
else
  CDROM="$( mktemp -d ${TMP:-/tmp}/bloodcd.XXXXXX )"
  RMTMP="$CDROM"
  if ! find_iso "$CDROM"; then
    echo "Couldn't find any game data"
    exit 1
  fi
fi

extract_data "$CDROM" "$DEST"

if [ -n "$RMTMP" ]; then
  umount "$RMTMP" &>/dev/null
  rmdir "$RMTMP"
fi