#!/bin/sh
#-- 2mandvd for Slackware --
# Build script by Phantom X <megaphantomx at bol.com.br>
# Suggested usage: $ 2mandvd.SlackBuild 2>&1 | tee build.log
#--
# Copyright 2008, 2009, 2010, 2011 Phantom X, Goiania, Brazil.
# Copyright 2006 Martijn Dekker, Groningen, Netherlands.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# http://kde-apps.org/content/show.php?content=99450

PACKAGER_ID=${PACKAGER_ID:-$USER}
PACKAGER=${PACKAGER:-$USER@$HOSTNAME}

# Set YES for native build with gcc >= 4.2
SB_NATIVE=${SB_NATIVE:-YES}

# Set to YES to replicate slackbuild and patches
SB_REP=${SB_REP:-YES}

CWD=$(pwd)
TMP=${TMP:-/tmp}
if [ ! -d ${TMP} ]; then
  mkdir -p ${TMP}
fi

PNAME=2ManDVD
NAME=2mandvd
PKG=${PKG:-${TMP}/package-${NAME}}

ADDRB=${ADDRB:-"transcode >= 1.1.5, ffmpegthumbnailer >= 2.0.7", exif >=  0.6.20,dvdauthor >= 0.6.18} #Add deps that need to be added to the slack-required file here
EXRB=${EXRB:-""} #Add deps that need to be excluded from the slack-required file here

VERSION=${VERSION:-1.7.3}
if [ "${SB_NATIVE}" = "YES" ] ;then
  ARCH=${ARCH:-$(uname -m)}
else
  ARCH=${ARCH:-x86_64}
fi
if [ "${ARCH}" = "x86_64" ] ;then
  SLKTARGET=${SLKTARGET:-x86_64}
else
  SLKTARGET=${SLKTARGET:-i486}
fi
SLKDTARGET=${SLKDTARGET:-slackware}
BUILD=${BUILD:-1}
NJOBS=${NJOBS:-$(( $(getconf _NPROCESSORS_ONLN) + 1 ))}
DOCDIR=${PKG}/usr/doc/${NAME}-${VERSION}
SBDIR=${PKG}/usr/src/slackbuilds/${NAME}
PKGDEST=${PKGDEST:-${CWD}}
PKGFORMAT=${PKGFORMAT:-txz}
PKGNAME=${NAME}-$(echo ${VERSION} | tr - . )-${ARCH}-${BUILD}${PACKAGER_ID}

DATE=$(LC_ALL=C date +%d-%b-%Y)

SRCDIR=${PNAME}
SRCARCHIVE=${SRCDIR}-${VERSION}.tar.gz

DL_PROG=${DL_PROG:-wget}
DL_TO=${DL_TO:-5}
DL_OPTS=${DL_OPTS:-"--timeout=${DL_TO}"}
DL_URL="http://download.tuxfamily.org/${NAME}/${SRCARCHIVE}"

# if source is not present, download in source rootdir if possible
test -r ${CWD}/${SRCARCHIVE} || ${DL_PROG} ${DL_OPTS} ${DL_URL} || exit 1

if [ "${SB_NATIVE}" = "YES" ] ;then
  SLKCFLAGS="-O2 -march=native -mtune=native -pipe"
  [ "${SB_ECFLAGS}" ] && SLKCFLAGS="${SLKCFLAGS} ${SB_ECFLAGS}"
else
  case "${ARCH}" in
    i[3-6]86)    SLKCFLAGS="-O2 -march=${ARCH} -mtune=i686"
                 ;;
    x86_64)      SLKCFLAGS="-O2 -fPIC"
                 ;;
    s390|*)      SLKCFLAGS="-O2"
                 ;;
  esac
fi
if [ "${ARCH}" = "x86_64" ] ;then
  LIBDIRSUFFIX="64"
  SLKCFLAGS="${SLKCFLAGS} -fPIC"
else
  LIBDIRSUFFIX=""
fi

if [ -d ${PKG} ]; then
  # Clean up a previous build
  rm -rf ${PKG}
fi
mkdir -p ${PKG}

unset QTDIR QTINC QTLIB
export QTDIR=$(qmake-qt4 -query QT_INSTALL_PREFIX)
PATH="$(qmake-qt4 -query QT_INSTALL_BINS):${PATH}" ; export PATH

cd ${TMP}
rm -rf ${SRCDIR}
tar -xvf ${CWD}/${SRCARCHIVE} || exit 1
cd ${SRCDIR} || exit 1

chmod -R u+w,go+r-w,a-s .
find . -type d -print0 | xargs -0 chmod 0755
find . -type f -print0 | xargs -0 chmod 0644

rm -f *.o

# fix installation path
for file in \
  diaporama.cpp list_themes.cpp mainfrm.cpp media_browser.cpp \
  rendering.cpp tetrahedron.cpp; do
  sed -i "s:qApp->applicationDirPath().\?+.\?\":\"/usr/share/${NAME}/:" \
  ${file} || exit 1
done

sed -i "s:qApp->applicationDirPath():\"/usr/share/${NAME}/\":" \
  mainfrm.cpp || exit 1

if [ -r ${CWD}/apply-patches.sh ]; then
  . ${CWD}/apply-patches.sh || exit 1
fi

# Blanks, parenthesis and other special characters on file names... oh, man
( cd Bibliotheque/Themes || exit 1
  for dir in */* ;do
    mv "${dir}" "$(echo ${dir} | sed -e 's| |_|g;s|)||g;s|(||g;s|é|e|g;s|ç|c|g')"
  done
)
( cd Bibliotheque/Masks || exit 1
  for file in *.png ;do
    mv "${file}" "$(echo ${file} | sed -e 's|è|e|g')"
  done
)

for filepro in $(find . -type f -name "*.pro") ; do
  echo "QMAKE_CFLAGS_RELEASE = ${SLKCFLAGS}" >> "${filepro}"
  echo "QMAKE_CXXFLAGS_RELEASE = ${SLKCFLAGS}" >> "${filepro}"
  echo "QMAKE_LFLAGS_RELEASE = ${LDFLAGS}" >> "${filepro}"
done

qmake ${PNAME}.pro PREFIX=/usr || exit 1

make -j${NJOBS} || make || exit 1

mkdir -p ${PKG}/usr/bin
install -p -m 0755 ${PNAME} ${PKG}/usr/bin/ || exit 1

mkdir -p ${PKG}/usr/share/${NAME}
cp -r Bibliotheque ${PKG}/usr/share/${NAME}/ || exit 1
cp -r Interface ${PKG}/usr/share/${NAME}/ || exit 1
cp -r fake.pl ${PKG}/usr/share/${NAME}/ || exit 1

install -pm0644 *.qm ${PKG}/usr/share/${NAME}/ || exit 1

find ${PKG} | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

mkdir -p ${PKG}/usr/share/applications
cat > ${PKG}/usr/share/applications/${NAME}.desktop <<EOF
[Desktop Entry]
Name=${PNAME}
Type=Application
Exec=${PNAME}
Icon=${NAME}
GenericName=ManDVD
Comment=Video DVD Creator
Comment[sv]=Skapa DVD Video
Terminal=false
Categories=Qt;AudioVideo;Video;DiscBurning;
X-KDE-StartupNotify=true
X-Desktop-File-Install-Version=0.15
EOF

mkdir -p ${PKG}/usr/share/icons/hicolor/128x128/apps
install -m0644 Interface/mandvd.png \
               ${PKG}/usr/share/icons/hicolor/128x128/apps/${NAME}.png || exit 1
mkdir -p ${PKG}/usr/share/icons/hicolor/48x48/apps
convert Interface/mandvd.png -depth 8 -filter Lanczos -resize 48x48 \
        ${PKG}/usr/share/icons/hicolor/48x48/apps/${NAME}.png || exit 1

# Add a documentation directory:
mkdir -p ${DOCDIR}
cp -a \
  COPYING README.txt ${CWD}/ChangeLog.SB \
  ${DOCDIR}/
find ${DOCDIR}/ -type d -print0 | xargs -0 chmod 0755
find ${DOCDIR}/ -type f -print0 | xargs -0 chmod 0644

mkdir -p ${PKG}/install
cat ${CWD}/slack-desc > ${PKG}/install/slack-desc
#cat ${CWD}/slack-required > ${PKG}/install/slack-required

sed -i "s|_PACKAGER|${PACKAGER}|g; s|_BUILD_DATE|${DATE}|g" \
       ${PKG}/install/slack-desc

if [ "${SB_REP}" = "YES" ] ;then
  # Replicate slackbuild and patches
  mkdir -p ${SBDIR}/patches
  install -m0644 ${CWD}/slack-desc ${CWD}/slack-required ${CWD}/ChangeLog.SB \
                 ${CWD}/apply-patches.sh ${SBDIR}/
  install -m0755 ${CWD}/${NAME}.SlackBuild \
                 ${SBDIR}/${NAME}.SlackBuild
  install -m0644 ${CWD}/patches/*.* \
                 ${SBDIR}/patches/
fi

# Build package:
set +o xtrace        # no longer print commands upon execution

ROOTCOMMANDS="set -o errexit -o xtrace ; cd ${PKG} ;
  /bin/chown --recursive root:root .  ;"

 echo "Finding dependencies..."
ADD="$ADDRB" EXCLUDE="$EXRB" requiredbuilder -v -y -s ${SRCDIR} $PKG 


ROOTCOMMANDS="${ROOTCOMMANDS}
  /sbin/makepkg --linkadd y --chown n ${PKGDEST}/${PKGNAME}.${PKGFORMAT} "

if test ${UID} = 0; then
  eval ${ROOTCOMMANDS}
  set +o xtrace
elif test "$(type -t fakeroot)" = 'file'; then
  echo -e "\e[1mEntering fakeroot environment.\e[0m"
  echo ${ROOTCOMMANDS} | fakeroot
else
  echo -e "\e[1mPlease enter your root password.\e[0m (Consider installing fakeroot.)"
  /bin/su -c "${ROOTCOMMANDS}"
fi

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  echo "Cleaning..."
  if [ -d ${TMP}/${SRCDIR} ]; then
    rm -rf ${TMP}/${SRCDIR} && echo "${TMP}/${SRCDIR} cleanup completed"
  fi
  if [ -d ${PKG} ]; then
    rm -rf ${PKG} && echo "${PKG} cleanup completed"
  fi
  rmdir ${TMP} && echo "${TMP} cleanup completed"
fi
exit 0
