#Packager: George Vlahavas <vlahavas~at~gmail~dot~com>
#Previous packager: Ruari Odegaard <ruario AT opera DOT com>

# This SLKBUILD has some quirks that are needed because it is a 
# binary repackage, rather than a typical source-based build. It should
# *not* be used as a model for other SLKBUILDs.

pkgname=opera
pkgver=12.14
_buildver=1738
pkgrel=1gv
options=("nostrip")

# We need to make sure that $ARCH/$arch are defined now because they
# are needed for $_oarch (and hence $source) to work correctly.
# Otherwise they would remain undefined until build time and hence the
# we could not locate the correct "source" package.

# Base $ARCH on uname or what the packager has predefined outside of 
# this script. If set to i386, i486 or i586 it will be changed to i686
# as Opera will not run on older processors.
ARCH=$(echo ${ARCH:-$(uname -m)} | sed "s/i[345]86/i686/")

# Have $arch inherit its value directly from $ARCH or what the packager
# has predefined outside of this script. Again correcting for i386, 
# i486 or i586 being predefined.
arch=$(echo ${arch:-$ARCH} | sed "s/i[345]86/i686/")

# Opera labels its 32-bit packages i386, even though they are i686. The
# following ensures the correct package is downloaded, even when 
# $ARCH/$arch have been set to i686.
_oarch=$(echo $arch | sed "s/i686/i386/")
source=("http://ftp.opera.com/pub/opera/linux/${pkgver/./}/opera-${pkgver}-${_buildver}.${_oarch}.linux.tar.xz")
slackdesc=\
(
"${pkgname} (Fast and secure web browser and Internet suite)"
"Opera is a small, fast, customizable, powerful and user-friendly web"
"browser, as well as an Internet suite, including an email client, an"
"IRC client, web developer tools (Opera Dragonfly), and a personal web"
"server (Opera Unite)."
" "
"http://www.opera.com/browser/"
)

build() {
# Install Opera into $PKG
opera-${pkgver}-${_buildver}.${_oarch}.linux/install --prefix /usr --repackage "${PKG}/usr" || exit 1

# Move man and doc to the standard Slackware locations
mv "${PKG}"/usr/share/{man,doc} "${PKG}/usr/"
mv "${PKG}/usr/doc/${pkgname}" "${PKG}/usr/doc/${pkgname}-${pkgver}"

# If the LICENSE is in the doc directory, move it to Opera's share
# directory so that it can be found by Opera on first startup
if [ -f "${PKG}/usr/doc/${pkgname}-${pkgver}/LICENSE" ]; then
  find "${PKG}"/usr/share/${pkgname}/{defaults,locale/en*} -name license.txt -type l -delete
  mv "${PKG}/usr/doc/${pkgname}-${pkgver}/LICENSE" "${PKG}/usr/share/${pkgname}/defaults/license.txt"
  ( cd "${PKG}/usr/doc/${pkgname}-${pkgver}/" ; ln -s ../../share/${pkgname}/defaults/license.txt LICENSE )
fi

# If a $LIBDIRSUFFIX is set to anything other than a zero length string
# then we need to adjust the Opera library directory accordingly.
if [ -n "$LIBDIRSUFFIX" ]; then
  mv "${PKG}/usr/lib" "${PKG}/usr/lib${LIBDIRSUFFIX}"
  sed -i "s,/lib/,/lib${LIBDIRSUFFIX}/," "${PKG}/usr/bin/${pkgname}"
fi
}

# The rest of this script is provided to ensure immediate updates to
# various caches. If you prefer you can remove all of this.
doinst() {
# Update application menu cache, as Opera added new *.desktop files
if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database -q usr/share/applications
fi

# Update MIME associations cache, as Opera handles a number of MIME types
if [ -x /usr/bin/update-mime-database ]; then
  /usr/bin/update-mime-database usr/share/mime >/dev/null
fi
}