# samba
# Builds from https://github.com/puppylinux-woof-CE/petbuilds

. ../func
. ../build.conf

URL=http://samba.org/samba/ftp/stable
PKG=samba
#VER=4.1.20
VER=4.14.7
COMP=tar.gz
DESC="share files with M.S. Windows"
DEPS=
CAT=Network
DESKTOP=samba.desktop
BUILD=
CWD=$(pwd)
[ -z "$MWD" ] && MWD=$(dirname $CWD)	
[ -z "$BUILD" ] && BUILD=$DEF_BUILD
#[ -f "$DESKTOP" ] || DESKTOP=not
#[ -z "$DEPS" ] && DEPS=not	

ARCH=$(uname -m)
case $ARCH in
 *64) 	LIBDIR=$LIBCONF64 ;;
 arm*)	LIBDIR=$LIBCONFarm;;
 *) 	LIBDIR=$LIBCONF32 ;;
esac

build() {
	cd ${PKG}-${VER}
	./configure --prefix=/usr \
				--localstatedir=/var \
				--sysconfdir=/etc \
				--libdir=/usr/${LIBDIR} \
				--enable-fhs \
				--includedir=/usr/include \
				--bindir=/usr/bin \
				--sbindir=/usr/sbin \
				--with-configdir=/etc/samba \
				--with-piddir=/var/run \
				--with-privatedir=/etc/samba/private \
				--with-privatelibdir=/usr/${LIBDIR} \
				--with-modulesdir=/usr/${LIBDIR} \
				--with-lockdir=/var/cache/samba \
				--with-logfilebase=/var/log/samba \
				--enable-cups \
				--with-acl-support \
				--with-automount \
				--with-quotas \
				--with-syslog \
				--with-utmp \
				--without-winbind \
				--with-ldap \
				--without-pam \
				--without-ad-dc \
				--without-ads
				
	[ "$?" -eq 0 ] || exit
	make $MKFLG
	[ "$?" -eq 0 ] || exit
	make DESTDIR=$CWD/${PKG}-install install
	cd -
}
	
package() {
	cp -a --remove-destination ${PKG}-template/* ${PKG}-install
	# add this recipe
	install -d -m 0755 ./${PKG}-install/usr/src/dev
	cat ${PKG}.petbuild > ./${PKG}-install/usr/src/dev/${PKG}-build-recipe
	# split
	get_files ${PKG}-install ${PKG}-${VER}-${ARCH}_${BUILD}
	(cd ${PKG}-${VER}; $MWD/split.sh ../${PKG}-install $BUILD)
	if [ -f "$DESKTOP" ];then
		install -d -m 0755 ./${PKG}-${VER}-${ARCH}_${BUILD}/usr/share/applications
		cat $DESKTOP > ./${PKG}-${VER}-${ARCH}_${BUILD}/usr/share/applications/$DESKTOP
	fi
	# delete any icon cache or library cache
	find ./${PKG}-${VER}-${ARCH}_${BUILD} -type f -name '*cache' -delete
	[ -f ./pinstall.sh ] && install -m 0755 pinstall.sh ./${PKG}-${VER}-${ARCH}_${BUILD}/
	[ -f ./puninstall.sh ] && install -m 0755 puninstall.sh ./${PKG}-${VER}-${ARCH}_${BUILD}/
	# extra procesing for samba
	echo "extra procesing for samba"
	find ${PKG}-${VER}-${ARCH}_${BUILD} | xargs file | grep -e "executable" -e "shared object" \
		| grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	mkdir -p ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/share/
	mkdir -p ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/bin/
	mkdir -p ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/sbin/
	mkdir -p ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/${LIBDIR}/
	mv ${PKG}-${VER}-${ARCH}_${BUILD}/usr/share/perl5 ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/share/
	mv ${PKG}-${VER}-${ARCH}_${BUILD}/usr/bin/samba-tool ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/bin/
	mv ${PKG}-${VER}-${ARCH}_${BUILD}/usr/bin/pidl ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/bin/
	mv ${PKG}-${VER}-${ARCH}_${BUILD}/usr/${LIBDIR}/python2.7 ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/${LIBDIR}/
	mv ${PKG}-${VER}-${ARCH}_${BUILD}/usr/sbin/samba_* ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/sbin/
	for p in $(ls|grep "\-${ARCH}_${BUILD}"|grep -v "files$") ; do
		case $p in
			*_DEV*) DESC="$PKG development"; DEPS=+${PKG} ;;
			*_DOC*) DESC="$PKG documentation"; DEPS=+${PKG} ;;
			*_NLS*) DESC="$PKG locales"; DEPS=+${PKG} ;;
		esac	
		echo "packaging $p"	
		dir2pet -x -s -w="$DESC" -d="$DEPS" -c="$CAT" -p=${p} 2>&1 >/dev/null
		rm -r $p
		mv ${p}.pet ../0pets_out
	done
	rm -r ${PKG}-install
	rm -r ${PKG}-${VER}
	mv -f *.files ../0logs
	echo "done!"
}

# main
retrieve "${PKG}-${VER}.${COMP} --no-check-certificate"
extract ${PKG}-${VER}.${COMP}
build
package	
