#!/bin/bash

# Slackware build script for rename.pl

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# This replaces the former perl-rename from the SBo repo. The
# maintainer of perl-rename asked me to take over the build, and I
# decided I like the name "rename.pl" better. Also, the old one was in
# the perl/ category, implying it was a perl library. It's not. Just
# because an application is *written* in perl, doesn't mean it should
# be in the perl/ category (same goes for python/, people).

# This is a completely new SlackBuild (shares no code with
# perl-rename.SlackBuild).

# This is the "classic" rename, that used to ship with the perl source
# ages ago. It's also the same "rename" as Debian used to ship. I'm
# aware of the existence of https://metacpan.org/release/File-Rename
# but it's not the same: it's missing a lot of options (--no-stdin,
# -i, -l, -v, and all the options relating to backups). It also
# added some new options that do look useful (-0, -d, -e, -E), but it
# doesn't look like a good tradeoff to me.

# There's also a completely different rename.pl here:
# https://github.com/kugland/rename.pl
# ...which AUR ships at version 2.0.0, so repology keeps telling me
# this is outdated. Wish there were a way to fix that.

# Since the name "rename" conflicts with util-linux, I've named this
# rename.pl, and gone to some trouble to make sure its documentation
# refers to rename.pl consistently (particularly the example commands,
# which wouldn't work if you copy/pasted them as-is).

# This version of rename is still actively maintained, as of 20230226.

# 20230905 bkw: updated for v1.14, tweak man page, add shell completions.
# 20221228 bkw: updated for v1.12, fix PKGTYPE assignment.

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

PRGNAM=rename.pl
VERSION=${VERSION:-1.14}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

SRCNAM=rename

ARCH=noarch

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}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
cd $SRCNAM-$VERSION
chown -R root:root .
chmod 644 bin/* *
chmod 755 . bin

# rename => rename.pl in embedded docs. Tricky because the word
# "rename" is also used as a normal verb and shouldn't be changed in
# that case. Also fix typos in embedded docs, and clean up the
# shell completion.
sed -i \
  -e '/^rename - renames/s,rename,rename.pl,' \
  -e '/^  *rename /s,rename,rename.pl,' \
  -e 's,rename prompts,rename.pl prompts,g' \
  -e 's,<rename>,<rename.pl>,g' \
  -e '/make sure rename doesn.t/s,rename,B<rename.pl>,' \
  -e 's,Us it,Use it,' \
  -e 's,mv prompts,<rename.pl> prompts,' \
  -e 's,fo details,for details,' \
  -e 's,insted,instead,' \
  -e 's,wheter,whether,' \
  -e 's,reame,rename,' \
  -e 's,<-no-stdin>,<--no-stdin>,' \
  -e 's,I<rename\.pl>,B<rename.pl>,' \
  -e 's, <rename\.pl>, B<rename.pl>,' \
  -e 's,destionation,destination,' \
  -e 's,_comp_rename,_comp_rename_pl,' \
  -e '/comp/s,rename;,rename.pl;,' \
  bin/$SRCNAM.PL

perl Makefile.PL
make

# Generated man page still calls itself RENAME. Also, fix the
# version info.
sed -i -e '/^\./s,\<RENAME\>,&.PL,' \
       -e '/^\.TH/s,"perl v5[^"]*","rename.pl-'$VERSION'",' \
       blib/man1/$SRCNAM.1

# This really is just a perl script and a man page. 'make install'
# creates lib(64) dirs and a perllocal.pod that we don't need.
mkdir -p $PKG/usr/bin $PKG/usr/man/man1
install -m0755 bin/$SRCNAM $PKG/usr/bin/$PRGNAM
gzip -9c < blib/man1/$SRCNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

# 20230905 bkw: go ahead and include shell completions in the pkg.
# Thanks to TommyC on libera IRC for assistance with the zsh stuff.
ZSHDIR=$PKG/usr/share/zsh/site-functions
BASHDIR=$PKG/usr/share/bash-completion/completions
mkdir -p $ZSHDIR $BASHDIR

echo "#compdef $PRGNAM" > $ZSHDIR/_$PRGNAM
./bin/$SRCNAM --shell-completion=zsh >> $ZSHDIR/_$PRGNAM

mkdir -p $PKG/usr/share/bash-completion/completions
./bin/$SRCNAM --shell-completion=bash > $BASHDIR/$PRGNAM

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a Changes README.md $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$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
