#
#  Common functions for slackbuild scripts.

do_nothing() {
    # no, I don't remember why this is here.
    echo Hi mom!

} 

#======================================================================
set_workdir() {

    #----------------------------------------
    # Set/create the working directory and some other path variables
    #----------------------------------------

    # set working directory
    if [ "$workdir" == "." ]; then
	workdir=`pwd`/$pkgname
    else
	workdir=$workdir/$pkgname
    fi

    # create the working directory if necessary
    if [ ! -d $workdir ]; then
	echo Creating working directory $workdir ...
	mkdir -p $workdir
    fi

    # other vars
    builddir=$workdir/$pkgname-$pkgver
    slackver=`cat /etc/slackware-version`
    thisfile=`pwd`/`basename $0`
    libfile=`pwd`/sbuild.functions

    echo Working directory is $workdir
    cd $workdir
    echo Changed directory to  `pwd`

}

#======================================================================
set_target() {
    # set the location and create the target directory
    target=$workdir/target
    if [ ! -d $target ]; then
	mkdir $target
    fi
}

#======================================================================
run_configure() {
    echo Running configure...
    ./configure  $1 \
	> $workdir/config.output 2>&1

    if [ $? -ne 0 ]; then
	echo There was a problem with the build process.
	echo check the contents of $workdir/config.output
	echo Exiting...
	exit
    fi
}

#======================================================================
run_make() {
    echo Running make...
    make > $workdir/make.output 2>&1
    if [ $? -ne 0 ]; then
	echo There was a problem with the build process.
	echo check the contents of $workdir/make.output
	echo Exiting...
	exit
    fi

}

#======================================================================
run_makeinstall() {
    echo Installing to $target ...
    make install DESTDIR=$target > $workdir/install.output 2>&1

}

#======================================================================
create_docdir() {

    #----------------------------------------
    # create documentation directory
    #----------------------------------------
    docdir=$target/usr/doc/$pkgname-$pkgver
    mkdir -p $docdir
    cp $builddir/[A-Z]* $docdir
    mkdir -p $docdir/Slackware
    cp -v $thisfile $docdir/Slackware/SlackPkg.sh
    cp -v $libfile $docdir/Slackware/
    chmod -R a+r $target/usr/doc/$pkgname-$pkgver/*
}

#======================================================================
fix_bingrp() {
#----------------------------------------
# fix group on bin directories.
#----------------------------------------
    for i in bin sbin usr/sbin usr/bin usr/X11R6/bin opt/kde/bin
      do
      if [ -d $target/$i ]; then
	  echo Changing group on $target/$i
	  chgrp -R bin $target/$i
      fi
    done
}

#======================================================================
strip_bin() {
#----------------------------------------
# strip  bin files
#----------------------------------------
    echo Stripping files...
    for i in bin sbin usr/sbin usr/bin usr/X11R6/bin opt/kde/bin
      do
      if [ -d $target/$i ]; then
	  for k in $target/$i/*
	    do
	    if [ -f $k ]; then
		echo Stripping file $k
		ls -lh $k |awk -F" " '{print $5}'
		strip $k
		ls -lh $k |awk -F" " '{print $5}'
	    fi
	  done
      fi
    done
}

#======================================================================
strip_libs() {
#----------------------------------------
# strip lib files
#----------------------------------------
    echo Stripping libs...
    for i in lib  usr/lib  usr/X11R6/lib opt/kde/lib
      do
      if [ -d $target/$i ]; then
	  for k in $target/$i/*
	    do
	    if [ -f $k ]; then
		echo Stripping file $k
		ls -lh $k |awk -F" " '{print $5}'
		strip $k
		ls -lh $k |awk -F" " '{print $5}'
	    fi
	  done
      fi
    done
}

#======================================================================
compr_man() {
#----------------------------------------
# check for uncompressed man pages
#----------------------------------------
    for i in usr/man opt/kde/man usr/share/man usr/X11R6/man
      do
      if [ -d $target/$i ]; then
	  find $target/$i -name \*.[0-9] -exec gzip {} \;
      fi
    done
}

#======================================================================
compr_info() {
#----------------------------------------
# check for uncompressed info pages
#----------------------------------------
    if [ -d $target/usr/info ]; then
	find $target/usr/info -name \*.info\* -exec gzip {} \;
    fi
    if [ -r $target/usr/info/dir ]; then
	rm $target/usr/info/dir
    fi
}

#======================================================================
create_pkg() {

# makepkg
    cd $target
    echo Creating package...
    makepkg  -c n -l y $workdir/$package 
    echo Package created and located in $workdir
    cd $workdir
    echo DONE
}

#======================================================================
