#!/bin/bash
# Copyright (c) 2000-2015 Synology Inc. All rights reserved.

echo "making necessary directories..."

env PRIVATELIBDIR=/usr/lib/samba \
	DESTDIR=${TmpInstDir} make install ${MAKE_FLAGS}

SZD_SDKHOOK="${TmpInstDir}/usr/libexec"

## Install upstart script
install -D -m644 synosmb/samba.logrotate ${TmpInstDir}/etc/logrotate.d/samba
install -D -m644 synosmb/samba.servicecfg ${TmpInstDir}/usr/syno/etc/synoservice.d/samba.cfg
install -D -m644 synosmb/winbindd.servicecfg ${TmpInstDir}/usr/syno/etc/synoservice.d/winbindd.cfg
install -D -m644 synosmb/nmbd.servicecfg ${TmpInstDir}/usr/syno/etc/synoservice.d/nmbd.cfg
install -D -m755 synosmb/smbd.upstart ${TmpInstDir}/etc/init/smbd.conf
install -D -m755 synosmb/nmbd.upstart ${TmpInstDir}/etc/init/nmbd.conf
install -D -m755 synosmb/winbindd.upstart ${TmpInstDir}/etc/init/winbindd.conf
install -D -m755 synosmb/S80samba.sh ${TmpInstDir}/usr/syno/etc/rc.sysv/S80samba.sh
install -D -m644 synosmb/samba.schema ${TmpInstDir}/usr/syno/etc/openldap/schema/samba.schema
install -D -m644 synosmb/samba.ldif ${TmpInstDir}/usr/syno/etc/openldap/schema/samba.ldif
install -D -m644 synosmb/samba.pam ${TmpInstDir}/etc/pam.d/samba
install -D -m555 synosmb/WinbinddVolUnmountHook.sh ${TmpInstDir}/usr/libexec/volume/unmount/WinbinddVolUnmountHook.sh
install -D -m555 synosmb/sambaUserDeleteHook.sh ${TmpInstDir}/usr/libexec/user_delete/sambaUserDeleteHook.sh

## Install sdk hooks
install -D -m755 synosmb/samba_hostname_sensitive.sh ${SZD_SDKHOOK}/net/hostname_change/samba_hostname_sensitive.sh
install -D -m755 synosmb/samba_mtu_sensitive.sh ${SZD_SDKHOOK}/net/mtu_change/samba_mtu_sensitive.sh
install -D -m755 synosmb/samba_ip_sensitive.sh ${SZD_SDKHOOK}/net/ipv4_change/samba_ip_sensitive.sh
install -D -m755 synosmb/samba_ip_sensitive.sh ${SZD_SDKHOOK}/net/ipv6_change/samba_ip_sensitive.sh
install -D -m755 synosmb/samba_link_sensitive.sh ${SZD_SDKHOOK}/net/if_link_up/samba_link_sensitive.sh
install -D -m755 synosmb/samba_link_sensitive.sh ${SZD_SDKHOOK}/net/if_link_down/samba_link_sensitive.sh

## Install debug.dat whitelist and collector
install -D -m644 synosmb/debug_whitelist ${TmpInstDir}/usr/syno/etc/logcollector/whitelist/samba-4.x
install -D -m700 synosmb/debug_collector.sh ${TmpInstDir}/usr/syno/bin/logcollector/samba_collector.sh

mkdir -p ${TmpInstDir}/etc/security
install -c -m644 examples/pam_winbind/pam_winbind.conf ${TmpInstDir}/etc/security/pam_winbind.conf

rm ${TmpInstDir}/usr/include -rf
rm ${TmpInstDir}/usr/share/{man,perl5} -rf
rm ${TmpInstDir}/usr/lib/pkgconfig -rf
rm ${TmpInstDir}/var/run -rf

if [ "x$NOSTRIP" != "xNOSTRIP" ]; then
	#remove unuse part
	rm ${TmpInstDir}/tmp/ -rf

	rm ${TmpInstDir}/usr/lib/python2.7/site-packages/samba/external/testtools -rf
	rm ${TmpInstDir}/usr/lib/python2.7/site-packages/samba/tests -rf
	rm ${TmpInstDir}/usr/lib/samba/libdlz-bind9-for-torture-samba4.so -f
	rm ${TmpInstDir}/usr/lib/samba/libtorture-samba4.so -f
	rm ${TmpInstDir}/usr/bin/smbtorture -f

	find ${TmpInstDir}/ -name '*.so*' | xargs ${STRIP}
	allFiles=`find ${TmpInstDir}/usr -type f | xargs echo`
	for files in $allFiles; do
		elfFile=`file $files | grep -i elf`;
		if [ -n "$elfFile" ]; then
			${STRIP} $files
		fi
	done


fi

# vim:filetype=sh
