#!/bin/sh

#. /etc/init.d/tc-functions

#checkroot


TODAY=`date +%Y/%m/%d`
PACKAGE="rage"
VERSION="0.1.4"
MIRROR="https://download.enlightenment.org/rel/apps/$PACKAGE"
DESCRIPTION="a video and audio player written with Enlightenment Foundation Libraries with some extra bells and whistles."
AUTHORS="Carsten Haitzler <raster@rasterman.com>
Eduardo Lima (Etrunko) <eblima@gmail.com>"
HOMEPAGE="https://www.enlightenment.org/about-rage"
LICENSE="Copyright (C) 2014 Carsten Haitzler and various contributors"
ME="jls_legalize (jlslegalize@gmail.com"
DEPS="libelementary"
DEVDEPS="libelementary-dev"
BUILDDEPS="build-essential \
	 wget"
TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}-${VERSION}.tar.xz"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE}-${VERSION}"
TCEDIR=/etc/sysconfig/tcedir
CURRENT="${TODAY} First version $VERSION"

clean(){
	rm -rf ${THISDIR}/${PACKAGE}
}
environment(){
	for each in ${DEVDEPS} ${BUILDDEPS}; do
		sce-load ${each}
	done
	[ -d "${PKGDIR}" ] && rm -rf ${PKGDIR}
	mkdir -p ${PKGDIR}/tmp
	cd ${PKGDIR}
	wget --no-check-certificate ${MIRROR}/${SRC} 
	
	mkdir ${SRCDIR}
	xz -d ${SRC} 
	/bb/tar -xf $PACKAGE-${VERSION}.tar
}

buildit(){
	cd ${SRCDIR}
	make clean
	./configure ${FLAGS} || exit 1 
	echo -n "Press Enter key."
	read gagme
	make || exit 1 
	make DESTDIR=${PKGDIR}/tmp install-strip || exit 1 
}

packageit(){
   	cd ${PKGDIR}/tmp
	[ -e ../${PACKAGE}.sce ] && rm ../${PACKAGE}.sce
	mksquashfs . ../${PACKAGE}.sce
	cd ${PKGDIR}
	md5sum ${PACKAGE}.sce > ${PACKAGE}.sce.md5.txt
	size=`du -h ${PACKAGE}.sce | cut -f 1`
	cat <<EOF> ${PACKAGE}.sce.info
Title:		${PACKAGE}
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	
		
Change-log:	
Current:	${CURRENT}
EOF
}
tarit(){	
	cd ${PKGDIR}/tmp
	[ -e ../${PACKAGE}.tar.gz.list ] && rm ../${PACKAGE}.tar.gz.list
	for dir in `ls -A`; do
	   find ${dir} -not -type d | sort >> ../${PACKAGE}.tar.gz.list
	done
	tar	cvzf ../${PACKAGE}.tar.gz *
	cd ${PKGDIR}
	md5sum ${PACKAGE}.tar.gz > ${PACKAGE}.tar.gz.md5.txt
	[ -e ${PACKAGE}.dep ] && rm ${PACKAGE}.dep
   	for each in ${DEPS}; do echo ${each} >> ${PACKAGE}.dep; done
	tar cvf ../${PACKAGE}.tar \
		${PACKAGE}.tar.gz \
		${PACKAGE}.tar.gz.md5.txt \
       		${PACKAGE}.tar.gz.list \
		${PACKAGE}.dep \
		../${PACKAGE}.dcbuild
}

#here we go

#clean
#environment
#buildit
#packageit
tarit