#!/bin/bash
#Automatically Created by buildpkg 1.1.0
#Maintainer: Pedro Pinto (pnboy~AT~pinguix.com)

######Begin Redundant Code######################################
check_for_root() {
	if [ "$UID" != "0" ]; then
		echo "You need to be root"
		exit 1
	fi
}

clean_old_builds () {
	for COMPLETED in $package.{tgz,md5,dep} src pkg; do
		if [ -e $COMPLETED ]; then
			rm -rf $COMPLETED
		fi
	done
}

set_pre_permissions() {
	cd $startdir/src
	find . -perm 664 -exec chmod 644 {} \;
	find . -perm 600 -exec chmod 644 {} \;
	find . -perm 444 -exec chmod 644 {} \;
	find . -perm 400 -exec chmod 644 {} \;
	find . -perm 440 -exec chmod 644 {} \;
	find . -perm 777 -exec chmod 755 {} \;
	find . -perm 775 -exec chmod 755 {} \;
	find . -perm 511 -exec chmod 755 {} \;
	find . -perm 711 -exec chmod 755 {} \;
	find . -perm 555 -exec chmod 755 {} \;
}


check_for_menu_complaince() {
	if [ -d "$startdir/pkg/usr/share/applications" ]; then
		cd $startdir/pkg/usr/share/applications
		for desktop in *.desktop; do
			. $desktop 2>/dev/null
			if [[ "$Icon" ]]; then
				if [ ! "$Icon" == "${Icon%.*}" ]; then
					sed '/Icon/s/\..*$//' -i $desktop
					. $desktop 2>/dev/null
				fi
				if [ -d "$startdir/pkg/usr/share/pixmaps" ]; then
					cd $startdir/pkg/usr/share/pixmaps
					for pixmaps in *; do
				 		if [ "${pixmaps%.*}" == "$Icon" ]; then
				 			DIR=$(file $pixmaps | awk -F, '{print $2}' | sed 's/ //g')
							mkdir -p $startdir/pkg/usr/share/icons/hicolor/$DIR/apps
							mv $pixmaps $startdir/pkg/usr/share/icons/hicolor/$DIR/apps
							if [[ ! "$(ls $startdir/pkg/usr/share/pixmaps)" ]]; then
								rm -rf $startdir/pkg/usr/share/pixmaps
							fi
						fi
					done
				fi
				if [ -d "$startdir/pkg/usr/share/icons" ]; then
					cd $startdir/pkg/usr/share/icons
					for icons in *; do
				 		if [ "${icons%.*}" == "$Icon" ]; then
				 			DIR=$(file $icons | awk -F, '{print $2}' | sed 's/ //g')
							mkdir -p $startdir/pkg/usr/share/icons/hicolor/$DIR/apps
							mv $icons $startdir/pkg/usr/share/icons/hicolor/$DIR/apps
						fi
					done
				fi
			fi
		done
	fi
}

strip_binaries() {
	cd $startdir/pkg
	find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
	xargs strip --strip-unneeded 2> /dev/null
	find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
	xargs strip --strip-unneeded 2> /dev/null
}

gzip_man_and_info_pages() {
	for DOCS in man info; do
		if [ -d "$startdir/pkg/usr/share/$DOCS" ]; then
			mv $startdir/pkg/usr/share/$DOCS $startdir/pkg/usr/$DOCS
			if [[ ! "$(ls $startdir/pkg/usr/share)" ]]; then
				rm -rf $startdir/pkg/usr/share
			fi
		fi
		if [ -d "$startdir/pkg/usr/$DOCS" ]; then
			find $startdir/pkg/usr/$DOCS -type f -exec gzip -9 '{}' \;
		fi
	done
}

set_post_permissions() {
	if [ -d "$startdir/pkg/usr/bin" ]; then
		chown -R root:root $startdir/pkg/usr/bin
	fi
}

copy_build_script() {
	mkdir -p $startdir/pkg/usr/src/$pkgname-$pkgver
	cp $startdir/build-$pkgname.sh $startdir/pkg/usr/src/$pkgname-$pkgver/build-$pkgname.sh
}

create_package() {
	cd $startdir/pkg
	makepkg -l y -c n $startdir/$package.tgz
	cd $startdir
	md5sum $package.tgz > $startdir/$package.md5
}

#########End Redundant Code#####################################
#########Begin Non Redundant Code##############################

prepare_directory() {
	mkdir $startdir/src
	for SOURCES in http://d4x.krasu.ru/files/d4x-2.5.7.1.tar.bz2; do
		if [ ! -f "$(basename $SOURCES)" ]; then
			protocol=$(echo $SOURCES | sed 's|:.*||')
			if [ "$protocol" = "http" -o "$protocol" = "https" -o "$protocol" = "ftp" ]; then
				echo -e "\nDownloading $(basename $SOURCES)\n"
				wget $SOURCES
				if [ ! "$?" == "0" ]; then
					echo "Download failed"
					exit 2
				fi 
			else
				echo "$SOURCES does not appear to be a url nor is it in the directory"
				exit 2
			fi
		fi
		cp -R $(basename $SOURCES) $startdir/src
	done
	mkdir $startdir/pkg
}

extract_source() {
	cd $startdir/src
	if [[ "$(ls $startdir/src)" ]]; then	
		for FILES in *; do
			if [[ "$(echo $FILES | grep tar)" ]]; then
				tar xf $FILES
			fi
		done
	else
		echo "no files in the src directory $startdir/src"
		exit 2
	fi
}

build() {
        cd $startdir/src/$pkgname-$pkgver
        ./configure --prefix=/usr --localstatedir=/var --sysconfdir=/etc --disable-glibtest --disable-gtktest --disable-debug --mandir=/usr/man
        make || return 1
        make install DESTDIR=$startdir/pkg
        # dekstop file
        mkdir -p $startdir/pkg/usr/share/applications/
        cp share/nt.desktop $startdir/pkg/usr/share/applications/d4x.desktop
        sed -i -e 's/Icon=nt.png/Icon=d4x/g' $startdir/pkg/usr/share/applications/d4x.desktop
        echo "Categories=Application;Network;" >> $startdir/pkg/usr/share/applications/d4x.desktop
        # icon
        mkdir -p $startdir/pkg/usr/share/icons/hicolor/48x48/apps/
        cp share/nt.png $startdir/pkg/usr/share/icons/hicolor/48x48/apps/d4x.png
        # duplicate docs
        rm -rf $startdir/pkg/usr/share/d4x/{FAQ*,INSTALL*,README*,LICENSE,AUTHORS,TROUBLES}
        mkdir -p $startdir/pkg/usr/doc/$pkgname-$pkgver
        cp -a DOC/FAQ* DOC/LICENSE DOC/README* DOC/TROUBLES DOC/THANKS $startdir/pkg/usr/doc/$pkgname-$pkgver

}

create_slackdesc() {
mkdir $startdir/pkg/install
cat <<"EODESC" >$startdir/pkg/install/slack-desc
d4x:  (Downloader for X)
d4x: 
d4x: d4x lets you download files from the Internet using the FTP and the
d4x: HTTP protocols. It has the following main features:
d4x: multithreaded design, convient user-friendly interface, automatic
d4x: resuming after connection breaks, multiple simultaneous downloads,
d4x: recursive FTP and HTTP downloading, proxy support, supports for
d4x: traffic limitation, FTP search, drag'n'drop support, and many more.
d4x: 
d4x: 
d4x: 
EODESC
}

setup_doinst() {
cat <<"EODOINST" >>$startdir/pkg/install/doinst.sh
doinst() {
if [ -x usr/bin/update-desktop-database ]; then
  usr/bin/update-desktop-database usr/share/applications >/dev/null 2>&1
fi
}
doinst
EODOINST
}
copy_docs() {
	for stuff in ABOUT-NLS AUTHORS COPYING ChangeLog* NEWS PLANS TODO; do
		if [ ! -d "$startdir/pkg/usr/doc/$pkgname-$pkgver" ]; then
			mkdir -p $startdir/pkg/usr/doc/$pkgname-$pkgver
		fi
		find $startdir/src -type f -iname "$stuff" -exec cp -R '{}' $startdir/pkg/usr/doc/$pkgname-$pkgver \;
	done
}
detect_dependencies() {
	echo "Detecting dependencies..."
	depends=$(requiredbuilder -z $startdir/$package.tgz)
	
	depstring=$depends
	unset FIRST
	for deps in $(echo "${depstring}" | sed 's/,/\n/g' | sort); do
		if [[ ! "$FIRST" ]]; then
			finaldeps=$deps,
			FIRST='1'
			continue
		fi
		finaldeps=${finaldeps}${deps},
	done
	echo "$finaldeps" | sed -e 's/^,//' -e 's/,$//' > $startdir/$package.dep
}

####End Non Redundant Code############################

#Variables

startdir=$(pwd)
SRC=$startdir/src
PKG=$startdir/pkg

pkgname=d4x
pkgver=2.5.7.1
pkgrel=1
zenver=48
arch=i486
package=$pkgname-$pkgver-$arch-$zenver.$pkgrel
export CFLAGS="-O2 -march=$arch -mtune=i686"
export CXXFLAGS="-O2 -march=$arch -mtune=i686"

#Execution

check_for_root
clean_old_builds
prepare_directory
extract_source
set_pre_permissions
build
if [ ! "$?" = "0" ]; then
	echo "build() failed."
	exit 2
fi
create_slackdesc
setup_doinst
copy_docs
check_for_menu_complaince
strip_binaries
gzip_man_and_info_pages
set_post_permissions
copy_build_script
create_package
detect_dependencies
echo "Package has been built."
