package sophena.db;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

import sophena.io.datapack.Import;

/**
 * TODO: This should be located in a build-project?
 * 
 * Creates a new database.
 */
public class BuildDb {

	public static void main(String[] args) {
		try {
			Path dbDir = Files.createTempDirectory("__sophena_db__");
			Files.delete(dbDir);
			System.out.println("Database folder: " + dbDir);
			Database db = new Database(dbDir.toFile());
			String dataPath = "C:/Users/Besitzer/Projects/go_path/src/git.greendelta.com/sophdat/gen/base_data.sophena";
			Import dataImport = new Import(
					new File(dataPath), db);
			dataImport.run();
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

@@ -91,16 +91,96 @@
 .Path;%0A%0A
+import org.apache.commons.io.FileUtils;%0Aimport org.zeroturnaround.zip.ZipUtil;%0A%0A
 import s
@@ -322,16 +322,152 @@
 ldDb %7B%0A%0A
+%09private static final String PACKAGE_PATH = %22C:/Users/Besitzer/Projects/go_path/src/git.greendelta.com/sophdat/gen/base_data.sophena%22;%0A%0A
 %09public 
@@ -512,16 +512,17 @@
 y %7B%0A
+%0A
 %09%09%09Path 
 dbDi
@@ -517,18 +517,19 @@
 %09%09%09Path 
-db
+tmp
 Dir = Fi
@@ -582,22 +582,53 @@
 File
-s.delete(dbDir
+ dbDir = new File(tmpDir.toFile(), %22database%22
 );%0A%09
@@ -649,25 +649,34 @@
 rintln(%22
-D
+Generate d
 atabase 
 folder: 
@@ -671,14 +671,10 @@
 ase 
-folder
+in
 : %22 
@@ -722,230 +722,523 @@
 bDir
-.toFile()
 );%0A
+%0A
 %09%09%09S
-tring dataPath = %22C:/Users/Besitzer/Projects/go_path/src/git.greendelta.com/sophdat/gen/base_data.sophena%22;%0A%09%09%09Import dataImport = new Import(%0A%09%09%09%09%09new File(dataPath), db
+ystem.out.println(%22Import data from: %22 + PACKAGE_PATH);%0A%09%09%09Import dataImport = new Import(new File(PACKAGE_PATH), db);%0A%09%09%09dataImport.run();%0A%09%09%09db.close();%0A%0A%09%09%09File zip = new File(%22resources/database.zip%22);%0A%09%09%09System.out.println(%22Delete old databse: %22 + zip);%0A%09%09%09FileUtils.forceDelete(zip);%0A%09%09%09System.out.println(%22Package new database%22);%0A%09%09%09ZipUtil.pack(tmpDir.toFile(), zip
 );%0A
+%0A
 %09%09%09
-dataImport.run();%0A%09%09%09db.close(
+System.out.println(%22Delete temporary folder%22);%0A%09%09%09FileUtils.deleteDirectory(tmpDir.toFile());%0A%0A%09%09%09System.out.println(%22All done%22
 );%0A%09

copy created database directly to resource folder