# Maintainer: cygSimple <cygsimple@users.sourceforge.net>

# Cross build requires specifying --host, --build and sometimes --target.
# The --build is always $CHOST as specified in /etc/makepkg.conf
# The --host will be $CHOST if --target is also specified otherwise $THOST
# The --target is specified if the binary runs as a cross tool
if [ -z "${THOST}" ]; then
  THOST=${CHOST}
fi

if test ! ${THOST} = ${CHOST}; then
  warning "Cross compilation does not currently work correctly."
fi

# The pkgbase is the controller of the templated actions
pkgbase=icu

# The `eval'uated parts below require srcdir and pkgdir be set; but PKGBUILD
# is sourced before these are set so we must do it explicitly here.
srcdir=`pwd`/src
pkgdir=`pwd`/pkg

# Identify the package type to split the packages into.
pkgtypes=(
	'exe'
	'lib-static'
	#'doc-info'
	'doc-man'
	#'doc-html'
	#'doc-text'
	#'locale'
	'data'
	'license'
)

# We always provide an all meta package so that PACKAGE-all delivers
# every component of PACKAGE.
pkgtypes+=('all')

# If we are cross building then we cannot `make check`
# instead we create a -testing.pkg.tar with a -testing.install
# to execute the testing on the target host.
# pacman -U PACKAGE-testing.pkg.tar.xz will execute the `make check`
# after installing the /usr/src/testing/PACKAGE-PKGVER-PKGREL.
if test ! $CHOST = $THOST; then
  pkgtypes+=('testing')
  touch ${pkgbase}-testing.install
  cat <<'  EOD'>${pkgbase}-testing.install.in
post_install() {
  cd /usr/src/testing/@PKGBASE@-@PKGVER@-@PKGREL@/@PKGBLDDIR@
  make tests
}

post_upgrade() {
  post_install
}

post_remove() {
  rm -rf /usr/src/testing/@PKGBASE@-@PKGVER@-@PKGREL@
}
  EOD
fi

# Then pkgname becomes an array of the concantenation of the pkgbase and the
# individual pkgtypes.
pkgname=()
for T in "${pkgtypes[@]}"; do
  pkgname+=("${pkgbase}-${T}")
done

# The pkgver comes from the upstream source distribution.
pkgver=62.1

# The pkgrel is the count of number of issues of the same pkgver.
pkgrel=1

# The pkgdesc here comes from an upstream description of the package source.
pkgdesc="International Components for unicode library"

# cygSimple is only providing x86_64
arch=('x86_64')

# The license comes from the upstream package source.
license=('custom:icu')

# The URL describing the package.
url='http://www.icu-project.org/'

# The source files both virtual and physical
source=(
	http://download.icu-project.org/files/${pkgbase}4c/${pkgver}/${pkgbase}4c-${pkgver//./_}-src.tgz
        toolutil-Makefile-ln-force.patch
)

# The extra binaries should be declared here.
pkgexealsoprovides=(
	#@PKGEXEALSOPROVIDES@
)

# The extra libraries should be declared here.
pkglibalsoprovides=(
	#@PKGLIBALSOPROVIDES@
)

# The pkgconfigure is used to identify the name of the configure script.
# Not everyone uses autoconf tools.
pkgconfigure="${srcdir}/${pkgbase}-${pkgver}/source/configure"

# The pkgconfigoptions is used to add configure options that aren't standard.
# When specifying --target --host must be $CHOST.
pkgconfigoptions=(
	'--prefix=/usr'
	"--host=${THOST}"
	#"--host=${CHOST}"
	"--build=${CHOST}"
	#"--target=${THOST}"
	'--sysconfdir=/etc'
	'--sharedstatedir=/var'
	'--localstatedir=/var'
	'--disable-shared'
	'--enable-static'
)

if test ! ${THOST} = ${CHOST}; then
  pkgconfigoptions+=("--with-cross-build=${srcdir}/native")
fi

# The pkgmake is used to identify the make program to use
# Not everyone likes GNU make
pkgmake=/usr/bin/make

# The pkgdestdir identifies deposition directory for creating packages.
pkgdestdir="${srcdir}/dest"

# The pkgsrcdir identifies the source directory of the package.
pkgsrcdir="${srcdir}/${pkgbase}-${pkgver}"

# The pkgblddir identifies the build directory for the source.
pkgblddir="${srcdir}/cygwin"

# The pkgmakeinstall is used to identify the method to use for installing
pkgmakeinstall="/usr/bin/make DESTDIR=${pkgdestdir} install"

# The pkgmakecheck is used to identify the method for testing the package.
pkgmakecheck='make check'

# The sha256sums array is in the same order as the source array, use 'SKIP'
# if the source file doesn't or shouldn't have one.
sha256sums=(
	'3dd9868d666350dda66a6e305eecde9d479fb70b30d5b55d78a1deffb97d5aa3'
	'1df08d857ba3979cacb8417679b723a3b42029c2b09bdbbd0d743b4656a41b62'
)

# The validpgpkeys for the upstream source packages if there are any.
# You need these keys in your keystore.
validpgpkeys=(
	#'@VALIDPGPKEYS@'
)

# The make process depends on packages listed here.
makedepends=(
	#@MAKEDEPENDS@
)
checkdepends=(
	#@CHECKDEPENDS@
)

# ####################### #
#      Helper macros      #
# ####################### #
apply_patch_with_msg() {
  if [[ $1 == +([0-9]) ]]; then
    plvl=$1
    shift
  else
    plvl=1
  fi
  for F in "$@"
  do
    msg2 "Applying patch file $F"
    patch -Nbp${plvl} -i "${srcdir}/${F}"
  done
}

del_file_exists() {
  for _f in "$@"
  do
    if [ -l $_f ] || [ -f $_f ]; then
      rm -f $_f
    fi
    if [ -d $_f ]; then
      rm -rf $_f
    fi
  done
}

fn_exists() {
  test x$(type -t $1) = xfunction
}

# ####################### #
#         Hooks           #
# ####################### #
# prefix pre_ or post_ to #
#  any of the standard    #
#  functions (prepare,    #
#  build, check) to add   #
#  operations for each    #
#  step.                  #
# Create auto_conf() if   #
#  some configuration     #
#  script needs to exe-   #
#  cute to create the     #
#  configure scripts.     #
#  E.G. autoreconf -vfi   #
#                         #
# @see gettext/PKGBUILD   #
#  for examples of use.   #
# ####################### #

# Change or remove auto_conf as necessary
# autoreconf is the more common option so included for convenience
# @see gettext/PKGBUILD:auto_conf() for another example
auto_conf() {
  cd source
  autoreconf -vfi
}

pre_prepare() {
  if [ -e ${srcdir}/${pkgbase}-${pkgver} ] && [ -e ${srcdir}/${pkgbase} ]; then
    rm -rf ${srcdir}/${pkgbase}-${pkgver}
  fi
  if [ -e ${srcdir}/${pkgbase} ]; then
    mv ${srcdir}/${pkgbase} ${srcdir}/${pkgbase}-${pkgver}
  fi
}

pre_build() {
  # YIKES - ICU is confused about what --with-cross-build means; well the term confused me anyway.
  # The --with-cross-build is for the native build of icu and in particular it needs icudefs.mk
  # @TODO See if supplying native/icudefs.mk is all that is required.
  # @TODO Replace native/bin/*.\, native/tools/gettest/gettest, native/tools/icuinfo/icuinfo with
  # the versions in source/bin and source/tools respectively in the testing package.
  if test ! ${THOST} = ${CHOST}; then
    mkdir ${srcdir}/native
    pushd ${srcdir}/native > /dev/null
    ${srcdir}/${pkgbase}-${pkgver}/source/runConfigureICU `uname -s` --enable-static --disable-shared
    make
    popd > /dev/null
  fi
}

# ####################### #
#     Required macros     #
# ####################### #
prepare() {
  fn_exists pre_prepare && pre_prepare || true
  # Patches are always listed in the source array.  This scheme will force the
  # patch to be at level 1 deep and the file name to end in .patch
  cd ${pkgbase}-${pkgver}
  for F in ${source[@]}; do
    if test "${F##*.}" = 'patch'; then
      apply_patch_with_msg $F
    fi
  done
  fn_exists auto_conf && auto_conf || true
  fn_exists post_prepare && post_prepare || true
}

build() {
  fn_exists pre_build && pre_build || true
  mkdir ${srcdir}/cygwin
  cd ${srcdir}/cygwin
  $pkgconfigure \
	${pkgconfigoptions[@]}
  ${pkgmake}
  ${pkgmakeinstall}
  fn_exists post_build && post_build || true
}

check() {
  fn_exists pre_check && pre_check || true
  if test "${THOST}" = "${CHOST}"; then
    cd ${pkgbase}-${pkgver}
    ${pkgmakecheck}
  else
    msg2 "NOTE: The make check function is disabled when cross compiling."
    msg2 "A ${pkgbase}-testing package has been created for you to use to"
    msg2 "test on the native system by using \`pacman -S ${pkgbase}-testing\`"
    msg2 "on your Windows clinet."
  fi
  fn_exists post_check && post_check || true
}

# ####################### #
#      Package macros     #
# ####################### #

# The pdir variable is used because of the timing of the eval.
pdir=${pkgdir}/${pkgbase}

eval "package_${pkgbase}-all() {
  pkgdesc='${pkgbase^^} all package'
  depends=()
  for I in ${pkgtypes[@]}; do
    case \$I in
	    testing|all) ;;
	    *) depends+=(${pkgbase}-\${I}) ;;
    esac
  done
}"

eval "package_${pkgbase}-exe() {
  pkgdesc='${pkgbase^^} executable files'
  provides=('${pkgbase}=${pkgver}' ${pkgexealsoprovides[@]})
  groups=('executables')
  depends=('icu-lib=${pkgver}' 'icu-data=${pkgver}' 'icu-license')

  mkdir -p ${pdir}-exe/usr
  cp -a ${pkgdestdir}/usr/bin ${pdir}-exe/usr/
  if [ -e ${pkgdestdir}/usr/sbin ]; then
    cp -a ${pkgdestdir}/usr/sbin ${pdir}-exe/usr/
  fi
}"

eval "package_${pkgbase}-lib-static() {
  pkgdesc='${pkgbase^^} headers and static libraries'
  provides=('${pkgbase}-lib=${pkgver}' '${pkgbase}-devel=${pkgver}' ${pkglibalsoprovides[@]})
  groups=('libraries' 'libstatic')
  depends=('icu-data=${pkgver}' 'icu-exe=${pkgver}' 'icu-license')

  mkdir -p ${pdir}-lib-static/usr
  cp -a ${pkgdestdir}/usr/{include,lib} ${pdir}-lib-static/usr/
}"

eval "package_${pkgbase}-doc-info() {
  pkgdesc='${pkgbase^^} info documentation'
  groups=('documentation' 'doc-info' '${pkgbase}-doc')
  depends=('info-exe')

  install=${pkgbase}-doc-info.install

  mkdir -p ${pdir}-doc-info/usr/share
  cp -a ${pkgdestdir}/usr/share/info ${pdir}-doc-info/usr/share/
}"

eval "package_${pkgbase}-doc-man() {
  pkgdesc='${pkgbase^^} man documentation'
  groups=('documentation' 'doc-man' '${pkgbase}-doc')
  depends=('man-exe' 'icu-license')

  mkdir -p ${pdir}-doc-man/usr/share
  cp -a ${pkgdestdir}/usr/share/man ${pdir}-doc-man/usr/share/
}"

eval "package_${pkgbase}-doc-html() {
  pkgdesc='${pkgbase^^} html documentation'
  groups=('documentation' 'doc-html' '${pkgbase}-doc')

  mkdir -p ${pdir}-doc-html/usr/share/doc/html
  cp -a ${pkgdestdir}/usr/share/doc/${pkgbase}/*.html ${pdir}-doc-html/usr/share/doc/hmtl/${pkgbase}/
}"

eval "package_${pkgbase}-doc-text() {
  pkgdesc='${pkgbase^^} text documentation'
  groups=('documentation' 'doc-text' '${pkgbase}-doc')

  mkdir -p ${pdir}-doc-text/usr/share/doc/text
  cp -a ${pkgdestdir}/usr/share/doc/${pkgbase}/* ${pdir}-doc-text/usr/share/doc/text/${pkgbase}/
  rm -f ${pdir}-doc-text/usr/share/doc/text/${pkgbase}/*.html
}"

eval "package_${pkgbase}-locale() {
  pkgdesc='${pkgbase^^} locale message database files'
  groups=('locale')

  mkdir -p ${pdir}-locale/usr/share
  cp -a ${pkgdestdir}/usr/share/locale ${pdir}-locale/usr/share/
}"

eval "package_${pkgbase}-data() {
  pkgdesc='${pkgbase^^} data files'
  groups=('data')
  depends=('icu-exe=${pkgver}' 'icu-lib=${pkgver}' 'icu-license')

  mkdir -p ${pdir}-data/usr/share
  cp -a ${pkgdestdir}/usr/share/${pkgbase} ${pdir}-data/usr/share/
  rm -f ${pdir}-data/usr/share/${pkgbase}/${pkgver}/LICENSE
}"

eval "package_${pkgbase}-license() {
  pkgdesc='${pkgbase^^} license'
  groups=('license')

  mkdir -p ${pdir}-license/usr/share/licenses/icu/${pkgver}
  cp -a ${pkgdestdir}/usr/share/icu/${pkgver}/LICENSE ${pdir}-license/usr/share/licenses/icu/${pkgver}/
}"

eval "package_${pkgbase}-testing() {
  pkgdesc='${pkgbase^^} preconfigured and built files to be used for native host testing'
  groups=('testing')
  provides=('${pkgbase}-testing=${pkgver}')
  options=('staticlibs' '!strip' 'libtool')
  install='${pkgbase}-testing.install'

  cat ${startdir}/${pkgbase}-testing.install.in | \
    sed -e 's/@PKGBASE@/${pkgbase}/' \
        -e 's/@PKGVER@/${pkgver}/' \
        -e 's/@PKGREL@/${pkgrel}/' \
        -e 's~@PKGBLDDIR@~`basename ${pkgblddir}`~' > \
	  ${startdir}/${pkgbase}-testing.install
  D=${pdir}-testing/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}
  mkdir -p \${D}
  cp -a ${pkgsrcdir} \${D}
  if [ -e ${srcdir}/native ]; then
    cp -a ${srcdir}/cygwin \${D}/native
    chmod ugo+x \${D}/native/bin/*
  fi
  if test ! '$pkgblddir' = '$pkgsrcdir'; then
    cp -a ${pkgblddir} \${D}/
  fi
  find ${pdir}-testing/usr/src/testing -type d -name autom4te.cache -prune -exec rm -rf {} \; 
  for I in \`grep -rl ${srcdir}/ ${pdir}-testing/usr/src/testing/\`
  do
    sed -e 's~${srcdir}/~/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}/~g' \$I > \${I}.new
    mv -f \${I}.new \${I}
  done
}"
