#!/bin/bash

# aspell-dict.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 17-Jun-2004
#
# Copyright 2008, 2009, 2016, 2018  Patrick J. Volkerding, Sebeka, Minnesota, USA
# 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.

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

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

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

# Change the CWD.  Slackware x86 keeps this in /extra:
#CWD=/extra/source/aspell-word-lists
CWD=${CWD/\/source\/l\/aspell-dict/\/extra\/source\/aspell-word-lists\/}
echo "Slackware x86 src: $CWD"

# We'll stash the many packages in here:
STASH=$TMPBUILD/$PKGNAM-stash
mkdir -p $STASH

# Create individual packages of each dictionary:
# **************************************************************************
# **************************************************************************
# Note: this isn't the normal Slackware ARM method - we DON'T auto update the
# build number here.  In x11 for example we compare the version number to
# the build and reset to 1 if it does not match.
#
# This package does not change often so it's never been a problem, but once
# this package is updated -- build this logic in here.
# The main issue here is knowing _what_ to build
#
# Proposal:
# 1-Extract the list from the ChangeLog from between R2B markers.
# Walk the list: ls ~/slackware64-current/extra/aspell-word-lists/*.t?z
# Determine whether x86 package version matches ARM's from the x86_64 package file.
# If not same, reset build=1 and build. if match, bump build.
# Create an r2b build list script, as for k/
# If there isn't a build list, we'll cruise the source archive list and use
# that as the build list.
# Automatically remove the existing package file.
# Figure out how to handle the base 'aspell' and 'aspell-en' packages in l/ series.
# **************************************************************************
# **************************************************************************
#
( cd $CWD/src
   for file in aspell*bz2 ; do
   # pt_PT-preao requires different package name handling:
   if echo $file | grep -q pt_PT-preao ; then
     NAME=$(echo $file | cut -f 1-3 -d -)-$(echo $file | cut -f 4- -d - | rev | cut -b9- | rev | tr '[\-]' '[_]')
    else
     NAME=$(echo $file | cut -f 1-2 -d -)-$(echo $file | cut -f 3- -d - | rev | cut -b9- | rev | tr '[\-]' '[_]')
   fi
   if [ -s $PORTCWD/build-nums/$(basename $file | cut -f 1,2 -d -) ]; then
     BUILD="$( cat $PORTCWD/build-nums/$(basename $file | cut -f 1,2 -d -) )"
     echo "Found build number: $BUILD"
   else
     BUILD=1
     echo "No build number found: creating new housing file and setting to $BUILD"
     echo 1 > $PORTCWD/build-nums/$( basename $file | cut -f 1,2 -d -)
   fi
    rm -rf $PKG
    mkdir -p $PKG
    cd $TMPBUILD
    tar xf $CWD/src/$file
    cd `basename $file .tar.bz2`
    slackhousekeeping
    ./configure || exit 1
    make || exit 1
    make install DESTDIR=$PKG || exit 1
    # Use a uniform package name.
    NAME=$(echo $NAME | sed 's/aspell5/aspell/' | sed 's/aspell6/aspell/')
    mkdir -p $PKG/usr/doc/${NAME}
    cp -a \
     README* Copyright* COPYING* \
     $PKG/usr/doc/${NAME}
    if [ -d doc ]; then
       # Overwrite from doc/ is fine:
       cp -fav doc/* $PKG/usr/doc/${NAME}
    fi
    mkdir $PKG/install
    # pt_PT-preao requires different package name handling:
    if echo $file | grep -q pt_PT-preao ; then
      SLACKDESC=$(echo $NAME | cut -f 1-3 -d -)
    else
      SLACKDESC=$(echo $NAME | cut -f 1-2 -d -)
    fi
    echo "${SLACKDESC}: ${SLACKDESC}" > $PKG/install/slack-desc
    echo "${SLACKDESC}:" >> $PKG/install/slack-desc
    echo "${SLACKDESC}: $(head -n 1 README)." >> $PKG/install/slack-desc
    echo "${SLACKDESC}:" >> $PKG/install/slack-desc
    echo "${SLACKDESC}:" >> $PKG/install/slack-desc
    echo "${SLACKDESC}:" >> $PKG/install/slack-desc
    echo "${SLACKDESC}:" >> $PKG/install/slack-desc
    echo "${SLACKDESC}:" >> $PKG/install/slack-desc
    echo "${SLACKDESC}:" >> $PKG/install/slack-desc
    echo "${SLACKDESC}:" >> $PKG/install/slack-desc
    echo "${SLACKDESC}:" >> $PKG/install/slack-desc
    cd $PKG
    slackslack
    # for 'aspell-en' package, we should make an exception to set PKGSTORE=l
    # and for others, use the main tree variable and move to /extra
    # check how the 'slackmp' routine handles this.
    makepkg -l y -c n $STASH/${NAME}-$PKGARCH-$BUILD.txz
  done )

# Move the packages into the correct locations:
cd $STASH
pwd
echo "For the time being, you should look at the end of this SlackBuild"
echo "and see where each package should go."
echo "Manually move the new/upgraded/added files in to the correct place"
exit


rm -vf $PKGSTORE/l/aspell-[a-z]*-[0-9]*-$PKGARCH.{t?z,asc,txt}
# The English one lives in the main tree:
mv -fv aspell-en-*.t?z $PKGSTORE/l/
# The rest live in /extra (along with a copy of the aspell-en package that resides
# in l/) :
mkdir -vpm755 $PKGSTORE/../extra/aspell-word-lists/
rm -rf $PKGSTORE/../extra/aspell-word-lists/*
mv -fv *.t?z $PKGSTORE/../extra/aspell-word-lists/
