#!/bin/bash

# git.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>

eval `perl '-V:installsitelib'` 
PERLDIR=$installsitelib/$ARCH-linux-thread-multi/auto

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

eval $(perl '-V:installvendorlib')
PERLDIR=$installvendorlib/$ARCH-linux-gnueabi-thread-multi/auto

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Copy docs:
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 )
( cd $PKG/usr/doc/git-$VERSION && find . -name ".git*" -exec rm -r "{}" \; )

make $NUMJOBS \
  prefix=/usr \
  mandir=/usr/man \
  CFLAGS="$SLKCFLAGS" \
  INSTALLDIRS=vendor \
  ASCIIDOC8=YesPlease \
  all doc || exit 1

make \
  prefix=/usr \
  mandir=/usr/man \
  "CFLAGS=$SLKCFLAGS" \
  INSTALLDIRS=vendor \
  ASCIIDOC8=YesPlease \
  install \
  install-doc \
  DESTDIR=$PKG || exit 1

mv $PKG/usr/share/man/man3 $PKG/usr/man
rmdir $PKG/usr/share/man

# This removes our DESTDIR from the packlist filenames, to keep perl's
# internal inventories consistent and correct.
find $PKG -name .packlist | while read plist ; do
  sed -e "s%/share/man%/man%g" \
      -e "s%$PKG%%g" \
      -e "s%\.1$%\.1\.gz%g" \
      -e "s%\.2$%\.2\.gz%g" \
      -e "s%\.3$%\.3\.gz%g" \
      -e "s%\.3pm$%\.3pm\.gz%g" \
      -e "s%\.4$%\.4\.gz%g" \
      -e "s%\.5$%\.5\.gz%g" \
      -e "s%\.6$%\.6\.gz%g" \
      -e "s%\.7$%\.7\.gz%g" \
      -e "s%\.8$%\.8\.gz%g" \
      ${plist} > ${plist}.new
      mv -f ${plist}.new ${plist}
done

# This is junk:
eval $(perl '-V:privlib')
( cd $PKG$(dirname $privlib) && rm -rf 5.* )

#
# 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
)

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
