#!/bin/bash
#
# source: http://gftp.seul.org/gftp-2.0.19.tar.bz2
#
# maintainer: gmg (g.monpontet@gmail.com)
#

soft="gftp"
softversion="2.0.19"
arch="i486"
cpu="i686"
packageversion="66.1"

buildir=$(pwd)
srcpkg="$buildir/$soft-$softversion.tar.bz2"
src="$buildir/$soft-$softversion"
package="$soft-$softversion-$arch-$packageversion"
dest="$buildir/$package"

# -----------------Extract sources and get ready to build

cd $buildir
rm -rf $src
tar -xvjf $srcpkg
rm -f $dest.tgz
rm -rf $dest

cd $src

# added mandriva patch
cat $buildir/gftp-2.0.19-fix-crash.patch | patch -p0 --verbose
cat $buildir/gftp-2.0.19-datetime-fixedsort.patch | patch -p0 --verbose

chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --mandir=/usr/man

make -j2
make install DESTDIR=$dest

find $dest | xargs file | grep -e "shared object" -e "executable" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# gftp icon fix for .txz

rm $dest/usr/share/gftp/gftprc
zcat $buildir/gftprc.gz > $dest/usr/share/gftp/gftprc

# Documentation

mkdir -p $dest/usr/doc/$soft-$softversion
cp -a \
  AUTHORS ChangeLog COPYING INSTALL README* THANKS TODO \
  $dest/usr/doc/$soft-$softversion

# Put symlinks to the themable icons in place
mkdir -p $dest/usr/share/icons/hicolor/{scalable,16x16,22x22,24x24,32x32,48x48}/apps
for i in 16x16 22x22 24x24 32x32 48x48 ; do
  ln -s /usr/share/gftp/gftp-${i}.png \
    $dest/usr/share/icons/hicolor/${i}/apps/gftp.png
done

cd $dest/usr/share/icons/hicolor/scalable/apps
ln -s /usr/share/gftp/gftp-scalable.svg gftp.svg

# Compress Man Pages

gzip -9 $dest/usr/man/man?/*.?

# make the .desktop file
rm $dest/usr/share/applications/$soft.desktop
cat << "EOF" > $dest/usr/share/applications/$soft.desktop 
[Desktop Entry]
Name=gFTP
Name[br]=gFTP
Name[da]=gFTP
Name[de]=gFTP
Name[es]=gFTP
Name[fr]=gFTP
Name[ja]=gFTP
Name[ko]=gFTP
Name[nl]=gFTP
Name[pl]=gFTP
Name[pt_BR]=gFTP
Name[zh_TW.Big5]=gFTP
GenericName=File Transfer Protocol Utility
Comment=Multithreaded FTP Client
Comment[br]=Cliente de ftp multithreaded
Comment[da]=Flertrådet ftpklient
Comment[de]=Multithreaded FTP Programm
Comment[es]=Cliente FTP multihilo
Comment[fr]=Client FTP multithread
Comment[ja]=¥Þ¥ë¥Á¥¹¥ì¥Ã¥ÉFTP¥¯¥é¥¤¥¢¥ó¥È
Comment[ko]=´ÙÁß¾²·¹µå FTP ÇÁ·Î±×·¥
Comment[nl]=Multithreaded ftp programma
Comment[pl]=Wielozadaniowy klient FTP
Comment[pt_BR]=Cliente FTP multi-tarefa
Comment[zh_TW.Big5]=¦h½uºü  ftp client
Exec=gftp
Icon=gftp
Terminal=false
Type=Application
Categories=GTK;GNOME;Network;
X-GNOME-Bugzilla-Bugzilla=GNOME
X-GNOME-Bugzilla-Product=gftp
EOF

# Description file

mkdir -p $dest/install
cat << "EODESC" > $dest/install/slack-desc
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

    |-----handy-ruler----------------------------------------------------------|
gftp: gftp - free multithreaded file transfer client for *NIX based machines
gftp:
gftp: gFTP is a free multithreaded file transfer client for *NIX based machines
gftp: running X11R6 or later. It has text and GTK+ 1.2/2.x interfaces and
gftp: supports the FTP FTPS (control connection only),HTTP, HTTPS, FSP, and SSH
gftp: protocols and has FTP and HTTP proxy server support. It supports UNIX,
gftp: EPLF, Novell, MacOS, VMS, MVS,and NT (DOS) style directory listings,
gftp: and has a bookmarks menu that allows you to quickly connect to remote
gftp: sites. The code is fully internationalized and there are currently 45
gftp: translations available.
gftp:
EODESC

mkdir -p $dest/usr/src/$soft-$softversion
cp $buildir/build-$soft.sh $dest/usr/src/$soft-$softversion

# Packaging

cd $dest
makepkg -l y -c n $dest.txz

cd $buildir
md5sum $package.txz > $package.md5
