#!/bin/bash

# Slackware build script for words 

# Copyright 2026 Frank Honolka <info@snukware.com>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

PRGNAM=words			
VERSION=${VERSION:-2.1}	

BUILD=${BUILD:-1}
TAG=${TAG:-_snuk}		
PKGTYPE=${PKGTYPE:-txz}

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

TMP=${TMP:-/var/cache/snukware/tmp}	
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/var/cache/snukware/a}	

ARCH=noarch

set -e 

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
mkdir -p $PRGNAM/src
cd $PRGNAM/src
src=(
aspell-de-0.50-2.tar.bz2  
aspell6-de-alt-2.1-1.tar.bz2  
aspell6-ca-2.1.5-1.tar.bz2    
aspell6-en-2020.12.07-0.tar.bz2  
aspell6-fi-0.7-0.tar.bz2
aspell-fr-0.50-3.tar.bz2  
aspell6-es-1.11-2.tar.bz2        
aspell6-it-2.2_20050523-0.tar.bz2
)

for i in ${src[@]}; do
	tar xvf $CWD/files/${i} || exit 1
done

find $TMP/$PRGNAM/src -name '*.cwl' -exec cp -a "{}" $TMP/$PRGNAM  \; || exit 1

cd $TMP/$PRGNAM
preunzip *.cwl
for wl in *.wl; do
    iconv --from-code=ISO-8859-1 --to-code=UTF-8 $wl | \
    cut -d '/' -f 1 | LC_ALL=C sort -df > $wl.utf8
done

cat en-common.wl.utf8 en_US* | sort -u | LC_ALL=C sort -df > us-merged || exit 1
cat en-common.wl.utf8 en_GB* | sort -u | LC_ALL=C sort -df > gb-merged || exit 1
cat de-only.wl.utf8   de_*   | sort -u | LC_ALL=C sort -df > de-merged || exit 1

install -Dm644 us-merged "$PKG/usr/share/dict/american-english" || exit 1
install -Dm644 gb-merged "$PKG/usr/share/dict/british-english" || exit 1
install -Dm644 ca-common.wl.utf8 "$PKG/usr/share/dict/catala" || exit 1
install -Dm644 de-merged       "$PKG/usr/share/dict/ngerman" || exit 1
install -Dm644 de-alt.wl.utf8  "$PKG/usr/share/dict/ogerman" || exit 1
install -Dm644 fr-40-only.wl.utf8 "$PKG/usr/share/dict/french" || exit 1
install -Dm644 es.wl.utf8 "$PKG/usr/share/dict/spanish" || exit 1
install -Dm644 fi.wl.utf8 "$PKG/usr/share/dict/finnish" || exit 1
install -Dm644 it.wl.utf8 "$PKG/usr/share/dict/italian" || exit 1

mkdir -p $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
cat $CWD/doinst.sh > $PKG/install/doinst.sh

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