# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Vesa Kaihlavirta <vesa@archlinux.org>
# Contributor: Thomas Dziedzic <gostrc@gmail.com>

# Special note for devs looking to upgrade this package:
#   ghc places a unique hash for each library when it is built.
#   Libraries depend on versions specified by those hashes.
#   This implies that all libraries need to be rebuilt when ghc is rebuilt.

shopt -s extglob

pkgbase=ghc
pkgname=(ghc-libs ghc ghc-static)
pkgver=9.2.8
pkgrel=1.2
arch=(host)
cross=ncxf
pkgdesc='The Glasgow Haskell Compiler'
url='https://www.haskell.org/ghc/'
license=('custom')
ncxfat_makedepends=('gcc' 'binutils' 'glibc' 'gmp' 'libffi' 'ncurses')
ncxfab_makedepends=('ghc' 'ghc-static' 'perl' 'make' 'happy' 'alex' 'patch'
                    'cabal-install' 'autoconf' 'automake' 'python' 'python-sphinx' 'libedit'
                    'numactl' 'llvm14' 'ghc-llvm-bin-symlinks')

# ncxfab_makedepends=('ghc-static' 'perl' 'libxslt' 'docbook-xsl' 'python-sphinx' 'haskell-hadrian'
#                     'haskell-hscolour' 'texlive-fontsrecommended' 'texlive-latexextra' 'texlive-xetex'
#              'ttf-dejavu' 'alex' 'happy' 'time' 'numactl' 'python-sphinx_rtd_theme')
source=("https://downloads.haskell.org/~ghc/$pkgver/$pkgbase-${pkgver}-src.tar.xz"
        ghc-sphinx-6.patch::https://gitlab.haskell.org/ghc/ghc/-/commit/00dc51060881df81258ba3b3bdf447294618a4de.patch
        ghc-rebuild-doc-index.hook ghc-register.hook ghc-unregister.hook
        enable-xhtml.patch)
sha512sums=('SKIP' #'c1159741725deaa00be7c230b38d43aed0fd456621fb4bc434434f13b03b74da1f91c09ce5dad553cf535f14038d8568fce7f21ea068680211b8bd35eafacf49'
            'a7d6fe02b8d75d2d08bd0df64c4ac03c40fde598cf19b0c7cb7c293dd62a7ffab574817cee1936dcf9323a98f6280cf81f3c38e78c645ffa701cf3b04bba30e9'
            '940e58508a34bd5ec6a58dba2e60dd28be78138b2e820fc45b115879335d4a160dafcac5c68bf5d19300d6d403f80a0a8c783a6dd187fb690c6608888c21effe'
            '5f659651d8e562a4dcaae0f821d272d6e9c648b645b1d6ab1af61e4dd690dc5a4b9c6846753b7f935963f001bb1ae1f40cd77731b71ef5a8dbc079a360aa3f8f'
            '3bdbd05c4a2c4fce4adf6802ff99b1088bdfad63da9ebfc470af9e271c3dd796f86fba1cf319d8f4078054d85c6d9e6a01f79994559f24cc77ee1a25724af2e6'
            'SKIP')

prepare() {
  cd ghc-$pkgver
  patch -p1 -i ../ghc-sphinx-6.patch
  patch -Np1 < ../enable-xhtml.patch

  # devendor rtd-theme for sphinx compatibility
  rm -r docs/users_guide/rtd-theme
  sed -i 's/rtd-theme/sphinx_rtd_theme/' docs/users_guide/conf.py

  # detects GCC correctly
  sed -i 's/grep -q gcc/grep -qi gcc/' m4/fp_gcc_version.m4

  cp mk/build.mk{.sample,}
  if (( MPKG_CROSSCOMPILED )); then
      sed -i '1iBuildFlavour = perf-cross' mk/build.mk
  else
      sed -i '1iBuildFlavour = perf' mk/build.mk
  fi
  ./boot
}

build() {
  cd ghc-$pkgver

  local _config_opts=(
    --prefix=/usr
    --target=$MPKG_TARGETMACHINE
    --docdir=/usr/share/doc/ghc
    --with-system-libffi
  )
  
  LDFLAGS="${LDFLAGS/-Wl,-z,pack-relative-relocs/}" ./configure \
         "${_config_opts[@]}"

  if [[ x"$MPKG_TARGETMACHINE" == x"$MPKG_HOSTMACHINE" ]]; then
      # Enable shared libraries and dynamic linking
      sed -i 's/\(DYNAMIC_GHC_PROGRAMS\s*=\s*\)NO/\1YES/' mk/flavours/perf-cross.mk
  fi

  # Don't bother building profiling libraries for this bootstrap
  sed -i 's/\(BUILD_PROF_LIBS\s*=\s*\)YES/\1NO/' mk/flavours/perf-cross.mk
  
  # ghc has deep-rooted dependency problems that prevent it from
  # building from clean. Rather than go endlessly down the rabbit hole
  # of fixing something that upstream clearly cannot fix (hence all
  # the new alternative build systems), just build it twice and be
  # done.
  make || /bin/true

  make

  if [[ x"$MPKG_TARGETMACHINE" == x"$MPKG_HOSTMACHINE" ]]; then
      sed -i 's/\(Stage1Only\s*=\s*\)YES/\1NO/' mk/flavours/perf-cross.mk
  fi
}

check() {
    cd ghc-$pkgver
    # hadrian test --flavour=perf
}

package_ghc-static() {
  pkgdesc='The Glasgow Haskell Compiler - Static Libraries and Documentation'
  ncxfat_depends=('ghc')

  cd ghc-$pkgver

  DESTDIR="$pkgdir" make install

  mv "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/package.conf.d "$srcdir"/static-package.conf.d
  find "$pkgdir"/usr/lib ! \( -name "*.a" -o -name "*.o" -o -name "*.p_o" -o -name "*.p_hi" -o -name "*.hi" \) -type f -delete
  find "$pkgdir"/usr/lib -type d -empty -delete

  mv "$srcdir"/static-package.conf.d "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/
  #rm "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/lib/static-package.conf.d/*.copy

  rm -r "$pkgdir"/usr/bin # "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/bin 

  if [[ x"$MPKG_USRPATH" != x"/usr" ]]; then
      mkdir -p $pkgdir/$MPKG_USRPATH
      mv $pkgdir/usr/share $pkgdir/$MPKG_USRPATH/
  fi

  install -Dm755 libraries/{gen_contents_index,prologue.txt} -t "$pkgdir"$MPKG_USRPATH/share/doc/ghc/html/libraries/
  install -Dm644 "$srcdir"/ghc-rebuild-doc-index.hook -t "$pkgdir"$MPKG_USRPATH/share/libalpm/hooks/

  install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}

package_ghc() {
  pkgdesc='The Glasgow Haskell Compiler'
  ncxfat_depends=('gcc' 'ghc-libs')
  xat_depends=('ghc-binutils-symlinks')
  provides=( #'haskell-haddock=2.26.0'
            'haskell-hp2ps=0.1'
            'haskell-hpc-bin=0.68'
            'haskell-hsc2hs=0.68.8')
  replaces=( #'haskell-haddock'
            'haskell-hp2ps'
            'haskell-hpc-bin'
            'haskell-hsc2hs')

  provides+=("haskell-ghc=$pkgver")
  replaces+=("haskell-ghc")

  cd ghc-$pkgver
  DESTDIR="$pkgdir" make install

  # Remove static libs
  find "$pkgdir"/usr/lib \( -name "*.a" -o -name "*.o" -o -name "*.p_o" -o -name "*.p_hi" -o -name "*.hi" \) -delete

  # # ghc-pkg is in ghc-libs
  rm "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/bin/ghc-pkg*
  rm "$pkgdir"/usr/bin/${MPKG_PKGPREFIX}ghc-pkg*

  if [[ x"$MPKG_USRPATH" == x"/usr" ]]; then
      (cd "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/; rm -r !(ghc-$pkgver|bin|*.h|*.txt|include))
  fi

  # # docs moved to ghc-static
  rm -r "$pkgdir"/usr/share/doc

  # install -Dm644 _build/docs/users_guide/build-man/*.1 -t "$pkgdir"/usr/share/man/man1/

  install -Dm644 utils/completion/ghc.bash \
          "$pkgdir$MPKG_USRPATH/share/bash-completion/completions/ghc"

  install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}

package_ghc-libs() {
  pkgdesc='The Glasgow Haskell Compiler - Dynamic Libraries'
  install='ghc.install'
  ncxfat_depends=('gmp' 'libffi')
  ncxfah_depends=( 'perl' 'numactl')
  provides=('haskell-array=0.5.4.0'
            'haskell-base=4.16.4.0'
            'haskell-binary=0.8.9.0'
            'haskell-bytestring=0.11.4.0'
            'haskell-cabal=3.6.3.0'
            'haskell-containers=0.6.5.1'
            'haskell-deepseq=1.4.6.1'
            'haskell-directory=1.3.6.2'
            'haskell-exceptions=0.10.4'
            'haskell-filepath=1.4.2.2'
            'haskell-ghc-bignum=1.2'
            'haskell-ghc-boot=9.2.8'
            'haskell-ghc-boot-th=9.2.8'
            'haskell-ghc-compact=0.1.0.0'
            'haskell-ghc-heap=9.2.8'
            'haskell-ghci=9.2.8'
            'haskell-ghc-prim=0.8.0'
            'haskell-haskeline=0.8.2'
            'haskell-hpc=0.6.1.0'
            'haskell-integer-gmp=1.1'
            'haskell-libiserv=9.2.8'
            'haskell-mtl=2.2.2'
            'haskell-parsec=3.1.15.0'
            'haskell-pretty=1.1.3.6'
            'haskell-process=1.6.16.0'
            'haskell-stm=2.5.0.2'
            'haskell-template-haskell=2.18.0.0'
            'haskell-terminfo=0.4.1.5'
            'haskell-text=1.2.5.0'
            'haskell-time=1.11.1.1'
            'haskell-transformers=0.5.6.2'
            'haskell-unix=2.7.2.2'
            'haskell-xhtml=3000.2.2.1'
            'haskell-ghc-pkg=9.2.8')
  replaces=('haskell-array'
            'haskell-base'
            'haskell-binary'
            'haskell-bytestring'
            'haskell-cabal'
            'haskell-containers'
            'haskell-deepseq'
            'haskell-directory'
            'haskell-exceptions'
            'haskell-filepath'
            'haskell-ghc-bignum'
            'haskell-ghc-boot'
            'haskell-ghc-boot-th'
            'haskell-ghc-compact'
            'haskell-ghc-heap'
            'haskell-ghci'
            'haskell-ghc-prim'
            'haskell-haskeline'
            'haskell-hpc'
            'haskell-integer-gmp'
            'haskell-libiserv'
            'haskell-mtl'
            'haskell-parsec'
            'haskell-pretty'
            'haskell-process'
            'haskell-stm'
            'haskell-template-haskell'
            'haskell-terminfo'
            'haskell-text'
            'haskell-time'
            'haskell-transformers'
            'haskell-unix'
            'haskell-xhtml'
            'haskell-ghc-pkg')
  
  provides+=("haskell-ghci=$pkgver")
  conflicts+=('haskell-ghci')

  cd ghc-$pkgver

  DESTDIR="$pkgdir" make install

  # Remove static libs
  find "$pkgdir"/usr/lib \( -name "*.a" -o -name "*.o" -o -name "*.p_o" -o -name "*.p_hi" -o -name "*.hi" \) -delete

  (cd "$pkgdir"/usr/bin; rm !(${MPKG_PKGPREFIX}ghc-pkg*))
  (cd "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/bin; rm !(${MPKG_PKGPREFIX}ghc-pkg*))
  
  # ghc library, headers, and other exes are in the ghc package
  if [[ x"$MPKG_USRPATH" == x"/usr" ]]; then
      rm -r "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/ghc-$pkgver
      rm "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/*.h
      rm "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/*.txt
      # rm "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/lib/package.conf.d/*.copy 
      rm -r "$pkgdir"/usr/lib/${MPKG_PKGPREFIX}ghc-$pkgver/include
 fi

  # docs moved to ghc-static
  rm -r "$pkgdir"/usr/share/doc

  install -Dm644 "$srcdir"/ghc-register.hook "$pkgdir"$MPKG_USRPATH/share/libalpm/hooks/ghc-register.hook
  install -Dm644 "$srcdir"/ghc-unregister.hook "$pkgdir"$MPKG_USRPATH/share/libalpm/hooks/ghc-unregister.hook

  install -dm755 "$pkgdir"$MPKG_USRPATH/share/haskell/{register,unregister}

  install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}
