#!/bin/bash

myver='1.22'
startdir=`pwd`

[ -f /etc/makepkg.conf ] && . /etc/makepkg.conf

strip_url() {
  echo $1 | sed 's|^.*://.*/||g'
}

if [ "$1" = "--help" -o "$1" = "-h" ]; then
  shift
  echo "makepkg version $myver"
  echo "usage: $0 [build_script]"
  echo
  echo "  if build_script is not specified, makepkg will look for a PKGBUILD"
  echo "  file in the current directory."
  echo
  exit 1
fi

BUILDSCRIPT="`pwd`/PKGBUILD"
if [ "$1" != "" ]; then
  BUILDSCRIPT=$1
fi

if [ ! -f $BUILDSCRIPT ]; then
  echo "==> ERROR:  $BUILDSCRIPT does not exist!" >&2
  exit 1
fi

. $BUILDSCRIPT

# check for no-no's
if [ `echo $pkgver | grep '-'` ]; then
	echo "==> ERROR:  pkgver is not allowed to contain hyphens." >&2
	exit 1
fi

echo
d=`date`
echo "==> Building package $pkgname  ($d)" >&2

# extract source
echo "==> Acquiring/Extracting Sources..." >&2
mkdir -p src pkg
cd $startdir/src
for netfile in ${source[@]}; do
  file=`strip_url $netfile`
  if [ -f ../$file ]; then
    echo "==> Found $file in build dir" >&2
    cp ../$file .
  elif [ -f /var/cache/pacman/src/$file ]; then
    echo "==> Using local copy of $file" >&2
    cp /var/cache/pacman/src/$file .
  else
    proto=`echo $netfile | sed 's|://.*||'`
    if [ "$proto" != "ftp" -a "$proto" != "http" ]; then
      echo "==> ERROR:  $netfile was not found in the build directory and is not a proper URL."
      echo "==> Aborting..." >&2
      exit 1
    fi
    # check for a download utility
    if [ -x /usr/bin/snarf ]; then
    	ftpagent="/usr/bin/snarf"
    elif [ -x /usr/bin/lftpget -a "$proto" = "ftp" ]; then
    	ftpagent="/usr/bin/lftpget"
    elif [ -x /usr/bin/wget ]; then
    	ftpagent="/usr/bin/wget --passive-ftp --tries=3 --waitretry=3"
    else
    	echo "==> ERROR:  You need an ftp client installed (snarf/lftp/wget) in /usr/bin" >&2
    	exit 1
    fi
    echo "==> Downloading $file" >&2
    $ftpagent $netfile 2>&1
    if [ ! -f $file ]; then
      echo "==> ERROR: Failed to download $file" >&2
      echo "==> Aborting..." >&2
      exit 1
    fi
    mkdir -p /var/cache/pacman/src && cp $file /var/cache/pacman/src
  fi
  case $file in
	    *.tar.gz|*.tar.Z|*.tgz)
    cmd="tar --use-compress-program=gzip -xf $file" ;;
	    *.tar.bz2)
    cmd="tar --use-compress-program=bzip2 -xf $file" ;;
	    *.zip)
    cmd="unzip -qq $file" ;;
	    *)
    cmd="cp ../$file ." ;;
	esac
	echo "==> $cmd" >&2
	$cmd
done

# build
echo "==> Building Package..." >&2
build 2>&1
if [ $? -gt 0 ]; then
  echo "==> Build Failed.  Aborting..." >&2
  exit 2
fi

# write the .PKGINFO file
echo "==> Generating .PKGINFO file..." >&2
cd $startdir/pkg
echo "# Generated by makepkg $myver" >.PKGINFO
echo -n "# " >>.PKGINFO
date >>.PKGINFO
echo "pkgname = $pkgname" >>.PKGINFO
echo "pkgver = $pkgver-$pkgrel" >>.PKGINFO
for bakfile in "${backup[@]}"; do
  echo "backup = $bakfile" >>.PKGINFO
done
if [ "$install" != "" ]; then
  cat $startdir/$install  | egrep '^[^$]' | sed 's/^/install = /g' >>.PKGINFO
fi
if [ "$remove" != "" ]; then
  cat $startdir/$remove  | egrep '^[^$]' | sed 's/^/remove = /g' >>.PKGINFO
fi

# remove info/doc files
cd $startdir
rm -rf pkg/usr/info pkg/usr/share/info
rm -rf pkg/usr/doc pkg/usr/share/doc

# strip binaries
cd $startdir
echo "==> Stripping debugging symbols from libraries..." >&2
find pkg/{,usr,usr/local}/lib -type f \
   -exec /usr/bin/strip --strip-debug '{}' ';' 2>&1
echo "==> Stripping symbols from binaries..." >&2
find pkg/{,usr,usr/local}/{bin,sbin} -type f \
   -exec /usr/bin/strip '{}' ';' 2>&1

# tar it up
echo "==> Compressing package..." >&2
cd $startdir/pkg
tar czvf $startdir/$pkgname-$pkgver-$pkgrel.pkg.tar.gz .PKGINFO * >../filelist

cd $startdir
d=`date`
echo "==> Finished  ($d)" >&2
exit 0
