#!/usr/bin/make -f

START=$(shell startmonth -1)
END=$(shell endmonth -1)
MONTH=$(shell startmonth -1 | sed 's/..$$//')
DEPLOYMENTS=
#tas act snc demo for testing
CUSTOMER_FIELDS=name active create_date active_date full_name street suburb state postcode category domain_name www_domain
USAGE_FIELDS=$(shell </etc/sina/stats_internal_report_usage_fields nosql column -N field)
DAC=deployment admin customer_name

all: icr.$(MONTH).zip license_fee

summary_types:
	summary_types |							\
	nosql sort type_name >$@

usage_fields.1: /etc/sina/stats_internal_report_usage_fields summary_types
	<$< process -P -o 'summary_type field' -m '			\
		for $$summary_type (split / /, $$summary_types) {pr}' |	\
	nosql sort summary_type |					\
	nosql join -1 summary_type -2 type_name - summary_types |	\
	process -o 'type_id field' -r 'type_id summary_type' |		\
	nosql sort type_id						\
	>$@

types_needed: usage_fields.1
	<$< nosql column -N summary_type | uniq | commas >$@

customers/all:
	sina-query -d customers "					\
		select $(shell echo $(CUSTOMER_FIELDS) | tr ' ' ',')	\
		from customers						\
	" $(DEPLOYMENTS)

customers.1: customers/all
	<$< process -o "deployment admin $(CUSTOMER_FIELDS)"		\
		-m '$$active_date ne "\\" and				\
			$$active_date > $(END) and			\
				$$active = "N"'				\
		-r 'name customer_name' >$@

groups/all:
	sina-query -d groups "						\
		select customer_name, group_name, count(*) as users	\
		from users						\
		group by customer_name, group_name			\
	" $(DEPLOYMENTS)

groups.1: groups/all
	<$< process							\
		-o "$(DAC) `<$< distinct group_name | header - |	\
			sed 's/^\\\\$$/NONE/; s/^/group_/'`"		\
		-g "$(DAC)"						\
		-vai 0							\
		-ga '	$$group_name eq "\\" and $$group_name = "NONE";	\
			no strict; $${"group_$$group_name"} = $$users'	\
	>$@

users: groups/all
	<$< process -o "$(DAC) total_users"				\
		-g "$(DAC)"						\
		-ga '$$total_users += $$users'				\
	>$@

summary/all: types_needed
	sina-query -d summary "						\
		select customer_name, summary_type, sum(data) as data	\
		from summary						\
		where summary_date >= $(START)				\
			and summary_date <= $(END)			\
			and summary_type in (`< types_needed`)		\
		group by customer_name, summary_type			\
	" $(DEPLOYMENTS)

usage.1: summary/all usage_fields.1
	<$< mjoin summary_type usage_fields.1 |				\
	nosql sort $(DAC) field |					\
	process	-o '$(DAC) field total'					\
		-g '$(DAC) field'					\
		-ga '$$total += $$data' >$@

usage.2: usage.1
	<$< process -o "$(DAC) $(USAGE_FIELDS)"				\
		-g "$(DAC)"						\
		-vai 0							\
		-ga 'no strict; $$$$field = $$total'			\
	>$@

user_summary/all:
	sina-query -d user_summary "					\
		select distinct customer_name, login_name		\
		from user_summary					\
		where summary_date >= $(START)				\
			and summary_date <= $(END)			\
		order by customer_name					\
	" $(DEPLOYMENTS)

unique_logins: user_summary/all
	<$< process -g '$(DAC)'						\
		-a unique_logins					\
		-ga '$$unique_logins++' >$@

report: customers.2 usage.2 groups.1 users unique_logins
	<$< mjoin -a1 "$(DAC)"						\
		usage.2 groups.1 users unique_logins >$@

icr.$(MONTH).zip: report
	zip $@ $<

cleanish:
	-rm unique_logins customers.1 groups.1 users usage.1 usage.2 report

clean: cleanish
	-rm -r customers groups summary user_summary summary_types	\
		usage_fields.1 types_needed

view: report
	<$< nosql print | sed 's/^/ /' | less

not_billable.all:
	sina-query -A -d not_billable "					\
		select customer_name					\
		from options						\
		where name='not_billable'				\
	" $(DEPLOYMENTS)
	mv not_billable/all not_billable.all

not_billable.1: not_billable.all
	<$< process -r 'name customer_name' >$@

customers.active: customers.1
	< $< process -f -o 'deployment customer_name' -m '$$active eq "y"' > $@

customers.inactive: customers.1
	< $< process -f -o 'deployment customer_name' -m '$$active ne "y"' > $@

%.uniq: %
	< $< nosql sort -u > $@

customers.%.system: customers.%
	< $< process -f -m '						\
		$$_ = $$customer_name;					\
		/^$$/ or						\
		/^adm[0-9]+$$/ or					\
		/^prx[0-9]+$$/ or					\
		/^upgrade/ or						\
		/training/ or						\
		/^rtr/ or						\
		/^other/						\
	' > $@

customers.system: customers.active.system customers.inactive.system
	cjoin $^ > $@

customers.%.real: customers.%.uniq customers.%.system
	cjoin -w 10 $^ > $@

customers.billable: customers.active.real not_billable.1.uniq
	cjoin -w 10 $^ > $@

customers.not_billable: customers.active.real customers.billable
	cjoin -w 10 $^ > $@

customers.2: customers.1 customers.billable customers.not_billable customers.system customers.inactive
	cjoin -W $^ |							\
	process -r "which billable"					\
		-v "map"						\
		-m 'BEGIN { $$map = {"11000" => "y", "10100" => "n",	\
			"10010" => "s", "10001" => "i"} }		\
			$$which = $$map->{$$which} || $$which		\
		' > $@

license_fee: customers.2
	< $< nosql column billable deployment category | nosql sort |	\
	process -g 'billable deployment category'			\
		-a 'count'						\
		-ga '$$count ++'					\
		> $@

.PHONY: all cleanish clean view
