<Project DefaultTargets="Build" InitialTargets="CreateOutputPath" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

	<!-- ASCII Constants -->
	<PropertyGroup>
		<DOUBLE_QUOTES>%22</DOUBLE_QUOTES>
	</PropertyGroup>

   <PropertyGroup>
		<DBUser Condition=" '$(DBUser)' == '' ">sysdba</DBUser>
		<DBPassword Condition=" '$(DBPassword)' == '' ">masterkey</DBPassword>   
		<DBServer Condition=" '$(SqlCmdRunner)' == '' ">localhost</DBServer>
		<DeploymentBuild Condition=" '$(DeploymentBuild)' == '' ">false</DeploymentBuild>
		<SqlScriptsFolder Condition=" '$(SqlScriptsFolder)' == '' ">$(MSBuildProjectDirectory)\Scripts</SqlScriptsFolder>
		<ToolsFolder Condition=" '$(ToolsFolder)' == '' ">$(MSBuildProjectDirectory)\..\Tools</ToolsFolder>
		<CodeOutputFolder Condition=" '$(CodeOutputFolder)' == '' ">$(MSBuildProjectDirectory)\..\Output</CodeOutputFolder>
		<SqlCmdRunner Condition=" '$(SqlCmdRunner)' == '' ">$(ToolsFolder)\ISQL\isql.exe</SqlCmdRunner>	  
		<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
   </PropertyGroup>

   <PropertyGroup Condition=" '|$(Configuration)|' == '|Debug|' ">
      <OutputPath>$(CodeOutputFolder)\Debug\Database</OutputPath>
   </PropertyGroup>
   <PropertyGroup Condition=" '|$(Configuration)|' == '|Release|' ">
      <OutputPath>$(CodeOutputFolder)\Release\Database</OutputPath>
   </PropertyGroup>

   <ItemGroup>
      <Sql Include="$(SqlScriptsFolder)\etiquetas.sql" />
      <Sql Include="$(SqlScriptsFolder)\etiquetas_sysdata.sql" />	  
      <SqlOutput Include="$(OutputPath)\etiquetas.fdb" />
   </ItemGroup>
  
	<Target Name="CreateOutputPath">
		<MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')"/>
	</Target>

   <Target Name="Build" 
           DependsOnTargets="CreateDatabase;InsertSysData"/>


    <Target Name="CreateDatabase" 
           Inputs="@(Sql)" 
           Outputs="@(SqlOutput)">

		<Exec Command="$(DOUBLE_QUOTES)$(SqlCmdRunner)$(DOUBLE_QUOTES) -i $(DOUBLE_QUOTES)$(SqlScriptsFolder)\etiquetas.sql$(DOUBLE_QUOTES) -u $(DBUser) -p $(DBPassword) -q" 
			WorkingDirectory="$(OutputPath)" />
   </Target>
   
    <Target Name="InsertSysData">
		<Exec Command="$(DOUBLE_QUOTES)$(SqlCmdRunner)$(DOUBLE_QUOTES) -i $(DOUBLE_QUOTES)$(SqlScriptsFolder)\etiquetas_sysdata.sql$(DOUBLE_QUOTES) -u $(DBUser) -p $(DBPassword) -q $(DOUBLE_QUOTES)$(OutputPath)\etiquetas.fdb$(DOUBLE_QUOTES)" 
			WorkingDirectory="$(OutputPath)" />
			
		<Exec Command="$(DOUBLE_QUOTES)$(SqlCmdRunner)$(DOUBLE_QUOTES) -i $(DOUBLE_QUOTES)$(SqlScriptsFolder)\etiquetas_listaprovincias.sql$(DOUBLE_QUOTES) -u $(DBUser) -p $(DBPassword) -q $(DOUBLE_QUOTES)$(OutputPath)\etiquetas.fdb$(DOUBLE_QUOTES)" 
			WorkingDirectory="$(OutputPath)" />
			
		<Exec Command="$(DOUBLE_QUOTES)$(SqlCmdRunner)$(DOUBLE_QUOTES) -i $(DOUBLE_QUOTES)$(SqlScriptsFolder)\etiquetas_listapoblaciones.sql$(DOUBLE_QUOTES) -u $(DBUser) -p $(DBPassword) -q $(DOUBLE_QUOTES)$(OutputPath)\etiquetas.fdb$(DOUBLE_QUOTES)" 
			WorkingDirectory="$(OutputPath)" />
   </Target>

   <Target Name="Extract" 
           Inputs="$(OutputPath)\etiquetas.fdb" 
           Outputs="$(OutputPath)\etiquetas_$(Configuration).sql">

		<Exec Command="$(DOUBLE_QUOTES)$(SqlCmdRunner)$(DOUBLE_QUOTES) -a -d etiquetas.fdb -u $(DBUser) -p $(DBPassword) -o $(DOUBLE_QUOTES)$(OutputPath)\etiquetas_$(Configuration).sql$(DOUBLE_QUOTES) $(DOUBLE_QUOTES)$(OutputPath)\etiquetas.fdb$(DOUBLE_QUOTES) -b" 
			WorkingDirectory="$(OutputPath)" />
   </Target>

   <Target Name="Rebuild" 
           DependsOnTargets="Clean;Build"/>

   <Target Name="Clean">
      <Delete Files="@(SqlOutput)"/>
   </Target>

</Project>