# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=mc
pkgver=4.8.33
pkgrel=1
pkgdesc="Midnight Commander is a text based filemanager/shell that emulates Norton Commander"
arch=('i686' 'x86_64')
url="https://www.midnight-commander.org/"
msys2_repository_url="https://github.com/MidnightCommander/mc"
license=('GPL')
depends=('glib2'
         'libssh2')
makedepends=('glib2-devel' 'libssh2-devel' 'ncurses-devel' 'gcc' 'autotools')
optdepends=('gawk: hp48+ extfs'
            #'aspell: spelling corrections'
            'cvs: CVS support'
            'perl: needed by several extfs scripts'
            #'unace: uace extfs'
            #'unarj: uarj extfs'
            'unrar: urar extfs'
            'zip: uzip extfs'
            'p7zip: support for 7zip archives')
options=('!emptydirs' '!makeflags')
backup=('etc/mc/edit.indent.rc'
        'etc/mc/filehighlight.ini'
        'etc/mc/mcedit.menu'
        'etc/mc/mc.ext'
        'etc/mc/mc.keymap'
        'etc/mc/mc.menu'
        'etc/mc/sfs.ini')
source=(${pkgname}-${pkgver}.tar.gz::"https://github.com/MidnightCommander/${pkgname}/archive/${pkgver}.tar.gz"
        4.7.5.2-ncursesw-term.h.patch
        0003-vfs-win32-absolute-paths.patch)
sha256sums=('092e440930fda43574739e45a8b41af384b974e6720184b6707d127b84082c51'
            '49e9a7d918088c8760b0090e99cd6257fa56efa3a4d3e6c9166270cda9c2e8e3'
            'fc33fcde6ead0406df9df2292804bd5f2d18fb05fc8b51a827f6abb1f6266b54')
noextract=(${pkgname}-${pkgver}.tar.gz)

prepare() {
  [[ -d ${srcdir}/${pkgname}-${pkgver} ]] && rm -rf ${srcdir}/${pkgname}-${pkgver}
  tar -xzf ${srcdir}/${pkgname}-${pkgver}.tar.gz -C ${srcdir} || true

  cd "${srcdir}/${pkgname}-${pkgver}"
  cp -f doc/{INSTALL,NEWS,README} .

  # work around version.sh git repo dependencies so mc gets a version number
  sed -i "s/CURR_MC_VERSION=.*/CURR_MC_VERSION=${pkgver}/" version.sh
  sed -i "s/SHOR_MC_VERSION=.*/SHOR_MC_VERSION=${pkgver}/" version.sh

  patch -Np2 -i "${srcdir}/4.7.5.2-ncursesw-term.h.patch"
  patch -Np0 -i "${srcdir}/0003-vfs-win32-absolute-paths.patch"

  ./autogen.sh
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  local CYGWIN_CHOST="${CHOST/-msys/-cygwin}"
  ./configure --build=${CYGWIN_CHOST} \
    --prefix=/usr \
    --sysconfdir=/etc \
    --without-libintl-prefix \
    --without-libiconv-prefix \
    --with-screen=ncursesw \
    --enable-charset \
    --libexecdir=/usr/lib

  make
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make DESTDIR="${pkgdir}" install

  # Fix FS#15177
  sed 's|op_has_zipinfo = 0|op_has_zipinfo = 1|' \
    -i "${pkgdir}/usr/lib/mc/extfs.d/uzip"

  sed 's#/usr/bin/env python#/usr/bin/python#' \
    -i "${pkgdir}/usr/lib/mc/extfs.d/s3+"
}
