# 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

# The pkgbase is the controller of the templated actions
pkgbase=newlib-cygwin

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

# 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="@PKGDESC@"

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

# The license comes from the upstream package source.
license=('GPL3')

# The URL describing the package.
url=https://cygwin.com/

# Can we cross build this package, if not set _allow_cross=false.
_allow_cross=true
if test ! ${THOST} = ${CHOST} && test $_allow_cross = false; then
  error "The ${pkgbase} package cannot be cross compiled."
  exit 1
fi

# 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'
	'dll'
	'lib-headers'
	'lib-static'
	'lib-shared'
	#'lib-pkgconfig'
	#'aclocal'
	#'doc-info'
	'doc-man'
	'doc-html'
	#'doc-text'
	'doc-pdf'
	#'locale'
	'data'
	'license'
	#'extra'
)

# 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 check
}

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}")
  case ${T} in
    doc-info)
      touch ${pkgbase}-doc-info.install
      cat <<'      EOD'>${pkgbase}-doc-info.install.in
infodir=usr/share/info
filelist=@PKGBASE@.info.gz

post_install() {
  [ -x usr/bin/install-info ] || return 0
  for file in ${filelist}; do
    usr/bin/install-info $infodir/$file $infodir/dir 2> /dev/null
  done
}

post_upgrade() {
  post_install $1
}

pre_remove() {
  [ -x usr/bin/install-info ] || return 0
  for file in ${filelist}; do
    usr/bin/install-info --delete $infodir/$file $infodir/dir 2> /dev/null
  done
}
      EOD
      ;;
    *) ;;
  esac
done

pkgname+=("${pkgbase}-cygserver-exe" "${pkgbase}-cyglsa-dll")

# The extra binaries should be declared here.
pkgexealsoprovides=(
	'cygcheck'
	'cygpath'
	'cygwin-console-helper'
	'gencat'
	'getconf'
	'getfacl'
	'kill'
	'ldd'
	'ldh'
	'locale'
	'minidumper'
	'mkgroup'
	'mkpasswd'
	'mount'
	'passwd'
	'pldd'
	'ps'
	'regtool'
	'setfacl'
	'setmetamode'
	'ssp'
	'strace'
	'tzset'
	'umount'
)

# The extra DLL should be declared here.
pkgdllalsoprovides=(
	#@PKGDLLALSOPROVIDES@
)

# The extra static libraries should be declared here.
pkglibstaalsoprovides=(
	#@PKGLIBSTAALSOPROVIDES@
)

# The extra shared libraries should be declared here.
pkglibshralsoprovides=(
	#@PKGLIBSHRALSOPROVIDES@
)

# The source files both virtual and physical
source=(
	${pkgbase}::git://cygwin.com/git/newlib-cygwin.git
	'cygwin-dll.install'
	'cygserver-exe.install'
	'cyglsa-dll.install'
	'newlib-cygwin-lib-shared.install'
	'newlib-cygwin-data.install'
)

# 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=(
        'SKIP'
	'ef22e0950faa15608d8cefd68a5d0cf213fa0c49243a5ee428ab42f984f88b66'  #cygwin-dll.install
	'9e11e33a06015063c6f21931faed652735cd37d326de972ebf93bd7889baaaa0'  #cygserver-exe.install
	'ae1077390206e94f357e2970713cc22010a2f6cfd0b773391ebf8c014bd4eb8c'  #cyglsa-dll.install
	'6e74f4aea68ca92bb4ccf2f6d3fe100c619993e44395887bb8be9fe4a5ee9d30'  #newlib-cygwin-lib-shared.install
	'a785c39ff9064543494ea48229fbe0b1ce2dec93148d656ae38898335c1a882b'  #newlib-cygwin-data.install
)

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

# 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 pkgmakeinstall is used to identify the method to use for installing
pkgmakeinstall="${pkgmake} DESTDIR=${pkgdestdir} install"

# The pkgmakecheck is used to identify the method for testing the package.
pkgmakecheck="${pkgmake} check"

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

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

# The pkgconfigure is used to identify the name of the configure script.
# Not everyone uses autoconf tools.
pkgconfigure="${pkgsrcdir}/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'
)

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

# The unit testing process depends on packages listed here.
checkdepends=(
	#@CHECKDEPENDS@
)

# ####################### #
#      Helper macros      #
# ####################### #

##
 # Apply patch with a message giving the name of the patch file
 #
 # @param mixed
 #   - integer identifying the -p number (defaults to 1) or
 #   - array of the names of the patch files
 # @param optional but required if Param 1 is an integer
 #   - array of the names of the patch files
 # @return the exit status of the patch operation
 #
 # @example
 #  apply_patch_with_msg some_file.patch
 #  apply_patch_with_msg 3 some_file.patch some_file2.patch
 #
 # @note
 #  The patch file must reside in ${srcdir}.
 ##
apply_patch_with_msg() {
  local plvl
  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
}

##
 # Delete a file, link or directory if it exists
 #
 # @param An array of files, links and/or directories
 # @return the exit status of the last rm operation or unchanged exit status
 ##
del_file_exists() {
  local _f
  for _f in "$@"
  do
    if [ -l $_f ] || [ -f $_f ]; then
      rm -f $_f
    fi
    if [ -d $_f ]; then
      rm -rf $_f
    fi
  done
}

##
 # Does function exist?
 #
 # @param A string identifying the name of a function
 # @return boolean true or false
 #
 # @example
 #  fn_exists fn_exists && this || that
 #
 #  if fn_exhsts fn_exists; then
 #    this
 #  else
 #    that
 #  fi
 ##
fn_exists() {
  test x$(type -t $1) = xfunction
}

##
 # Does NEEDLE exists in the values of HAYSTACK
 #
 # @param needle - the item to search for
 # @param haystack - the array containing items
 # @return boolean true or false
 #
 # @example
 #  in_array 'needle' "${haystack[@]}" && this || that
 #
 #  if in_array 'needle' "${haystac[@]}"; then
 #    this
 #  else
 #    that
 #  fi
 ##
in_array() {
  local needle haystack
  printf -v needle -- ',,%q' "$1"
  shift
  printf -v haystack -- ',,%q' "$@"
  [[ "${haystack},," =~ "${needle},," ]]
}

# ####################### #
#         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.   #
# ####################### #

pre_prepare() {
  local _gittag
  _gittag="cygwin-${pkgver//./_}-release"
  pushd ${srcdir} > /dev/null
  pushd ${pkgbase} > /dev/null
  msg "preparing `git describe`"
  gitdesc=`git describe`
  if [ "${gitdesc}" != "${_gittag}" ]; then
    git checkout ${_gittag}
  fi
  popd > /dev/null
  ln -s ${pkgbase} ${pkgbase}-${pkgver}
  popd > /dev/null
}

pre_build() {
  export CFLAGS=''
  export CXXFLAGS=''
}

# ####################### #
#     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
  if [ -e ${pkgblddir} ]; then
    rm -rf ${pkgblddir}
  fi
  mkdir ${pkgblddir}
  cd ${pkgblddir}
  $pkgconfigure \
	${pkgconfigoptions[@]}
  ${pkgmake}
  ${pkgmakeinstall}
  fn_exists post_build && post_build || true
}

check() {
  fn_exists pre_check && pre_check || true
  if test "${THOST}" = "${CHOST}"; then
    msg2 "@TODO: The check is failing and we need to determine why"
    #cd ${pkgblddir}
    #${pkgmakecheck}
  else
    msg2 "NOTE: The make check function is disabled when cross compiling."
  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}
SQ="'"
DQ='"'
BQ='`'

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

in_array 'exe' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-exe() {
  pkgdesc='${pkgbase^^} executable files'
  provides=('${pkgbase}=${pkgver}' ${pkgexealsoprovides[@]})
  groups=('executables' '${pkgbase}-executables' 'cygwin-executables' '${pkgbase}' 'cygwin')
  in_array 'dll' '${pkgtypes[@]}' && depends=('${pkgbase}-dll=${pkgver}') || true
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p ${pdir}-exe/usr
  cp -a ${pkgdestdir}/usr/bin ${pdir}-exe/usr/
  rm -f ${pdir}-exe/usr/bin/*.dll
  rm -f ${pdir}-exe/usr/bin/cygserver-config
  rm -f ${pdir}-exe/usr/bin/cyglsa-config
}"

in_array 'dll' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-dll() {
  pkgdesc='${pkgbase^^} DLL shared libraries'
  provides=(${pkgdllalsoprovides[@]})
  groups=('dlls' '${pkgbase}-dll' 'cygwin-dll' '${pkgbase}' 'cygwin')
  install='cygwin-dll.install'
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p ${pdir}-dll/usr/bin
  cp -a ${pkgdestdir}/usr/bin/cygwin1.dll ${pdir}-dll/usr/bin/cygwin1.dll.new
}"

in_array 'lib-headers' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-lib-headers() {
  pkgdesc='${pkgbase^^} library headers'
  provides=('${pkgbase}-headers=${pkgver}')
  groups=('libraries' 'headers' '${pkgbase}-headers' 'cygwin-headers' '${pkgbase}-devel' 'cygwin-devel' '${pkgbase}' 'cygwin')
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p ${pdir}-lib-headers/usr/include
  mkdir -p ${pdir}-lib-headers/usr/${THOST}/include
  cp -a ${pkgdestdir}/usr/${THOST}/include ${pdir}-lib-headers/usr/${THOST}
  cd ${pdir}-lib-headers/usr/include
  local FILE
  for FILE in ../${THOST}/include/*; do
    rm -f ${BQ}basename \$FILE${BQ}
    ln -sf \$FILE ${BQ}basename \$FILE${BQ}
  done
}"

in_array 'aclocal' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-aclocal() {
  pkgdesc='${pkgbase^^} aclocal m4 macros'
  groups=('libraries' 'aclocal' '${pkgbase}-aclocal' 'cygwin-aclocal' '${pkgbase}-devel' 'cygwin-devel' '${pkgbase}' 'cygwin')
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true
  mkdir -p ${pdir}-aclocal/usr/share
  cp -a ${pkgdestdir}/usr/share/aclocal ${pdir}-aclocal/usr/share/
}"

in_array 'lib-pkgconfig' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-lib-pkgconfig() {
  pkgdesc='${pkgbase^^} pkgconfig files'
  provides=('${pkgbase}-pkgconfig=${pkgver}')
  groups=('libraries' 'pkgconfig' '${pkgbase}-pkgconfig' 'cygwin-pkgconfig' '${pkgbase}-devel' 'cygwin-devel' '${pkgbase}' 'cygwin')
  depends=('${pkgbase}-lib=${pkgver}')
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p ${pdir}-lib-pkgconfig/usr/lib
  cp -a ${pkgdestdir}/usr/lib/pkgconfig ${pdir}-lib-pkgconfig/usr/lib/
}"

in_array 'lib-static' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-lib-static() {
  pkgdesc='${pkgbase^^} headers and static libraries'
  provides=('${pkgbase}-lib=${pkgver}' ${pkglibstaalsoprovides[@]})
  groups=('libraries' 'libstatic' '${pkgbase}-libstatic' 'cygwin-libstatic' '${pkgbase}-devel' 'cygwin-devel' '${pkgbase}' 'cygwin')
  depends=('${pkgbase}-lib-headers=${pkgver}')
  in_array 'aclocal' '${pkgtypes[@]}' && depends+=('${pkgbase}-aclocal=${pkgver}') || true
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p ${pdir}-lib-static/usr/lib
  mkdir -p ${pdir}-lib-static/usr/${THOST}
  cp -a ${pkgdestdir}/usr/${THOST}/lib ${pdir}-lib-static/usr/${THOST}
  rm -f ${pdir}-lib-static/usr/${THOST}/lib{g,cygwin}.a
  cd ${pdir}-lib-static/usr/lib
  local FILE
  for FILE in ../${THOST}/lib/*; do
    rm -f ${BQ}basename \$FILE${BQ}
    ln -sf \$FILE ${BQ}basename \$FILE${BQ}
  done
}"

in_array 'lib-shared' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-lib-shared() {
  pkgdesc='${pkgbase^^} shared libraries'
  provides=('${pkgbase}-shared=${pkgver}' '${pkgbase}-lib=${pkgver}' ${pkglibshralsoprovides[@]})
  install=${pkgbase}-lib-shared.install
  groups=('libraries' 'libshared' '${pkgbase}-libshared' 'cygwin-libshared' '${pkgbase}-devel' 'cygwin-devel' '${pkgbase}' 'cygwin')
  depends=('${pkgbase}-lib-headers=${pkgver}')
  in_array 'aclocal' '${pkgtypes[@]}' && depends+=('${pkgbase}-aclocal=${pkgver}') || true
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p ${pdir}-lib-shared/usr/lib
  mkdir -p ${pdir}-lib-shared/usr/${THOST}/lib
  
  cp -a ${pkgdestdir}/usr/${THOST}/lib/libcygwin.a ${pdir}-lib-shared/usr/${THOST}/lib/libcygwin.dll.a
  ln -s libcygwin.dll.a ${pdir}-lib-shared/usr/${THOST}/lib/libg.dll.a
  cd ${pdir}-lib-shared/usr/lib
  local FILE
  for FILE in ../${THOST}/lib/*; do
    rm -f ${BQ}basename \$FILE${BQ}
    ln -sf \$FILE ${BQ}basename \$FILE${BQ}
  done
}"

in_array 'doc-info' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-doc-info() {
  pkgdesc='${pkgbase^^} info documentation'
  groups=('documentation' 'doc-info' 'cygwin-doc-info' '${pkgbase}-doc' 'cygwin-doc' '${pkgbase}' 'cygwin')
  depends=('info-exe')
  options=('!strip')
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  cat ${startdir}/${pkgbase}-doc-info.install.in | \
    sed -e 's/@PKGBASE@/${pkgbase}/' \
        -e 's/@PKGVER@/${pkgver}/' \
        -e 's/@PKGREL@/${pkgrel}/' \
        -e 's~@PKGBLDDIR@~`basename ${pkgblddir}`~' > \
	  ${startdir}/${pkgbase}-doc-info.install
  install=${pkgbase}-doc-info.install

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

in_array 'doc-man' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-doc-man() {
  pkgdesc='${pkgbase^^} man documentation'
  groups=('documentation' 'doc-man' 'cygwin-doc-man' '${pkgbase}-doc' 'cygwin-doc' '${pkgbase}' 'cygwin')
  depends=('man-exe')
  options=('!strip')
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

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

in_array 'doc-html' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-doc-html() {
  pkgdesc='${pkgbase^^} html documentation'
  groups=('documentation' 'doc-html' 'cygwin-doc-html' '${pkgbase}-doc' 'cygwin-doc' '${pkgbase}' 'cygwin')
  options=('!strip')
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p ${pdir}-doc-html/usr/share/doc/html/${pkgbase}
  cp -a ${pkgdestdir}/usr/share/doc/cygwin-api    ${pdir}-doc-html/usr/share/doc/html/${pkgbase}/
  cp -a ${pkgdestdir}/usr/share/doc/cygwin-ug-net ${pdir}-doc-html/usr/share/doc/html/${pkgbase}/
}"

in_array 'doc-text' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-doc-text() {
  pkgdesc='${pkgbase^^} text documentation'
  groups=('documentation' 'doc-text' 'cygwin-doc-text' '${pkgbase}-doc' 'cygwin-doc' '${pkgbase}' 'cygwin')
  options=('!strip')
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p ${pdir}-doc-text/usr/share/doc/text/${pkgbase}/cygserver
  cp -a ${pkgdestdir}/usr/share/doc/Cygwin/cygserver.README ${pdir}-doc-text/usr/share/doc/text/${pkgbase}/cygserver/
}"

in_array 'doc-pdf' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-doc-pdf() {
  pkgdesc='${pkgbase^^} pdf documentation'
  groups=('documentation' 'doc-pdf' 'cygwin-doc-pdf' '${pkgbase}-doc' 'cygwin-doc' '${pkgbase}' 'cygwin')
  options=('!strip')
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p ${pdir}-doc-pdf/usr/share/doc/pdf/${pkgbase}
  cp -f ${pkgdestdir}/usr/share/doc/*.pdf ${pdir}-doc-pdf/usr/share/doc/pdf/${pkgbase}/

}"

in_array 'locale' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-locale() {
  pkgdesc='${pkgbase^^} locale message database files'
  groups=('locale' '${pkgbase}-locale' 'cygwin-locale' '${pkgbase}' 'cygwin')
  options=('!strip')
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

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

in_array 'data' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-data() {
  pkgdesc='${pkgbase^^} data files'
  groups=('data' '${pkgbase}-data' 'cygwin-data' '${pkgbase}' 'cygwin')
  install='${pkgbase}-data.install'
  options=('!strip')
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p ${pdir}-data/usr/share/${pkgbase}
  cp -a ${pkgdestdir}/usr/share/cygwin ${pdir}-data/usr/share/${pkgbase}/
  ln -s /usr/share/${pkgbase}/cygwin ${pdir}-data/usr/share/cygwin
}"

in_array 'license' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-license() {
  pkgdesc='${pkgbase^^} custom license file'
  groups=('license' '${pkgbase}-license' 'cygwin-license' '${pkgbase}' 'cygwin')
  options=('!strip')

  mkdir -p ${pdir}-license/usr/share/licenses/${pkgbase}
  cp -f ${pkgdestdir}/usr/share/doc/Cygwin/COPYING ${pdir}-license/usr/share/licenses/${pkgbase}/
  cp -f ${pkgdestdir}/usr/share/doc/Cygwin/CYGWIN_LICENSE ${pdir}-license/usr/share/licenses/${pkgbase}/
}"

in_array 'extra' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-extra() {
  pkgdesc='${pkgbase^^} custom license file'
  groups=('extra' '${pkgbase}-extra' 'cygwin-extra' '${pkgbase}' 'cygwin')
  options=('!strip')
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  # create what you need in ${pdir}-extra/
}"

in_array 'testing' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-testing() {
  pkgdesc='${pkgbase^^} preconfigured and built files to be used for native host testing'
  groups=('testing' '${pkgbase}-testing' 'cygwin-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
  mkdir -p ${pdir}-testing/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}
  cp -a ${pkgsrcdir} ${pdir}-testing/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}/
  if test ! '$pkgblddir' = '$pkgsrcdir'; then
    cp -a ${pkgblddir} ${pdir}-testing/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}/
  fi
  find ${pdir}-testing/usr/src/testing -type d -name autom4te.cache -prune -exec rm -rf {} \;
  for I in ${BQ}grep -rl ${srcdir}/ ${pdir}-testing/usr/src/testing/${BQ}
  do
    sed -e 's~${srcdir}/~/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}/~g' \$I > \${I}.new
    mv -f \${I}.new \${I}
  done
}"

eval "package_${pkgbase}-cygserver-exe() {
  pkgdesc='${pkgbase^^} cygserver package'
  groups=('administrator' 'server' '${pkgbase}' 'cygwin')
  install='cygserver-exe.install'
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p \${pkgdir}/usr/sbin
  mkdir -p \${pkgdir}/usr/share/doc/text/${pkgbase}/cygserver
  cp ${pkgdestdir}/usr/bin/cygserver-config \${pkgdir}/usr/sbin/
  cp ${pkgdestdir}/usr/sbin/cygserver.exe \${pkgdir}/usr/sbin/
  cp ${pkgdestdir}/usr/share/doc/Cygwin/cygserver.README \${pkgdir}/usr/share/doc/text/${pkgbase}/cygserver/
  cp -a ${pkgdestdir}/etc \${pkgdir}/
}"

eval "package_${pkgbase}-cyglsa-dll() {
  pkgdesc='${pkgbase^^} cyglsa64 package'
  groups=('administrator' 'server' '${pkgbase}' 'cygwin')
  install='cyglsa-dll.install'
  depends=()
  in_array 'license' '${pkgtypes[@]}' && depends+=('${pkgbase}-license=${pkgver}') || true

  mkdir -p \${pkgdir}/usr/bin
  cp -a ${pkgdestdir}/usr/bin/cyglsa* \${pkgdir}/usr/bin/
}"

# Need to reset the exit status due to the use of in_array.
true
