#!/bin/bash
# ProjectUrl: http://aspell.net/
# Source Loc: ftp://ftp.gnu.org/gnu/aspell/dict
# Maintainer: til <rob DOT til DOT freedman AT googlemail DOT com>
# Depends on: aspell
# Revision.2: March 05, 2007 fixed email & deps
# Revision.3: June  24, 2007 small doc fixes, zenbuild 48.1

FTP='ftp://ftp.gnu.org/gnu/aspell/dict'
ASPELL_VERSION='0.60.5'

DICT_FILE='aspell-gl-0.50-0.tar.bz2'

ARCH='noarch'
BUILD='48.1'
MYSELF=$0

# build root
CWD=${CWD:-$(pwd)}
SRC=${SRC:-$CWD/src}
TMP=${TMP:-$CWD/build}
DICT=${DICT:-$CWD/dict}
mkdir -p $TMP $DICT

# process source package dict.tar.bz2
pkgdir=${DICT_FILE%%.tar.bz2}
#XLS=${pkgdir%-*-*};
#NLS=${XLS#aspell?-};
#VER=${pkgdir##${XLS}-};
  XLS=$pkgdir
  NLS=${XLS#aspell6-*};
  NLS=${NLS#aspell5-*};
  NLS=${NLS#aspell-*};
  VER=${NLS#*-};
  NLS=${NLS%-${VER}};
  
# nasty cludge for 'de-alt'
  NLSDIR=$NLS
  if [[ "x$NLS" = "xde" ]]; then
     XER=${VER%%-*}
     if [[ "x$XER" = "xalt" ]]; then
        NLSDIR="de-alt"
        NLS="de_alt"
        VER=${VER#alt-*};
     fi
  fi
  PRE=${pkgdir%%-*}
  NAME="${PRE}-${NLS}"
  VER=$(echo $VER|tr "_-" ".")

cd $CWD
# download if needed
if [ ! -e $SRC/$DICT_FILE ]; then
   wget -P $SRC -c "$FTP/$NLSDIR/$DICT_FILE" || \
   ( 
     echo "Spellbook not found on server:"
     echo " $FTP/$NLSDIR/$DICT_FILE"
     echo "Please download manually."
   	 exit
   )
fi
# make version sane
#PRE=${pkgdir%%-*}
#NAME=${PRE}-${NLS}
#VER=$(echo $VER|tr "_-" ".")
#
PKG=${TMP}/package-${NAME}
mkdir -p ${PKG}/install
mkdir -p ${PKG}/usr/{src,doc}/${NAME}-${VER}

# build package 
cd $TMP
tar xjf $SRC/$DICT_FILE
cd $pkgdir
   chown -R root.root .
   ./configure
   make || exit 1
   make install DESTDIR=${PKG}
   # package docs
   cp -a README $PKG/usr/doc/${NAME}-${VER}/
     
   # create slackdesc
   SHORT_DESC="$(head -n1 README|cut -d' ' -f4-)"
   
   cat <<EOF >${PKG}/install/slack-desc
$NAME: $NAME $SHORT_DESC
$NAME:
$NAME: GNU Aspell is a Free and Open Source spell checker designed to
$NAME: eventually replace Ispell. It can either be used as a library or as
$NAME: an independent spell checker. 
$NAME:
$NAME: http://aspell.net/ 
$NAME: 
$NAME:
$NAME:
$NAME:
EOF
  
# make tgz 
PACKAGE="${NAME}-${VER}-${ARCH}-${BUILD}"
cd $PKG
cp -a $MYSELF $PKG/usr/src/$NAME-$VER/build-$NAME.sh

chown -R root.root .
find . -perm 711 -exec chmod 755 {} \;
find . -perm 640 -exec chmod 644 {} \;
makepkg -l y -c n ${DICT}/${PACKAGE}.tgz
      
# md5 & deps
cd ${DICT}
md5sum ${PACKAGE}.tgz >${PACKAGE}.md5

# depends on aspell, obviously and ncurses,perl
echo "aspell,ncurses,perl" >${PACKAGE}.dep

#rm -fr $TMP
