##################### GENERAL ##################### 
GARNAME = backuppc
GARVERSION = 4.3.1
GARELEASE = 4
HOME_URL = https://backuppc.github.io/backuppc/
CATEGORIES = app/backup
MASTER_SITES += https://github.com/backuppc/backuppc/releases/download/$(GARVERSION)/
DISTFILES = BackupPC-$(GARVERSION).tar.gz
PATCHFILES = s6-notification.patch
MAINTAINER = Felipe Sanchez <izto@asic-linux.com.mx>
LICENSE = GPL

DESCRIPTION = High-performance, enterprise-grade system for backing up hosts
define BLURB
endef

##################### PACKAGING #####################


#IZTACI_PACKAGE_PATH =

# Users and groups the binary package will create
# WHEN INSTALLED ON THE DESTINATION SYSTEM
CREATE_GROUPS = backuppc:92
CREATE_USERS  = backuppc:92:92

# This is to be appended to the package's installation script
# Use it to set file owners, permissions, DB initialization, etc.
# Remember any shell variables (Such as $ROOT) have to be escaped
# like this: \$$ROOT


define DOINST_APPEND
echo \"Setting ownership of $(BPC_DIRS) to backuppc:backuppc\"
for bpcdir in $(BPC_DIRS); do find \$$ROOT/\$$bpcdir -maxdepth 2 -print0 | xargs -0 chown backuppc:backuppc; done

echo \"Setting SUID backuppc bit for /usr/lib/apache2/cgi-bin/backuppc/bpcadmin\"
chmod 0755 /usr/lib/apache2/cgi-bin/backuppc/BackupPC_Admin
chown backuppc:backuppc /usr/lib/apache2/cgi-bin/backuppc/bpcadmin
chmod 4755 /usr/lib/apache2/cgi-bin/backuppc/bpcadmin
usermod -a -G backuppc httpd

echo
echo "Enabling httpd mod_cgi and mod_cgid if available..."
echo
for i in cgi_module.load cgi_module.conf cgid_module.load cgid_module.conf; do
   if test -f \$$ROOT/etc/apache2/mods-available/\$$i; then
      rm -rf \$$ROOT/etc/apache2/mods-enabled/\$$i
      ln -sv ../mods-available/\$$i \$$ROOT/etc/apache2/mods-enabled/\$$i
   fi
done

echo
echo \"-----------------------------------------------------------------------------\"
echo s6-rc services installed:
echo
echo \"    backuppc-server\"
echo \"    backuppc-log\"
echo \"    backuppc\"
echo
echo Run \'rc update\' to add them to the live services database.
echo \"-----------------------------------------------------------------------------\"
echo

if [ ! -f /etc/apache2/bpcadmin.users ]; then
   echo
   echo \"-----------------------------------------------------------------------------\"
   echo
   echo \"The default BackupPC configuration uses http basic authentication\"
   echo \"based on the apache authentication file found at:\"
   echo
   echo \"         /etc/apache2/bpcadmin.users\"
   echo
   echo \"The relevant httpd configuration file can be found at:\"
   echo
   echo \"         /etc/apache2/extra/backuppc.conf\"
   echo
   echo \"You might want to add a 'bpc-admin' user:\"
   echo
   echo \"         htpasswd -c /etc/apache2/bpcadmin.users bpc-admin\"
   echo \"         chown httpd /etc/apache2/bpcadmin.users\"
   echo \"         chmod 0400  /etc/apache2/bpcadmin.users\"
   echo
   echo \"Also remember to restart the httpd service after installing this package.\"
   echo
fi

echo \"-----------------------------------------------------------------------------\"
echo
echo Remember to run \'backuppc-config\' to configure your new BackupPC installation.
echo
echo \"-----------------------------------------------------------------------------\"
echo

endef

# Required dependencies
define SLACK_REQUIRED
perl
httpd
par2cmdline
rsync-bpc
bzip2
rsync
perl-libnet
perl-backuppc-xs
perl-file-listing
perl-archive-zip
perl-net-ftp-retrhandle
perl-net-ftp-autoreconnect
perl-cgi
perl-html-parser
rrdtool
dejavu-fonts
iztaci-netqmail
endef

# Suggested dependencies
define SLACK_SUGGESTS
samba
endef

# Package conflicts (lprng vs cups for example)
define SLACK_CONFLICTS
endef



##################### BUILDING #####################


CONFIGURE_SCRIPTS = custom
BUILD_SCRIPTS = 
INSTALL_SCRIPTS = 

CONFIGURE_ARGS = $(DIRPATHS)
NODIRPATHS = 

# Directories to be created as part of the install process.
# Can be inside the final package or just transient dirs we need
# while building it.
INSTALL_DIRS += $(DESTDIR)/srv/www/backuppc
INSTALL_DIRS += $(DESTDIR)/etc/apache2/extra
INSTALL_DIRS += $(DESTDIR)/usr/sbin
INSTALL_DIRS += $(DESTDIR)/etc/s6-rc/source
INSTALL_DIRS += $(DESTDIR)/etc/sysconfig
INSTALL_DIRS += $(DESTDIR)$(docdir)
INSTALL_DIRS += $(DESTDIR)$(mandir)/man8

# If some dirs are trying to install into /, list their names
# here, such as: prefix bindir mandir
INSTALL_OVERRIDE_DIRS = 


# Dependencies are of the form categorydir/packagedir
# LIBDEPS are for libraries, and DEPENDS are for everything else
DEPENDS =
LIBDEPS =
DEPENDS =
# Builddeps are installed in the build DESTIMG
BUILDDEPS =

##################### GAR #####################

# This should go before any hand-made rules.
include config.mk
include ../level.mk

##################### CUSTOMIZING #####################

$(WORKSRC)/bpcadmin: files/bpcadmin.c
	gcc -o $(WORKSRC)/bpcadmin files/bpcadmin.c

pre-configure:
	grep -Eq "^backuppc:" /etc/group  || groupadd backuppc
	grep -Eq "^backuppc:" /etc/passwd || useradd -s /bin/false -d /dev/null -g backuppc backuppc
	$(MAKECOOKIE)

post-build: $(WORKSRC)/bpcadmin
	$(MAKECOOKIE)

post-install:
	$(MAKE) s6rc
	$(MAKE) sysconfig
	mv $(DESTDIR)/etc/BackupPC/hosts $(DESTDIR)/etc/BackupPC/hosts.new
	mv $(DESTDIR)/etc/BackupPC/config.pl $(DESTDIR)/etc/BackupPC/config.pl.new
	mv -v $(DESTDIR)/usr/libexec/backuppc/share/doc/BackupPC/* $(DESTDIR)/$(docdir)
	$(foreach fixdir, $(FIX_DIRS), chmod 755 $(DESTDIR)/$(fixdir); )
	pod2man $(DESTDIR)/$(docdir)/BackupPC.pod $(DESTDIR)/$(mandir)/man8/BackupPC.8
	echo ".so man8/BackupPC.8" > $(DESTDIR)/$(mandir)/man8/backuppc.8
	rm -r $(DESTDIR)/var
	rm -rv $(DESTDIR)/usr/libexec/backuppc/share
	$(MAKECOOKIE)

configure-custom:
	cd $(WORKSRC); perl ./configure.pl $(CONFIGURE_OPTS)
	$(MAKECOOKIE)

