###
## Slackware Package Build Script
##  
## This file is to assist you in setting global
## package variables that can are used in Slackware
## build scripts download from Linuxpackages.net
##
##       Copyright (C) 2004 LinuxPackages.net
## 
##   Redistribution and use of this software, with or without modification, is
##   permitted provided that the following conditions are met:
##
## 1. Redistributions of this software 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.

## Slightly modified version of this script by Georgi D. Sotirov <gdsotirov@dir.bg>


## Version of this conf 
## Updated for Slackware 13.0
CONFVER=1.9.1

## We need to set the umask since by default root has 0077 and this will 
## create broken packages.
umask 0022

## Gota know where we are
CWD=`pwd`

## See if the TMP variable is set
if [ "$TMP" = "" ]; then 
          TMP=/usr/src/tmp
fi 
## Where to put packages once built
PKG_DIR="/usr/src/slackpack"
if [ ! -d $PKG_DIR ]; then
 mkdir -p $PKG_DIR
fi
 

## Your build tag for the build field such as jim replace the XXX.
MYIN="gds"

## Location of wget this should be fine.
WGET="/usr/bin/wget -c"

## Set the arch we will build for
#ARCH="i386"
#ARCH="i486"
#ARCH="i586"
#ARCH="i686"
ARCH="x86_64"

## Determin the CFLAGS based on the arch from above
if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mtune=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX="64"
fi

## CFLAGS we get these from the above settings
CFLAGS="$SLKCFLAGS" 

CXX=gcc 

## CXXFLAGS same as the CFLAGS
CXXFLAGS="$SLKCFLAGS"

## MaKe some generic funtions for cleaning stipping and permissions

## Call this in the script to remove debug and junk
strip_bin() {
find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
}

## Call this in the script to strip libs
strip_lib() {
find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
}

## Call this in the script to strip static libs
strip_static() {
find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null
}

## Call this to zip up and link the man pages
gzip_man() {
  if [ -d $1/usr/man ]; then
    ( cd $1/usr/man
      for manpagedir in $(find . -type d -name "man*") ; do
        ( cd $manpagedir
          for eachpage in $( find . -type l -maxdepth 1) ; do
            ln -s $( readlink $eachpage ).gz $eachpage.gz
            rm $eachpage
          done
          gzip -9 *.?
        )
      done
    )
  fi
}

## Call this to zip up the info documents
gzip_info() {
  if [ -d $1/usr/info ]; then
    ( cd $1/usr/info
      rm -f dir
      gzip -9 *
    )
  fi
}

## Call this to make sure bin and sbin are the correct groups
bin_perms() {
for DIRECTORY in bin usr/bin usr/local/bin usr/X11R6/bin opt/kde/bin; do
if [ -d $1/$DIRECTORY ]; then
chown -R root.root $1/$DIRECTORY
chmod -R 755 $1/$DIRECTORY
fi;done
}

sbin_perms() {
for DIRECTORY2 in sbin usr/sbin usr/local/sbin; do
if [ -d $1/$DIRECTORY2 ]; then
chown -R root.root $1/$DIRECTORY2
chmod -R 755 $1/$DIRECTORY2
fi;done
} 


## Call this to fix various permissions on the source before the compile
## The last two entries are for suid bins uncomment if you wish to remove them.

fix_source() {
chown -R root.root .
find . \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) -exec chmod 755 {} \;
find . \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;
}


## This will handle documents care of Piotr Simon taken from checkinstall
DOCFILES="ABOUT ABOUT-NLS ANNOUNCE AUTHORS CONFIGURATION CHANGES COPYING COPYRIGHT CREDITS ChangeLog \ 
Changelog CHANGELOG CONTRIBUTORS *FAQ* FEATURES FILES HACKING History HISTORY INSTALL* LICENSE LSM \ 
MANIFEST NEWS *README* *Readme* SITES *RELEASE* RELNOTES THANKS TIPS TODO VERSION CONFIGURATION* \
GPL License Roadmap ROADMAP *BUGS* index.ht* *INDEX*"

## Pass the pkg variable program name and version to this function to setup all the documents.
## Example create_docs $PKG apache-2.0.49  I use this method due to some scripts build mulitple
## packages.

create_docs () {
DOCS=$1/usr/doc/$2
mkdir -p ${DOCS}
for i in ${DOCFILES}; do
  if [ -e ${i} ]; then
   if ! [ -L ${i} ]; then
 cp -a ${i} ${DOCS}
 else
cp -LRp ${i} ${DOCS}
 fi
fi;done
chmod 644 $1/usr/doc/$2/*
chown -R root.root $1/usr/doc/$2/*
}


## A small function to uprade the slackbuilds package if needed

upgrade_self() {
# rm -rf 
mkdir -p $TMP/slackbuilds-install
cd $TMP/slackbuilds-install
VER_LOC="http://www1.linuxpackages.net/packages/SlackBuilds/LATESTVER"
$WGET -q $VER_LOC || true
NEWVER=`cat LATESTVER`
if [[ "`cat LATESTVER`" > "$CONFVER" ]]; then
   echo " Upgraded needed $CONFVER $NEWVER "
   UPG_LOC="http://www1.linuxpackages.net/packages/SlackBuilds/slackbuilds-$NEWVER-noarch-1jim.tgz"
   $WGET $UPG_LOC || true
   upgradepkg slackbuilds-$NEWVER-noarch-1jim.tgz
   cd ..
   rm -rf slackbuilds-install
   exit
else 
   echo "Your version is current no upgrade needed your Version $CONFVER Latest Version $NEWVER"
   cd ..
   rm -rf slackbuilds-install
   exit
fi
}


