#
# Makefile to create the Astium database
# 

EXTRACT_HELP=/usr/local/astium/setup/db/scripts/extract-help
ifeq ($(DB),sqlite)
  DBCMD=grep -v ^GRANT | sqlite3 astium.db
else
  DBCMD=psql --variable ON_ERROR_STOP=yes -q -U astium -d astium
endif

all: astium.created

ASTIUM_TABLES=astmessage queuelog country zonetab zonedst dhcp_parameter_type dhcp_parameter \
 national_dialplan rate rateplan activate alias astglobals  \
 building callrouting card_duogsm card_unogsm card_quadgsm card_e100p card_octobri card_quadbri card_t100p card_tdm800p \
 card_tdm2400p card_tdm400p card_tdm410 card_te110p card_te120p card_te205p card_te210p card_te405p card_te410p card_x100p \
 callhist billnotes certificate_store channel company conference_log conference contact_department \
 contact_number contact contacttype country department dir_entry disa dmz_to_external dmz_to_internal \
 domain emaildomain extensionrange external_to_dmz external_to_internal fastdial_company \
 fastdial_contact fdpin generate history host inbound_schedule inbound internal_to_dmz \
 internal_to_external ipsec_hosts ip ivroption ivr mailaccount mailtime moh network \
 nightsettings outofoffice pagegroup_phone pagegroup phonebutton phonelegend phoneline_member \
 phoneline phoneringtone phone phonetypebutton phonetypecontactbutton phonetypecontactphone \
 phonetypecontactringtone phonetypecontacttyperingtone phonetypephone phonetyperingtone phonetype \
 plugin queue_contact queue rateplanschedule redirect reverse_dns ringtone schedule \
 sender_blacklist sender_whitelist sessions soundfile staticroute subnet surf_blacklist \
 surf_whitelist to_intf_external trunk_lcr_trunk trunk vpn wwwdomain wwwrevproxy zap_channel \
 zonedst zonetab astium phoneline_forwarding profile phoneline_schedule screenpos voicemail \
 pickupgroup contact_pickupgroup permissions astiumcdr

ASTIUM_DATA=astglobals contact_department national_dialplan phonetypebutton soundfile \
 astmessage country phonetype zonedst company dhcp_parameter \
 phonelegend phonetyperingtone zonetab contact dhcp_parameter_type moh phoneline ringtone

ASTIUM_TRIGGERS=company phone rateplan phoneline

astium.created: $(ASTIUM_TABLES:%=%.table.created) $(ASTIUM_DATA:%=%.data.created) $(ASTIUM_TRIGGERS:%=%.triggers.created) astium.help
ifneq ($(DB),sqlite)
	cat foreign.keys | $(DBCMD) 
	cat astium.functions | $(DBCMD)
endif
	cat astium.views | $(DBCMD)
	touch astium.created

astium.help: $(ASTIUM_TABLES:%=%.table.help)

%.table.created: %.table
	cat $< | $(DBCMD)
	touch $<.created

%.table.help: %.table
	$(EXTRACT_HELP) $(PWD)/$< | $(DBCMD) 
	touch $<.help

%.triggers.created: %.triggers
	cat $< | $(DBCMD) 
	touch $<.created

%.data.created: %.data
	cat $< | $(DBCMD)
	touch $<.created

%.table: %.table.m4
	m4 < $< > $@

clean:	
	rm -f *.created
	rm -f phone.table phonetype.table phonetypephone.table phonetypecontactphone.table

company.triggers.created: phonetypebutton.table.created contacttype.table.created phoneline.data.created

phone.triggers.created: phonetypebutton.table.created phonetype.table.created phonebutton.table.created

rateplan.triggers.created: rateplanschedule.table.created

