make it optional where the hlr database is stored

Add --database to define where the database is stored. The default
was changed to not store the file in /tmp anymore.
diff --git a/src/bsc_hack.c b/src/bsc_hack.c
index 274a9cf..3076758 100644
--- a/src/bsc_hack.c
+++ b/src/bsc_hack.c
@@ -46,6 +46,7 @@
 /* MCC and MNC for the Location Area Identifier */
 static int MCC = 1;
 static int MNC = 1;
+static const char *database_name = "hlr.sqlite3";
 
 
 /* The following definitions are for OM and NM packets that we cannot yet
@@ -653,6 +654,7 @@
 	printf("  -s --disable-color\n");
 	printf("  -n --network-code number(MNC) \n");
 	printf("  -c --country-code number (MCC) \n");
+	printf("  -l --database db-name The database to use\n");
 	printf("  -h --help this text\n");
 }
 
@@ -666,6 +668,7 @@
 			{"disable-color", 0, 0, 's'},
 			{"network-code", 1, 0, 'n'},
 			{"country-code", 1, 0, 'c'},
+			{"database", 1, 0, 'l'},
 			{0, 0, 0, 0}
 		};
 
@@ -691,6 +694,9 @@
 		case 'c':
 			MCC = atoi(optarg);
 			break;
+                case 'l':
+			database_name = strdup(optarg);
+			break;
 		default:
 			/* ignore */
 			break;
@@ -771,7 +777,7 @@
 	/* parse options */
 	handle_options(argc, argv);
 
-	if (db_init()) {
+	if (db_init(database_name)) {
 		printf("DB: Failed to init database. Please check the option settings.\n");
 		return 1;
 	}	 
diff --git a/src/db.c b/src/db.c
index bc0b512..a3bf6ce 100644
--- a/src/db.c
+++ b/src/db.c
@@ -19,11 +19,14 @@
 
 #include <openbsc/db.h>
 
+#include <libgen.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <dbi/dbi.h>
 
+static char *db_basename = NULL;
+static char *db_dirname = NULL;
 dbi_conn conn;
 
 void db__error_func(dbi_conn conn, void* data) {
@@ -32,7 +35,7 @@
 	printf("DBI: %s\n", msg);
 }
 
-int db_init() {
+int db_init(const char *name) {
 	dbi_initialize(NULL);
 	conn = dbi_conn_new("sqlite3");
 	if (conn==NULL) {
@@ -51,10 +54,15 @@
 	*/
 
 	/* SqLite 3 */
-	dbi_conn_set_option(conn, "sqlite3_dbdir", "/tmp");
-	dbi_conn_set_option(conn, "dbname", "hlr.sqlite3");
+	char *db_basename = strdup(name);
+	char *db_dirname = strdup(name);
+	dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
+	dbi_conn_set_option(conn, "dbname", basename(db_basename));
 
 	if (dbi_conn_connect(conn) < 0) {
+		free(db_dirname);
+		free(db_basename);
+		db_dirname = db_basename = NULL;
 		return 1;
 	}
 
@@ -138,6 +146,11 @@
 int db_fini() {
 	dbi_conn_close(conn);
 	dbi_shutdown();
+
+	if (db_dirname)
+	    free(db_dirname);
+	if (db_basename)
+	    free(db_basename);
 	return 0;
 }
 
diff --git a/src/db_test.c b/src/db_test.c
index 0367ce2..760d7ab 100644
--- a/src/db_test.c
+++ b/src/db_test.c
@@ -25,7 +25,7 @@
 
 int main() {
 
-	if (db_init()) {
+	if (db_init("hlr.sqlite3")) {
 		printf("DB: Failed to init database. Please check the option settings.\n");
 		return 1;
 	}