#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-git
VERSION=1.7.0.4
ARCH=alpha
BUILD=1
TARGET==$ARCH-alphaslack-linux
PERLDIR="usr/lib/perl5/site_perl/5.10.0/alpha-alphaslack-linux-thread-multi/auto"

rm -rf $PKG
mkdir -p $PKG
( cd $TMP
rm -rf git-$VERSION
tar xjf $CWD/git-$VERSION.tar.bz2
cd git-$VERSION

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -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 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

#mkdir -p $PKG/usr/doc/git-$VERSION
#cp -a COPYING INSTALL README Documentation contrib $PKG/usr/doc/git-$VERSION
#( cd $PKG/usr/doc/git-$VERSION/Documentation ; rm *.1 *.3 *.7 )
./configure \
 --prefix=/usr \
 --docdir=/usr/doc \
 --mandir=/usr/man \
 --build=$TARGET || exit 1

make all || exit 1
make install DESTDIR=$PKG || exit 1

mv $PKG/usr/share/man $PKG/usr
rm -rf $PKG/usr/share/man

# Compress and if needed symlink the man pages:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/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

( cd $PKG
 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
)

# Since Stuart Winter hates hard links.  ;-)
# Switch a hard link with a soft link:
( cd $PKG/usr/bin
  find . -links +1 -not -name git | while read gitfile ; do
    if [ git -ef $gitfile ]; then
      rm -vf $gitfile
      ln -vfs git $gitfile
    fi
  done
)

# This removes our DESTDIR from the packlist filenames, to keep perl's 
# internal inventories consistent and correct.
sed -e "s#$PKG##" $PKG/$PERLDIR/Git/.packlist > $PKG/$PERLDIR/Git/.packlist.new
mv -f $PKG/$PERLDIR/Git/.packlist.new $PKG/$PERLDIR/Git/.packlist

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $PKG
makepkg -l y -c n $TMP/git-$VERSION-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/git.build.log
