# Maintainer : Márcio Silva <coadde@lavabit.com>
# Maintainer : André Silva <emulatorman@lavabit.com>

# We're getting this from Debian Sid
_debname=iceape
_debver=2.7.12
_debrel=1
_debrepo=http://ftp.debian.org/debian/pool/main/
debfile() { echo $@|sed -r 's@(.).*@\1/&/&@'; }

_pkgname=${_debname}
pkgname=${_debname}-libre
pkgver=${_debver}.${_debrel}
pkgrel=8
pkgdesc="A libre version of Debian Iceape, the internet suite based on SeaMonkey."
arch=('i686' 'x86_64' 'mips64el')
license=('MPL' 'GPL' 'LGPL')
depends=('alsa-lib' 'dbus-glib' 'desktop-file-utils' 'gtk2' 'hunspell' 'libevent' 'libnotify' 'libvpx' 'libxt' 'mime-types' 'mozilla-common' 'mozilla-searchplugins' 'nss' 'sqlite' 'startup-notification')
makedepends=('unzip-libre' 'zip' 'pkg-config' 'python2' 'python2-ply' 'librsvg' 'wireless_tools' 'yasm' 'mesa' 'autoconf2.13' 'quilt')
replaces=('seamonkey')
conflicts=('seamonkey')
provides=('seamonkey')
install=$_pkgname.install
url="http://packages.debian.org/sid/${_pkgname}"
source=("${_debrepo}/`debfile ${_debname}`_${_debver}.orig.tar.bz2"
        "${_debrepo}/`debfile ${_debname}`_${_debver}-${_debrel}.debian.tar.gz"
        mozconfig
        ${_pkgname}.desktop
        ${_pkgname}-2.0-lang.patch
        clrf.patch
        libre.patch)
md5sums=('1ee82aa6ec73c13e2099b4cb6f16a908'
         '1fb9c4affad5f030c4e3544aadeaeea2'
         '70575e76b339fa7e17274a8ed84429fc'
         '7266333e31731af8bb50c2eca8d0bd26'
         '25b6fe16ac24cd5c852213e5c1adb272'
         'c395d443a8c4c16880c7322bcf174743'
         '14e2f08fb1b98ff287e74de54baf44fe')

prepare() {
  export DEBIAN_BUILD="comm-release"
  export DEBIAN_BRANDING_DIR="debian/branding" 

  export ICEAPE_ICONS_BRANDING="suite/branding/nightly"

  export QUILT_PATCHES=debian/patches
  export QUILT_REFRESH_ARGS='-p ab --no-timestamps --no-index'
  export QUILT_DIFF_ARGS='--no-timestamps'

  mv comm-esr10 "${DEBIAN_BUILD}"
  mv debian "${srcdir}/${DEBIAN_BUILD}"
  cd "${srcdir}/${DEBIAN_BUILD}"
  quilt push -av

  patch -Np1 -i "$srcdir/$_pkgname-2.0-lang.patch"
  patch -Np1 -i "$srcdir/clrf.patch"
  patch -Np1 -i "$srcdir/libre.patch"

  # set up a simple non-animated throbber from the icon
  install -m644 debian/extras/Throbber-small.gif suite/themes/classic/communicator/brand/throbber-anim.png
  install -m644 debian/extras/Throbber-small.png suite/themes/classic/communicator/brand/throbber-single.png
  install -m644 debian/extras/Throbber-small.gif suite/themes/classic/communicator/brand/throbber16-anim.png
  install -m644 debian/extras/Throbber-small.png suite/themes/classic/communicator/brand/throbber16-single.png
  install -m644 debian/extras/Throbber-small.gif suite/themes/modern/communicator/brand/throbber-anim.png
  install -m644 debian/extras/Throbber-small.png suite/themes/modern/communicator/brand/throbber-single.png
  install -m644 debian/extras/Throbber-small.gif suite/themes/modern/communicator/brand/throbber16-anim.png
  install -m644 debian/extras/Throbber-small.png suite/themes/modern/communicator/brand/throbber16-single.png

  install -m644 debian/extras/preview.png suite/themes/classic/preview.png

  # Converting svg file to png and replacing seamonkey by iceape icons
  rsvg-convert -w 300 -h 280 -o ${ICEAPE_ICONS_BRANDING}/content/about.png ${DEBIAN_BRANDING_DIR}/iceape_logo_plain.svg
  rsvg-convert -w 64 -h 64 -o ${ICEAPE_ICONS_BRANDING}/content/icon64.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 128 -h 128 -o ${ICEAPE_ICONS_BRANDING}/icons/gtk/iceape.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 32 -h 32 -o ${ICEAPE_ICONS_BRANDING}/icons/gtk/default.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 32 -h 32 -o ${ICEAPE_ICONS_BRANDING}/icons/gtk/main-window.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 16 -h 16 -o ${ICEAPE_ICONS_BRANDING}/icons/gtk/default16.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 16 -h 16 -o ${ICEAPE_ICONS_BRANDING}/icons/gtk/main-window16.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 48 -h 48 -o ${ICEAPE_ICONS_BRANDING}/icons/gtk/default48.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 48 -h 48 -o ${ICEAPE_ICONS_BRANDING}/icons/gtk/main-window48.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  
  # Creating app-icons Folder for iceape*.png icons

  install -m755 -d "${DEBIAN_BRANDING_DIR}/app-icons"

  # Icons for /usr/share/icons/hicolor
  rsvg-convert -w 16 -h 16 -o ${DEBIAN_BRANDING_DIR}/app-icons/iceape16.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 32 -h 32 -o ${DEBIAN_BRANDING_DIR}/app-icons/iceape32.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 48 -h 48 -o ${DEBIAN_BRANDING_DIR}/app-icons/iceape48.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 64 -h 64 -o ${DEBIAN_BRANDING_DIR}/app-icons/iceape64.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg
  rsvg-convert -w 128 -h 128 -o ${DEBIAN_BRANDING_DIR}/app-icons/iceape128.png ${DEBIAN_BRANDING_DIR}/iceape_icon_plain.svg

  cp "${srcdir}/mozconfig" .mozconfig

  # configure script misdetects the preprocessor without an optimization level
  # https://bugs.archlinux.org/task/34644
  sed -i '/ac_cpp=/s/$CPPFLAGS/& -O2/' configure
}

build() {
  cd "${srcdir}/${DEBIAN_BUILD}"

  export LDFLAGS="${LDFLAGS} -Wl,-rpath,/usr/lib/${_pkgname}"
  export MOZ_MAKE_FLAGS="$MAKEFLAGS"
  unset MAKEFLAGS

  # Don't exit with error when some libs are missing which we have in
  # system.
  sed '/^MOZ_PKG_FATAL_WARNINGS/s@= 1@= 0@' \
      -i suite/installer/Makefile.in

  make -j1 -f client.mk build
}

package() {
  cd "${srcdir}/${DEBIAN_BUILD}"

  make -j1 -f client.mk DESTDIR="${pkgdir}" install

  rm -rf "$pkgdir"/usr/lib/$_pkgname/{dictionaries,hyphenation,searchplugins}
  ln -sf /usr/share/hunspell "$pkgdir/usr/lib/$_pkgname/dictionaries"
  ln -sf /usr/share/hyphen "$pkgdir/usr/lib/$_pkgname/hyphenation"
  ln -sf /usr/lib/mozilla/searchplugins "$pkgdir/usr/lib/$_pkgname/searchplugins"

  install -m755 -d "$pkgdir/usr/share/applications"
  install -m755 -d "$pkgdir/usr/share/pixmaps"
  install -m644 ${ICEAPE_ICONS_BRANDING}/icons/gtk/iceape.png \
                "$pkgdir/usr/share/pixmaps/"

  for i in 16x16 32x32 48x48 64x64 128x128; do
      install -Dm644 "${DEBIAN_BRANDING_DIR}/app-icons/${_pkgname}${i/x*/}.png" "${pkgdir}/usr/share/icons/hicolor/$i/apps/${_pkgname}.png"
  done
  install -Dm644 "${DEBIAN_BRANDING_DIR}/${_pkgname}_icon_plain.svg" "${pkgdir}/usr/share/icons/hicolor/scalable/apps/${_pkgname}.svg"

  install -m644 "$srcdir/$_pkgname.desktop" "$pkgdir/usr/share/applications/"
}