#!/bin/sh
# This is SlackBuild script, generated by slabgen utility
# Template for this file was created by Sergey Mironov (ierton@gmail.com)
# 05 April 2008, Moscow, Russia.

# see function main() for main program
# see function set_global_vars() for configurations

# This script
THIS=$0
CWD=`pwd`


#*****************************
# MAIN PART
#*****************************

function set_global_vars()
{
    NAME=liboil
    VERSION=0.3.14

    # TODO: modify arch according to your system
    ARCH=i486
    
    # Build revision
    REVISION=1
    
    # Short user signature
    MYIN=irn

    # Extention of archive
    TAR_TYPE=tar.gz

    # Name of archive to unpack
    DIST=$NAME-$VERSION.$TAR_TYPE
    
    # Full package name
    PKG_NAME=$NAME-$VERSION-$ARCH-$REVISION$MYIN
    
    # Temp directory (for unpacking, compiling and packet creation)
    TMP=/tmp/slabgen   
    
    # Distribution directory 
    SRC=$TMP/$NAME/$NAME-$VERSION    
    
    # Target install DESTDIR directory
    PKG=$SRC/slack-package
    
    # Target package directory
    PKG_DIR=$TMP/$NAME
    
    # Build direcory (can differs from distribution directory)
    # BLD=${BLD:-$SRC/slack-build}
    BLD=$SRC    

    # Some system directories
    MANDIR=/usr/man
    INFODIR=/usr/info
    SHAREDIR=/usr/share
    SLACKBUILDSDIR=/usr/src/slackbuilds/$NAME-$VERSION
    DOCDIR=/usr/doc/$NAME-$VERSION

    if [ ! "$ARCH" = "x86_64" ]; then
        SLKCFLAGS="-O2 -march=$ARCH -mtune=i686"
    else
        SLKCFLAGS="-O2 -march=x86_64"
    fi
    export CFLAGS=$SLKCFLAGS
    export CCFLAGS=$SLKCFLAGS
}

function main()
{
    local stage=$1
    set_global_vars

    mkdir -p "$TMP" >/dev/null 2>&1
    mkdir -p "$SRC" >/dev/null 2>&1
    mkdir -p "$BLD" >/dev/null 2>&1
    rm -R --force $PKG  > /dev/null 2>&1    
    mkdir -p "$PKG" >/dev/null 2>&1

    
    # Unpacking source code
    if ((stage<1)) ; then
        cd "$TMP/$NAME" || die "Cant cd to \"$TMP/$NAME\""
         tar -zxvf "$CWD/$DIST" || die "error: tar extract FAILED" # *.gz
        # tar -jxvf "$CWD/$DIST" || die "error: tar extract FAILED" # *.bz2
        # tar -xvf "$CWD/$DIST" || die "error: tar extract FAILED"  # *.tar
    fi

    # Changing directory to build location    
    cd "$BLD" || die "Can't cd to \"$BLD\""     
    if ((stage<2)) ; then
        $SRC/configure --prefix=/usr \
            --sysconfdir=/etc \
            --localstatedir=/var 2>&1 | tee $NAME-configure.log ||
            die "./configure FAILED"    
    fi

    if ((stage<3)) ; then
        make 2>&1 | tee $NAME-make.log ||
            die "make FAILED"
    fi

    if ((stage<4)) ; then
        grep DESTDIR Makefile* || 
            die "Makefile doesn't support DESTIDR. You should modify SlackBuild manually to handle it."

        make install DESTDIR=$PKG 2>&1 | tee $NAME-makeinstall.log ||
            die "make install FAILED"
    
        # Installin this SlackBuild
        ( cd $CWD; cp $THIS $SRC/$NAME.SlackBuild ) || die "Can't cp \"$THIS\" to \"$SRC/$NAME.SlackBuild\""

         
        generate_slack_desc $SRC/slack-desc
        generate_slack_required $SRC/slack-required

        # Installing package info
        mkdir -p $PKG/install
        install -m 644 $SRC/slack-desc  $PKG/install
        install -m 644 $SRC/slack-required  $PKG/install
        
        # Installing slackbuild info
        mkdir -p $PKG$SLACKBUILDSDIR
        for i in slack-desc slack-required $NAME.SlackBuild; do
            install -m 644 $SRC/$i  $PKG$SLACKBUILDSDIR
        done    
    
        # Installing documentation
        mkdir -p $PKG$DOCDIR
        for i in *README* *FAQ* INSTALL* LICENSE *BUGS* ABOUT CHANGELOG AUTHORS $NAME*log $NAME.SlackBuild; do
            install -m 644 $SRC/$i  $PKG$DOCDIR >/dev/null 2>&1
        done
        
        # Compress man pages if they exist
        compress_man_pages $PKG$MANDIR
        
        # Compress info pages if they exist (and remove $INFODIR/dir file)
        compress_info_pages $PKG$INFODIR
    
        # Stripping ;)
        find $PKG | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
        find $PKG | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
        find $PKG | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null
    fi

    if ((stage<5)); then
        # Creating package (root operation)
        echo ""
        echo "Installation complete."
        echo "You can watch packet tree in \"$PKG\"."
        echo "Enter root password, when ready."
        ( cd $CWD; su root -c "$THIS --root '`id -un`:`id -gn`'" ) ||
            die "su root FAILED"
    fi

    # Copying files to local server
    #copy_to_server_via_ssh "$PKG_DIR/$PKG_NAME.tgz"

    # Saving new revision into REVISION file
    echo "All done. New revision is $REVISION"
    echo "$REVISION" > "$SRC/REVISION"
}

function root_mode()
{
    local owner=$1
    set_global_vars
    
    [ "`id -un`" != "root" ] && die "Root user expected"
    [ "$1" = "" ]  && die "owner is not set"    
    [ "$PKG" = "/" ] && die "\"/\" is an invalid root package folder (PKG var)"
    cd "$PKG" || die "Cant cd into $PKG"

    # TODO: Check user ownership policy
    # Changing all ownership to root:root
    /bin/chown -R 'root:root' ./

    # Building packet
    /sbin/makepkg -l y -c n "$PKG_DIR/$PKG_NAME.tgz" || 
        die "makepkg FAILED"

    # Switching ownership to original user
    /bin/chown -R "$owner" ./
    /bin/chown "$owner" "$PKG_DIR/$PKG_NAME.tgz"
}

#*****************************
# HELPER FUNCTIONS
#*****************************

function die()
{
    echo "$@" >&2
    exit -1
}

function usage()
{
    echo "Command arguments:" >&2
    for i in "$(grep -e '.*).*#' $THIS | grep -v grep)"; do echo "$i" >&2; done
    die ""
}

function copy_to_server_via_ssh()
{
    local package=$1    
    local server_name=floppylord
    local server_path=store/linux-packages    
    echo "Copying $package to server"
    scp -r $package $server_name:$server_path 
}

function compress_info_pages()
{
    local infodir=$1
    if [ -d "$infodir" ]; then
        echo "Gzipping info pages ($infodir)"
        gzip -9 $infodir/*.info
        rm -f $infodir/dir
    fi
}

function compress_man_pages()
{
    local mandir=$1
    if [ -d "$mandir" ]; then
        echo "Gzipping man pages ($mandir)"        
        ( cd $mandir
            find . -type f -exec gzip -9 {} \;
            for i in $(find . -type l) ; do ln -s $(readlink $i).gz $i.gz ; rm $i ; done
        ) 
    fi
}   

function generate_slack_desc()
{
    cat > $1 << EOL
liboil: liboil (Optimized function library)
liboil: 
liboil: Liboil is a library of simple functions that are optimized for various
liboil: CPUs. These functions are generally loops implementing simple 
liboil: algorithms, such as converting an array of N integers to 
liboil: floating-point numbers or multiplying and summing an array of N 
liboil: numbers.
liboil: 
liboil: http://liboil.freedesktop.org/wiki/
liboil: 
liboil: Packaged by ierton (ierton@gmail.com)
liboil: Wed, 11 Jun 2008 01:15:03 +0400
EOL
}

function generate_slack_required()
{
    cat > $1 << EOL

EOL
}

case "$1" in
    "unpack") # Start from unpacking
        main 0
        ;;
    "configure") # Start from configuring
        main 1
        ;;
    "make") # Start from make
        main 2
        ;;
    "install") # Start from make install
        main 3
        ;;
    "pkgbuild") # Start from package build 
        main 4
        ;;        
    "--root") 
        # for internal use only
        shift
        root_mode $@
        ;;
	"")
		main 0
		;;
    *)  # Help  
        usage
        ;;
esac


