#!/bin/bash

# Slackware build script for beep

# Written by gnubien.
# This submission is considered to be in the public domain.

# Modified and now maintained by B. Watson <urchlay@slackware.uk>.

# In case you live in a country where public domain isn't valid,
# you can take this as being licensed under the WTFPL. See
# http://www.wtfpl.net/txt/copying/ for details.

# Modified by Robby Workman of the SlackBuilds.org project

# 20260704 bkw:
# - take over maintenance.
# - switch to maintained fork on github.
# - install setuid by default (but only for audio group).
# - add instructions on pcspkr module and alsamixer to README.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=beep
VERSION=${VERSION:-1.4.12}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar -xzvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find . ! -type l    -a \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \)

# NO configure file for beep

# We're going to install the beep executable into /bin instead of /usr/bin
# This way, init scripts can use it if you happen to want to customize them
# with some troubleshooting tones.  Thanks to Erik van Tromp (alphageek) for
# the suggestion.

# See the manpage for more information on why, but you will need
# to install beep with the suid bit set in order to have full
# functionality  in all instances.
# 20260704 bkw: install setuid by default, but only runnable by the 'audio'
# group. the "correct" way to do it would be udev rules for the pcspkr module,
# but I'm not faffing with that.
if [ "${SETUID:-yes}" = "yes" ]; then
  PERMS=4750
  GROUP=audio
else
  PERMS=0755
  GROUP=root
fi

# 20260704 bkw: not needed on 15.0, but 86 -Werror on general principles.
sed -i '/-Werror/d' GNUmakefile

mkdir -p $PKG/bin $PKG/usr/man/man1

make CFLAGS="$SLKCFLAGS"
install -s -m $PERMS -o root -g $GROUP beep $PKG/bin
gzip -9 < beep.1 > $PKG/usr/man/man1/beep.1.gz

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
rm -f INSTALL.md PACKAGING.md # binary package won't need these
cp -a COPYING *.md $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
