#!/bin/bash
#
#  p7zip.SlackBuild is free software; you can redistribute it and/or modify it under the
#  terms of the GNU General Public License as published by the Free Software
#  Foundation; either version 2 of the License, or any later version.
#
#  p7zip.SlackBuild is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
#  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along with
#  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
#  Place - Suite 330, Boston, MA 02111-1307, USA
#
#  Original by Heinz Wiesinger, Amsterdam, Netherlands


# default settings
PACKAGE="p7zip"
PACKAGE_EXT="bz2"
VERSION=${VERSION:=9.20.1}
BUILD=${BUILD:=1}

if [ -f ~/.slackbuildrc ]; then
  . ~/.slackbuildrc
elif [ -f ../../runtime/slackbuildrc ]; then
  . ../../runtime/slackbuildrc
fi

check_required

SRC="${PACKAGE}_${VERSION}_src_all.tar.$PACKAGE_EXT"
URL="http://downloads.sourceforge.net/project/p7zip/p7zip/${VERSION}/$SRC"

get_source $SRC $URL
unpack_source $SRC ${PACKAGE}_${VERSION}


# building the GUI
GUI=${GUI:-no}

make all3 \
  OPTFLAGS="$SLKCFLAGS" \
  DEST_HOME=/usr \
  DEST_SHARE_DOC=/usr/doc/$PACKAGE-$VERSION \
  DEST_SHARE=/usr/lib$LIBDIRSUFFIX/p7zip \
  DEST_DIR=$PKG

if ! [ "$GUI" = "no" ]; then
    sed -i "s|/usr/lib/|/usr/lib$LIBDIRSUFFIX/|g" CPP/7zip/UI/GUI/makefile.depend
    make 7zG \
    OPTFLAGS="$SLKCFLAGS" \
    DEST_HOME=/usr \
    DEST_SHARE_DOC=/usr/doc/$PACKAGE-$VERSION \
    DEST_SHARE=/usr/lib$LIBDIRSUFFIX/p7zip \
    DEST_DIR=$PKG
fi

make install \
  OPTFLAGS="$SLKCFLAGS" \
  DEST_HOME=/usr \
  DEST_SHARE_DOC=/usr/doc/$PACKAGE-$VERSION \
  DEST_SHARE=/usr/lib$LIBDIRSUFFIX/p7zip \
  DEST_DIR=$PKG

# install gzip like wrapper
install -m 0755 contrib/gzip-like_CLI_wrapper_for_7z/p7zip $PKG/usr/bin/
install -m 0644 contrib/gzip-like_CLI_wrapper_for_7z/man1/p7zip.1 $PKG/usr/man/man1/

# install midnight commander vfs add on
mkdir -p $PKG/usr/share/mc/extfs/
install -m 755 contrib/VirtualFileSystemForMidnightCommander/u7z \
  $PKG/usr/share/mc/extfs/

if ! [ "$GUI" = "no" ]; then
  # Unfortunately we have to install 7zG by hand
  install -m 555 $PKG/usr/bin/7z $PKG/usr/bin/7zG
  sed -i "s|/usr/lib$LIBDIRSUFFIX/p7zip/7z|/usr/lib$LIBDIRSUFFIX/p7zip/7zG|" $PKG/usr/bin/7zG

  install -m 555 bin/7zG $PKG/usr/lib$LIBDIRSUFFIX/p7zip/7zG
  cp -r GUI/Lang $PKG/usr/lib$LIBDIRSUFFIX/p7zip/Lang
  find $PKG/usr/lib$LIBDIRSUFFIX/p7zip/Lang -type d -exec chmod 555 {} \;
  find $PKG/usr/lib$LIBDIRSUFFIX/p7zip/Lang -type f -exec chmod 444 {} \;

  cp -r GUI/help $PKG/usr/lib$LIBDIRSUFFIX/p7zip/help
  find $PKG/usr/lib$LIBDIRSUFFIX/p7zip/help -type d -exec chmod 555 {} \;
  find $PKG/usr/lib$LIBDIRSUFFIX/p7zip/help -type f -exec chmod 444 {} \;

  install -m 555 GUI/p7zipForFilemanager  $PKG/usr/bin/p7zipForFilemanager

  install -m 444 -D GUI/p7zip_16_ok.png $PKG/usr/share/icons/hicolor/16x16/apps/p7zip.png

  mkdir -p $PKG/usr/share/kde4/services/ServiceMenus
  cp GUI/kde4/*.desktop $PKG/usr/share/kde4/services/ServiceMenus/
fi



fixup_package $PKG

# Clean up the extra stuff:
set +u
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$PACKAGE-$VERSION
  rm -rf $PKG
fi