#!/usr/bin/bash
# This script assumes it will be launched within "/PKGNAME/VERSION/src" dir.
# With all sources in "src" Your Vector Linux .tlz package, slack-desc,
# and slack-required will be found in "VERSION" dir. The extraction and
# build will be in a temp dir created in "PKGNAME" dir, and then removed on exit.
# Comment out second to last line to keep this dir intact.
#
# This Template was compiled from the contributions of many users of the Vector
# Linux forum at http://forum.vectorlinux.com and from tidbits collected 
# from all over the internet. 
#
# Generated by sbbuilder-0.4.12.3, written by Rodrigo Bistolfi 
# (rbistolfi) and Raimon Grau Cuscó (Kidd) for VectorLinux.
#
# Please put your name below if you add some original scripting lines.
# AUTHORS = Joe1962 (postgresql scripting)

PKGNAME="postgresql"         #Enter package Name!
PKGVERSION=${PKGVERSION:-"9.1.5"}      #Enter package Version!
VER=$(echo $VERSION|sed 's/-/_/') #this fixes - in version
TARNAME="$PKGNAME-$PKGVERSION"         #Enter tarball Name!
DIRNAME="$PKGNAME-$PKGVERSION"            #Enter inner dir Name!
VL_PACKAGER=${VL_PACKAGER:-"Joe1962"}   #Enter your Name!
LINK=${LINK:-"http://ftp.postgresql.org/pub/source/v$PKGVERSION/$PKGNAME-$PKGVERSION.tar.bz2"}  #Enter URL for package here!

#SYSTEM VARIABLES
#----------------------------------------------------------------------------
BUILDNUM=${BUILDNUM:-"1"}
VL_VERSION=${VL_VERSION:-"$(ls /var/log/packages/|grep vlconfig2|cut -d "-" -f4|cut -c 2-5)"}
BUILD=${BUILD:-"$BUILDNUM""$VL_VERSION"}
ARCH=${ARCH:-"$(uname -m)"}
CONFIG_OPTIONS=${CONFIG_OPTIONS:-"--enable-nls --with-perl --with-python --with-tcl --with-openssl --with-ldap --enable-thread-safety --with-libxml --with-libxslt --docdir=/usr/doc/ --with-system-tzdata=/usr/share/zoneinfo/"}
###:  --with-gssapi  --with-krb5
LDFLAG_OPTIONS=${LDFLAG_OPTIONS:-""}
ADDRB=${ADDRB:-""} #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
MAKEDEPENDS=${MAKEDEPENDS:-""} #Add deps needed TO BUILD this package here.
#----------------------------------------------------------------------------

# DO NOT EXECUTE if NORUN is set to 1
if [ "$NORUN" != "1" ]; then

#SETUP PACKAGING ENVIRONMENT
#--------------------------------------------
CWD=$(pwd)
cd ../
RELEASEDIR=$(pwd)
cd $CWD
mkdir -p $RELEASEDIR/tmp
TMP=$RELEASEDIR/tmp
PKG=$TMP/package-$PKGNAME
#--------------------------------------------

if [ $UID != 0 ]; then
   echo "You are not authorized to run this script. Please login as root"
   exit 1
fi

if [ ! -x /usr/bin/requiredbuilder ]; then
   echo "Requiredbuilder not installed, or not executable."
   exit 1
fi

if [ $VL_PACKAGER = "YOURNAME" ]; then
   echo 'Who are you?
   Please edit VL_PACKAGER=${VL_PACKAGER:-YOURNAME} in this script.
   Change the word "YOURNAME" to your VectorLinux packager name.
   You may also export VL_PACKAGER, or call this script with
   VL_PACKAGER="YOUR NAME HERE"'
   exit 1
fi


#CFLAGS SETUP
#--------------------------------------------
if [[ "$ARCH" = i?86 ]]; then
  ARCH=i586
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  CONFIGURE_TRIPLET="i486-slackware-linux"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fpic"
  CONFIGURE_TRIPLET="x86_64-slackware-linux"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "powerpc" ]; then
  SLKCFLAGS="-O2"
  CONFIGURE_TRIPLET="powerpc-vlocity-linux"
  LIBDIRSUFFIX=""
fi

export CFLAGS="$SLKCFLAGS $CFLAG_OPTIONS"
export CXXFLAGS=$CFLAGS
export LDFLAGS="$LDFLAGS $LDFLAG_OPTIONS"
#--------------------------------------------


#GET THE SOURCE
#--------------------------------------------
for SRC in $(echo $LINK);do
if [ ! -f $CWD/$(basename $SRC) ]
then
        wget -c $SRC
fi
done
#--------------------------------------------

rm -rf $PKG
mkdir -p $PKG
cd $TMP
rm -rf $DIRNAME

#EXTRACT SOURCES
#-----------------------------------------------------
echo "Extracting source..."
tar xvf $CWD/$TARNAME.tar.* || exit 1
#-----------------------------------------------------

cd $TMP/$DIRNAME

# Put any Patches into a patches folder in the src dir
#-----------------------------------------------------
for i in $CWD/patches/*;do
  patch -p1 <$i
  mkdir -p $PKG/usr/doc/$DIRNAME/patches/
  cp $i $PKG/usr/doc/$DIRNAME/patches/
done
#-----------------------------------------------------

#SET PERMISSIONS
#-----------------------------------------
echo "Setting permissions..."
chown -R root:root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 2777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 2755 -exec chmod 755 {} \;
find . -perm 774 -exec chmod 644 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
#-----------------------------------------


#CONFIGURE & MAKE
#----------------------------------------------------------------------
# If you are building a KDE-related app, then change the following
# arguments in the script below:
# --prefix=$(kde-config -prefix) \
# --sysconfdir=/etc/kde \
#
# Making these changes will ensure that your package will build in the
# correct path and that it will work seamlessly within the KDE environment.
#
#-----------------------------------------------------------------------

echo "Configuring source..."
./configure --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --infodir=/usr/info \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --with-included-gettext \
  --disable-debug \
  --program-prefix="" \
  --program-suffix="" \
  --build=$CONFIGURE_TRIPLET \
  $CONFIG_OPTIONS || exit 1

make || exit 1

make install DESTDIR=$PKG || exit


#######################################################################
#Miscellaneous tweaks and things outside a normal ./configure go here #
#######################################################################

mkdir -p $PKG/install

#if there is a doinst.sh in src dir use it
if test -f $CWD/doinst.sh.gz; then
	zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
fi
if test -f $CWD/doinst.sh; then
	cp -a $CWD/doinst.sh > $PKG/install/doinst.sh
fi

mkdir -p $PKG/usr/doc/$PKGNAME-$PKGVERSION

## Add common docs files:
cp -a \
ANNOUNCE* \
AUTHORS* \
BUGS* \
COPYING* \
ChangeLog* \
CHANGELOG* \
COPYRIGHT \
Credits* \
DEVELOPERS \
HISTORY \
INSTALL \
LICENSE* \
MANIFEST \
MAINTAINER* \
PROVENANCE \
Readme* \
README* \
TODO* \
Exception* \
EXCEPTION* \
$PKG/usr/doc/$PKGNAME-$PKGVERSION

## Add specific docs files:
cp -a \
doc/KNOWN_BUGS \
doc/MISSING_FEATURES \
doc/README.mb.big5 \
doc/README.mb.jp \
doc/TODO \
doc/bug.template \
$PKG/usr/doc/$PKGNAME-$PKGVERSION

cat $CWD/$PKGNAME.SlackBuild > $PKG/usr/doc/$PKGNAME-$PKGVERSION/$PKGNAME.SlackBuild

#----------------------------------------------------------------------

# adding scripts and menuitems:
echo "Adding scripts and menuitems..."
cd $CWD
##cd menuitems
cp -r menuitems/* $PKG
##cd ../
mkdir $PKG/usr/share/$PKGNAME/scripts/
##cd pg_scripts
chmod +x pg_scripts/*
cp -a pg_scripts/* $PKG/usr/share/$PKGNAME/scripts/
##cd ../
##cd sys_scripts
chmod +x sys_scripts/*
cp -a sys_scripts/* $PKG/usr/share/$PKGNAME/scripts/
##cd ../

### Now build adminpack:
#--------------------------------------------------------------
echo "building adminpack..."
cd $TMP/$DIRNAME/contrib/adminpack
make || exit 1
make install DESTDIR=$PKG/ || exit 1


#----------------------------------------------------------------------

if [ -d $PKG/usr/share/man ] ; then
mkdir -p $PKG/usr/man
mv $PKG/usr/share/man/* $PKG/usr/man
rm -rf $PKG/usr/share/man
fi
find $PKG/usr/man -type f -exec gzip -9 {} \;

if [ -d $PKG/usr/share/info ] ; then
mkdir -p $PKG/usr/info
mv $PKG/usr/share/info/* $PKG/usr/info
rm -rf $PKG/usr/share/info
fi 

find $PKG/usr/info -type f -exec gzip -9 {} \;

## THIS DOESN'T SEEM TO WORK>
#if [ -d $PKG/usr/info ] ; then
#cat >> $PKG/install/doinst.sh << EOF
#cd usr/info
#if [ -f dir ]; then
#    rm dir
#fi
#if [ -f dir.gz ]; then
#    rm dir.gz
#fi
#for i in *.info.gz
#        do
#        install-info $i dir
#    done
#EOF
#fi
## <THIS DOESN'T SEEM TO WORK

#if there is a slack-desc in src dir use it
if test -f $CWD/slack-desc; then
	cp -a $CWD/slack-desc $RELEASEDIR/slack-desc
else

# This creates the white space in front of "handy-ruler" in slack-desc below.
LENGTH=$(expr length "$PKGNAME")
SPACES=0
SHIM=""
until [ "$SPACES" = "$LENGTH" ]; do
SHIM="$SHIM "
let SPACES=$SPACES+1
done

# Fill in the package summary between the () below.
# Then package the description, License, Author and Website.
# There may be no more then 11 $PKGNAME: lines in a valid slack-desc.

cat > $RELEASEDIR/slack-desc << EOF
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

$SHIM|-----handy-ruler------------------------------------------------------|
$PKGNAME: $PKGNAME (Object-Relational Database Management System)
$PKGNAME: 
$PKGNAME: PostgreSQL is an advanced object-relational database management 
$PKGNAME: system that supports an extended subset of the SQL standard, 
$PKGNAME: including transactions, foreign keys, subqueries, triggers, 
$PKGNAME: user-defined types and functions.
$PKGNAME: 
$PKGNAME: License: BSD http://www.postgresql.org/about/licence
$PKGNAME: Authors: PostgreSQL Global Development Group
$PKGNAME: Website: http://www.postgresql.org
$PKGNAME: 
EOF
fi
cat >> $RELEASEDIR/slack-desc << EOF

#----------------------------------------
BUILDDATE: $(date)
PACKAGER:  $VL_PACKAGER
HOST:      $(uname -srm)
DISTRO:    $(cat /etc/vector-version)
CFLAGS:    $CFLAGS
LDFLAGS:   $LDFLAGS
CONFIGURE: $(awk "/\.\/configure\ /" $TMP/$DIRNAME/config.log)

EOF

cp -a $RELEASEDIR/slack-desc $PKG/install/slack-desc
cat $CWD/$PKGNAME.SlackBuild > $PKG/usr/doc/$PKGNAME-$PKGVERSION/$PKGNAME.SlackBuild

#STRIPPING
#------------------------------------------------------------------------------------------------------------------
cd $PKG
echo " "
echo "Stripping...."
echo " "
find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
#------------------------------------------------------------------------------------------------------------------

#FINISH PACKAGE
#--------------------------------------------------------------
echo "Finding dependencies..."
ADD="$ADDRB" EXCLUDE="$EXRB" requiredbuilder -v -y -s $RELEASEDIR $PKG

# append to doinst.sh:
cat >> $PKG/install/doinst.sh << EOF
( cd usr/share/postgresql/scripts/ ; cp -a postgresql.logrotate /etc/logrotate.d/postgresql )
( cd usr/share/postgresql/scripts/ ; cp -a postgresql.service /etc/rc.d/init.d/postgresql )
EOF

echo "Creating package $PKGNAME-$PKGVERSION-$ARCH-$BUILD.txz"
makepkg -l y -c n $RELEASEDIR/$PKGNAME-$PKGVERSION-$ARCH-$BUILD.txz

cd $CWD
echo "Cleaning up temp files..." && rm -rf $TMP
echo "Package Complete"
#--------------------------------------------------------------
fi
# vim: set tabstop=4 shiftwidth=4 foldmethod=marker : ##
