@ECHO OFF
IF "%1" == "" goto ERROR

set name=%1
set first=%2
set second=%3
set third=%4
setlocal EnableDelayedExpansion


ECHO Top 3 languages: %first% %second% %third%


if exist %name%_cities.sqlite del %name%_cities.sqlite

set firstSql=name_%first%
set secondSql=name_%second%
set thirdSql=name_%third%

REM If you've modified the script to use a specific language, remove/modify the appropriate %firstSql% etc columns from the query
Echo Building Database
 echo  drop table if exists Cities; 																					^
& echo  drop table if exists CitiesAlt; 																				^
& echo  create table CitiesAlt (																						^
& echo 	Name text,																										^
& echo 	%firstSql% text,																								^
& echo 	%secondSql% text,																								^
& echo 	%thirdSql% text,																								^
& echo 	Latitude real,																									^
& echo 	Longitude real);																								^
& echo create table Cities (																							^
& echo 	Name text,																										^
& echo 	%firstSql% text,																								^
& echo 	%secondSql% text,																								^
& echo 	%thirdSql% text,																								^
& echo 	Latitude real,																									^
& echo 	Longitude real); 																								^
& echo -- import the data from the generated csv files																	^
& echo .separator ","																									^
& echo .import %name%.csv CitiesAlt																				^
& echo -- clean the CityNames table...																					^
& echo -- ...by first removing entries where we don't have a name...													^
& echo delete																											^
& echo from CitiesAlt																									^
& echo where Name = '';																									^
& echo --Clean them further by removing duplicates of the same cities (same exact name and approximate location)		^
& echo Insert Into Cities(Name, %firstSql%, %secondSql%, %thirdSql%, Latitude, Longitude)								^
& echo select  name, 																									^
& echo 		%firstSql%, 																								^
& echo 		%secondSql%, 																								^
& echo 		%thirdSql%, 																								^
& echo 		cast(cast(Latitude*1000 as integer)as real)/1000  as Latitude,												^
& echo 		cast(cast(Longitude*1000 as integer)as real)/1000  as Longitude												^
& echo from CitiesAlt 																									^
& echo group by  Latitude, Longitude,  name																				^
& echo order by name asc;																								^
& echo drop table CitiesAlt;																							^
& echo .quit																											^
& | sqlite3.exe %name%_cities.sqlite 

echo Created File %name%_cities.sqlite, you can now use this file for Offline Cities search
endlocal
goto DONE

:ERROR
ECHO Need to supply the originial osm.pbf file to process
goto DONE

:ERROR_FILE_TYPE
ECHO File type needs to be osm.pbf

:DONE

