import 'compile/cmake'

name=nxengine-evo
version=2.4.1
update_url="https://github.com/isage/$name/releases"
update_names="/archive/v$version.tar.gz"

define_option 'console_key=`: The key used to access the debug console'

dlextract "https://github.com/isage/$name/archive/v$version/$name-$version.tar.gz" \
          'dd1b27f8ebda24263bfe9bbe3d5b092c'
download "http://www.cavestory.org/downloads/cavestoryen.zip" \
         '5aad47f1cb72185d6e7f4c8c392f6b6e'

extrafile "$name.desktop"
extrafile "$name.png"
header_end

# Copy Cave Story executable and data to the source directory.
extract 'cavestoryen.zip' '' 'unzip -qd {dst} {src}'
cp -an CaveStory/data CaveStory/Doukutsu.exe "$name-$version"

cd "$name-$version"

# Some keyboards don't have the default key accessible. A saner default would be '.' or '-'.
# Since it's kind of up to the player, I just shittily patch it here.
console_key="$(safe_sed $(option =console_key))"
sed -i -e "/if (key == '\`')/s/\`/$console_key/" src/input.cpp
sed -i -e "/case '\`':/s/\`/$console_key/" src/console.cpp

# Build the source
compile_cmake -I

# Extract extra data from Doukutsu.exe
./bin/extract

# Install game
install -Dm755 -t "$dir_install/$dir_prefix/$dir_libdir/$name" bin/nx
mkdir -p "$dir_install/$dir_prefix/$dir_bindir"
ln -s "/$dir_prefix/$dir_libdir/$name/nx" "$dir_install/$dir_prefix/$dir_bindir/nxengine-evo"

# Install data
mkdir -p "$dir_install/$dir_prefix/$dir_datadir/$name"
cp -aT data "$dir_install/$dir_prefix/$dir_datadir/$name"
ln -s "/$dir_prefix/$dir_datadir/$name" "$dir_install/$dir_prefix/$dir_libdir/$name/data"

# Install desktop file and icon
install -Dm644 -t "$dir_install/$dir_prefix/$dir_datadir/applications" "../$name.desktop"
install -Dm644 -t "$dir_install/$dir_prefix/$dir_datadir/pixmaps" "../$name.png"

# vim:set tabstop=4 shiftwidth=4 syntax=sh expandtab: