# Copyright (c) 1997-now, NeoNova BV, The Netherlands. All rights Reserved.
#
# This software is the copyrighted work of NeoNova BV.
# Use of the software is subject to the general business terms and conditions of NeoNova BV
# and its related transaction documents like license sheets or other written notices.#
# Any reproduction or redistribution of the software not in accordance with the license terms
# is expressly prohibited by law, and may result in severe civil and criminally penalties.
# Violators will be prosecuted to the maximum extent possible.

#
# 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=\
 activate \
 alert \
 asterisk_extensions \
 asterisk_hints \
 asterisk_queue_members \
 asterisk_queues \
 asterisk_sipconf \
 asterisk_voicemail \
 astglobals \
 astium \
 astiumcdr \
 astmessage \
 billnotes \
 building \
 callhist \
 callrouting \
 card_b410p \
 card_duogsm \
 card_e100p \
 card_octobri \
 card_quadbri \
 card_quadgsm \
 card_t100p \
 card_tdm2400p \
 card_tdm400p \
 card_tdm410 \
 card_tdm800p \
 card_te110p \
 card_te120p \
 card_te205p \
 card_te210p \
 card_te405p \
 card_te410p \
 card_unogsm \
 card_x100p \
 certificate_store \
 channel \
 cidnum_to_alert \
 cmc \
 company \
 conference \
 conference_log \
 contact \
 contact_department \
 contacthist \
 contact_number \
 contact_pickupgroup \
 contacttype \
 country \
 dahdi_channel \
 department \
 dhcp_parameter \
 dhcp_parameter_type \
 dir_entry \
 disa \
 dmz_to_external \
 dmz_to_internal \
 extensionrange \
 external_to_dmz \
 external_to_internal \
 extrafield \
 fastdial_company \
 fastdial_contact \
 fdpin \
 history \
 host \
 inbound \
 inbound_schedule \
 internal_to_dmz \
 internal_to_external \
 ivr \
 ivroption \
 lease \
 local_network \
 mailtime \
 messaging \
 messaging_input \
 messaging_output \
 messaging_translate \
 moh \
 national_dialplan \
 network \
 nightsettings \
 pagegroup \
 pagegroup_phone \
 parkinglot \
 permissions \
 phone \
 phonebook \
 phonebook_data_1 \
 phonebook_data_2 \
 phonebook_data_3 \
 phonebook_data_4 \
 phonebook_data_5 \
 phonebutton \
 phonelegend \
 phoneline \
 phoneline_forwarding \
 phoneline_member \
 phoneline_schedule \
 phoneringtone \
 phonetype \
 phonetypebutton \
 phonetypecontactbutton \
 phonetypecontactphone \
 phonetypecontactringtone \
 phonetypecontacttyperingtone \
 phonetypephone \
 phonetyperingtone \
 pickupgroup \
 plugin \
 presence \
 profile \
 qprc \
 queue \
 queue_contact \
 queuelog \
 rate \
 rateplan \
 rateplanschedule \
 ringgroup \
 ringgroup_contact \
 ringtone \
 role \
 router \
 schedule \
 screenpos \
 sessions \
 soundfile \
 subnet \
 switch \
 to_intf_external \
 trunk \
 trunk_lcr_trunk \
 voicemail \
 wakeup \
 zonedst \
 zonetab

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

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)
	cat astium.triggers | $(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

