# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Maintainer: Thorsten Töpper <atsutane-tu@freethoughts.de>
# Contributor: Andrea Scarpino <andrea@archlinux.org>
# Contributor: Dale Blount <dale@archlinux.org>
# Contributor: Tom Newsom <Jeepster@gmx.co.uk>
# Contributor: Michal Krenek <mikos@sg1.cz>

pkgname=john
_jumbover=1
_johnver=1.8.0
pkgver=${_johnver}.jumbo${_jumbover}
pkgrel=9
pkgdesc='John the Ripper password cracker'
url='http://www.openwall.com/john'
arch=('i686' 'x86_64')
license=('GPL2' 'custom')
depends=('openssl-1.0' 'gmp' 'libpcap' 'openmpi' 'gcc-libs' 'opencl-icd-loader')
optdepends=(
  'bash-completion: completion for bash'
  'perl: perl based john scripts'
  'ruby: ruby based john scripts'
  'python2: python based john scripts')
makedepends=('pkg-config' 'opencl-headers')
backup=('etc/john/john.conf')
install=john.install
source=(http://www.openwall.com/john/j/john-${_johnver}-jumbo-${_jumbover}.tar.xz
        params.h.patch
        gcc5.patch
        fix-32bit.patch
        fix-i686-openmp.patch)
sha512sums=('163cd71f634c2d1e9d0fa760984cc05001bfeef8300098d6b9cc8bf7e1719fec1d37142c39d7fd65ef37ee96c95681f01d7f0b1941058b7f9926442e2df5cd8e'
            '4e7c530bf75d0c56409018097a2fee4a991c8332b25f985b16d91ffb631079d46870fa0c54eb519c8d60c7425c62c616e24617e0ebbad4193ccef8d001c08893'
            '941a8fbe435d24287be158648d3083062806de639ce822645d949a171b2e662249bb13d9f5903017792cc979c7ff89615681adaed2afbf0d9c6ca5fe825e135a'
            '824497e6fa67b1ff17a4b87544dedf5361e5ba07617ffb9e7718f2e90152b0bc2f39cd80a1badd84de0ef1e3442783bda3df1332d990b722a29d04b8b7449590'
            '7eddd3c7360f459fba02b6c398b530baa7151f127384a1a8551a74658acbfb11a7d206a0482ab357496074be4ca20192c192962938e9bcdf1976cb4421d9af2c')

prepare() {
  cd ${pkgname}-${_johnver}-jumbo-${_jumbover}
  patch -p0 < "${srcdir}/params.h.patch"
  patch -p1 < "${srcdir}/gcc5.patch"
  patch -p1 < "${srcdir}/fix-32bit.patch"
  patch -Rp1 < "${srcdir}/fix-i686-openmp.patch"
  sed 's|env python|env python2|' -i run/*.py
  sed 's|/usr/bin/python|/usr/bin/python2|' -i run/*.py
  sed 's|"x$enable_native_tests" = xyes -a "x$PKG_CONFIG"|"x$PKG_CONFIG"|' -i src/configure
}

build() {
  cd ${pkgname}-${_johnver}-jumbo-${_jumbover}/src

  export PKG_CONFIG_PATH=/usr/lib/openssl-1.0/pkgconfig

  local JOHN_CFG_FULL_NAME="-DCFG_FULL_NAME='\"/etc/john/john.conf\"'"
  local JOHN_SYSTEMWIDE_FLAGS="-DJOHN_SYSTEMWIDE_EXEC='\"/usr/lib/john\"' -DJOHN_SYSTEMWIDE_HOME='\"/usr/share/john\"'"
  local JOHN_FLAGS="-DJOHN_SYSTEMWIDE=1 ${JOHN_SYSTEMWIDE_FLAGS} -DCPU_FALLBACK ${JOHN_CFG_FULL_NAME}"
  local CFLAGS="${CFLAGS} ${JOHN_FLAGS}"
  local CONFIGURE_FLAGS="--prefix=/usr --disable-native-tests --enable-openmp --enable-mpi"
  CONFIGURE_FLAGS+=" --enable-opencl --enable-pkg-config --enable-pcap"

  if [[ "${CARCH}" == "x86_64" ]]; then
    ./configure ${CONFIGURE_FLAGS} CFLAGS="${CFLAGS}"
    make clean; make
    mv ../run/john{,-non-avx}
    ./configure ${CONFIGURE_FLAGS} CFLAGS="${CFLAGS} -mavx"
    make clean; make
    mv ../run/john{,-non-xop}
    ./configure ${CONFIGURE_FLAGS} CFLAGS="${CFLAGS} -mxop"
    make clean; make
  elif [[ "${CARCH}" == "i686" ]]; then
    ./configure ${CONFIGURE_FLAGS} CFLAGS="${CFLAGS}"
    make clean; make
    mv ../run/john{,-non-mmx}
    ./configure ${CONFIGURE_FLAGS} CFLAGS="${CFLAGS} -mmmx"
    make clean; make
    mv ../run/john{,-non-sse}
    ./configure ${CONFIGURE_FLAGS} CFLAGS="${CFLAGS} -msse2"
    make clean; make
    mv ../run/john{,-non-avx}
    ./configure ${CONFIGURE_FLAGS} CFLAGS="${CFLAGS} -mavx"
    make clean; make
    mv ../run/john{,-non-xop}
    ./configure ${CONFIGURE_FLAGS} CFLAGS="${CFLAGS} -mxop"
    make clean; make
  else
    ./configure ${CONFIGURE_FLAGS} CFLAGS="${CFLAGS}"
    make clean; make
  fi
}

package() {
  cd ${pkgname}-${_johnver}-jumbo-${_jumbover}

  # config
  sed 's|$JOHN/john.local.conf|/etc/john/john.local.conf|g' -i run/john.conf
  install -Dm 644 run/john.conf -t "${pkgdir}/etc/john"
  install -Dm 644 run/*.conf -t "${pkgdir}/usr/share/john"
  rm "${pkgdir}/usr/share/john/"{john.conf,john.local.conf}

  # opencl
  install -Dm 644 run/kernels/* -t "${pkgdir}/usr/share/john/kernels"

  # docs
  install -Dm 644 doc/* -t "${pkgdir}/usr/share/doc/john"
  install -Dm 644 doc/LICENSE -t "${pkgdir}/usr/share/licenses/${pkgname}"

  # completion
  install -Dm 644 run/john.bash_completion "${pkgdir}/usr/share/bash-completion/completions/john"
  install -Dm 644 run/john.zsh_completion "${pkgdir}/usr/share/zsh/site-functions/_john"

  # binaries
  install -Dm 755 run/john -t "${pkgdir}/usr/bin"
  install -Dm 755 run/john-non-* -t "${pkgdir}/usr/lib/john"||true
  local john_bins=(calc_stat cprepair  genmkvpwd  luks2john    mkvcalcproba raw2dyna \
                   relbench  tgtsnarf  uaf2john   wpapcap2john vncpcap2john SIPdump)
  for bin in "${john_bins[@]}"; do
    install -Dm 755 run/${bin} -t "${pkgdir}/usr/bin"
  done

  # scripts
  install -Dm 755 run/*.py run/*.pl run/*.rb run/{mailer,benchmark-unify} -t "${pkgdir}/usr/lib/john"

  # data
  install -Dm 644 run/*.chr run/*.lst run/dictionary* run/stats -t "${pkgdir}/usr/share/john"

  # syminks
  cd "${pkgdir}/usr/bin"
  local john_links=(base64conv    dmg2john       gpg2john      hccap2john    \
                    keepass2john  keychain2john  keyring2john  keystore2john \
                    kwallet2john  pfx2john       putty2john    pwsafe2john   \
                    racf2john     rar2john       ssh2john      unique        \
                    unshadow      zip2john       unafs         undrop        \
                    truecrypt_volume2john)
  for link in "${john_links[@]}"; do
    ln -s john ${link}
  done
}

# vim: ts=2 sw=2 et:
