# Maintainer: Erich Eckner <arch at eckner dot net>
pkgname=openttd-git
pkgver=31531.19af13963
_commit=${pkgver#*.}
pkgrel=2
pkgdesc="A FOSS clone of Transport Tycoon Deluxe."
arch=('x86_64' 'i686' 'pentium4')
url="http://www.openttd.org"
license=('GPL2')
groups=()
_pinned_dependencies=(
  'gcc-libs=12.1.1'
  'glibc>=2.31'
  'libfluidsynth.so=3'
  'libfontconfig.so=1'
  'libfreetype.so=6'
  'libicui18n.so=71'
  'libicuuc.so=71'
  'libpng=1.6.37'
  'lzo=2.10'
  'sdl2=2.0.22'
  'xz=5.2.5'
  'zlib=1:1.2.12'
)
depends=(
  'hicolor-icon-theme'
  "${_pinned_dependencies[@]}"
)
makedepends=('git' 'cmake')
checkdepends=(
    'openttd-opengfx'
)
optdepends=(
    'openttd-opengfx: free graphics'
    'openttd-opensfx: free soundset'
    'openttd-openmsx: free music'
)
provides=('openttd')
conflicts=('openttd')
replaces=()
backup=()
options=()
source=(
    "${pkgname}::git+https://github.com/OpenTTD/OpenTTD.git#commit=${_commit}"
#    "chunnel-signaltunnel-pre.patch"
#    "chunnel-underground-pre.patch"
#    "chunnel.patch"
    "signaltunnel.patch"
#    "sloped-stations.patch"
#    "underground.patch"
    'opntitle.dat'
)
sha512sums=('SKIP'
            '2595106d104a6e0247e6b62c8ff7a3ece452a6397459121616f46f9bf68824b276a05fd4dccfce1f9c27c41784805f583e04f2e7250ffa572855dd45eac8e979'
            'a3fd5b230b7cdd67a1b6607887433c0cb85ba1dfd30cedfddf573a58b23ccce7c470a22cf7f65a29c173bba12976c5889482cef24f49c8c0a829883a71bcd87d')

pkgver() {
  printf '%s.%s\n' \
    "$((
      $(git -C "${srcdir}/${pkgname}" rev-list --count "${_commit}" ^f84ad5f7c) + 28004
    ))" \
    "$(
      git -C "${srcdir}/${pkgname}" rev-parse --short=9 "${_commit}"
    )"
}

prepare() {

    cd "${srcdir}"
#    cp chunnel.patch chunnel.pre.patch
#    patch -p1 -i "chunnel-signaltunnel-pre.patch"
#    patch -p1 -i "chunnel-underground-pre.patch"

    cd "${srcdir}/${pkgname}"
    cp "${srcdir}/opntitle.dat" media/baseset/

    ISODATE=$(
      date -d@$(
        git log -n1 --pretty=format:%ct
      ) +'%Y%m%d'
    )
    HASH=$(find {src,bin} -type f -exec sha512sum {} \; | sort | sha512sum - | cut -d' ' -f1)
    SHORTHASH=$(echo ${HASH} | cut -c1-8)
    printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
      "${ISODATE}-ewe-g${SHORTHASH}" \
      "${ISODATE}" \
      "0" \
      "${HASH}" \
      "0" \
      "0" \
      "${ISODATE:0:4}" \
      > .ottdrev
    sed -i '
      s,"\${CMAKE_SOURCE_DIR}/\.git","/dev/does/not/exist",
    ' cmake/scripts/FindVersion.cmake

#    for _p in "${srcdir}/"{signaltunnel,sloped-stations,underground,chunnel.pre}.patch; do
    for _p in "${srcdir}/signaltunnel.patch"; do
      >&2 echo "patching ${_p##*/} ..."
      patch -p1 -i "${_p}"
      >&2 echo "... ok"
    done
}

build() {

    cd "${srcdir}/${pkgname}"
    mkdir build
    cd build
    cmake \
      -DCMAKE_INSTALL_PREFIX=/usr \
      -DPERSONAL_DIR=.openttd \
      ..
    make

#      -DBINARY_DIR=bin \
#      -DDATA_DIR=share/openttd \
#      -DICON_DIR=share/openttd \
#      -DMAN_DIR=share/man \

}

check() {
    cd "${srcdir}/${pkgname}/build"
    make test || true
}

package() {

    cd "${srcdir}/${pkgname}/build"
    make DESTDIR=$pkgdir install

    # Remove unnecessary languages
    cp $pkgdir/usr/share/games/openttd/lang/{english,german}.lng $srcdir
    rm $pkgdir/usr/share/games/openttd/lang/*
    install -m 644 $srcdir/{english,german}.lng $pkgdir/usr/share/games/openttd/lang

    # Remove junk
    rm -rf $pkgdir/usr/share/games/doc
    rm -rf $pkgdir/usr/share/games/openttd/scripts

}