--- dlume-0.2.4.orig/src/prefs.c
+++ dlume-0.2.4/src/prefs.c
@@ -32,20 +32,84 @@
 
 FILE *prefs_filehandle;
 
+
+/*---------------------------------------------------------------------------*/
+
+void exit_with_message (const char *str)
+{
+    fprintf(stderr, str);
+
+    struct stat info;
+
+    /* old way; disabled now */
+    if ( 0 && stat("/usr/bin/xmessage", &info) == 0 )
+    {
+	execl("/usr/bin/xmessage", "dlume error", str, NULL);
+    }
+
+    /* http://developer.gnome.org/doc/API/2.4/gtk/GtkMessageDialog.html */
+    gtk_init( NULL, NULL);
+
+    GtkWindowType type = GTK_WINDOW_TOPLEVEL;
+    GtkWidget *parent = gtk_window_new(type);
+    GtkWidget *dialog = gtk_message_dialog_new( GTK_WINDOW(parent),
+                                                GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                GTK_MESSAGE_ERROR,
+                                                GTK_BUTTONS_CLOSE,
+                                                "%s",
+                                                str);
+
+    /* http://developer.gnome.org/doc/API/2.4/gtk/GtkDialog.html#gtk-dialog-run */
+    (void) gtk_dialog_run( GTK_DIALOG(dialog));
+    gtk_widget_destroy( dialog);
+
+
+    /*
+    gtk_widget_show( info_dialog);
+
+    g_signal_connect( G_OBJECT(info_dialog),
+                      "response",
+                      G_CALLBACK(gtk_widget_destroy),
+                      NULL);
+    */
+
+
+    /*  The execl may fail due to DISPLAY not set. */
+    exit(1);
+}
+
 /*---------------------------------------------------------------------------*/
 
 gchar* s_getdir_config (void)
 {
-static gchar cfgdir[MAX_PATH];
-struct stat cfg;
+       static gchar cfgdir[MAX_PATH];
+       gchar oldcfgdir[MAX_PATH];
+       struct stat cfg;
+
+       s_strcpy( oldcfgdir, getenv ("HOME"), MAX_PATH);
+       s_strcat( oldcfgdir, "/", MAX_PATH);
+       s_strcat( oldcfgdir, ".clay", MAX_PATH);
+
+       s_strcpy( cfgdir, getenv ("HOME"), MAX_PATH);
+       s_strcat( cfgdir, "/", MAX_PATH);
+       s_strcat( cfgdir, DATADIR, MAX_PATH);
+
+       if ( stat(oldcfgdir, &cfg) == 0 )
+       {
+	   char str[2000];
+	   sprintf(str,
+		   "[FATAL] dlume: Old config found. Please upgrade "
+                   "by running command: mv %s %s\n",
+		   oldcfgdir,
+		   cfgdir);
 
-	s_strcpy (cfgdir, getenv ("HOME"), MAX_PATH);
-	s_strcat (cfgdir, "/.clay", MAX_PATH);
+	   exit_with_message(str);
+       }
 
-	if(stat (cfgdir, &cfg) < 0)
-		mkdir (cfgdir, S_IRUSR | S_IWUSR | S_IXUSR);
+       if ( stat(cfgdir, &cfg) < 0 )
+	   mkdir (cfgdir, S_IRUSR | S_IWUSR | S_IXUSR);
 
-	return cfgdir;
+       return cfgdir;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -76,6 +140,72 @@
 
 /*---------------------------------------------------------------------------*/
 
+void s_funlock (gchar *filename)
+{
+	int  MAX     = 2000;
+	char lock[MAX];
+
+	strcpy( lock, filename);
+	s_strcpy( lock, filename, MAX);
+	s_strcat( lock, ".lock", MAX);
+
+	if ( !access(lock, 0) )
+	{
+	    if ( unlink(lock) )
+	    {
+		char str[MAX * 2];
+
+		sprintf(str,
+		   "[FATAL] dlume: Cannot remove lock: %s\n",
+		   lock);
+
+		exit_with_message(str);
+	    }
+	}
+}
+
+void s_flock (gchar *filename)
+{
+	FILE *handle = NULL;
+	int  MAX     = 2000;
+	pid_t pid    = getpid();
+
+	char lock[MAX];
+
+	strcpy( lock, filename);
+	s_strcpy( lock, filename, MAX);
+	s_strcat( lock, ".lock", MAX);
+
+	if ( !access(lock, 0) )
+	{
+	   char str[MAX * 2];
+
+	   sprintf(str,
+		   "[FATAL] dlume: Another process has locked the database: %s\n",
+		   lock);
+
+	   exit_with_message(str);
+	}
+
+	handle = fopen(lock, "wb");
+
+	if ( handle == NULL )
+	{
+	   char str[MAX * 2];
+
+	   sprintf(str,
+		   "[FATAL] dlume: Cannot create lock: %s\n",
+		   lock);
+
+	   exit_with_message(str);
+	}
+
+	fprintf( handle, "%lu\n", (unsigned long) pid);
+	fclose(handle);
+}
+
+/*---------------------------------------------------------------------------*/
+
 void* s_prefs_openfile (gchar *filename, gint openmode)
 {
 	prefs_filehandle = NULL;
@@ -361,8 +491,9 @@
 
 void write_config(void)
 {
+        char *file = s_getfilename_config();
 
-	if (s_prefs_openfile (s_getfilename_config (), P_WRITE)) {
+	if (s_prefs_openfile (file, P_WRITE)) {
 
 		s_prefs_put_int ("window_pos_x", config.window_x);
 		s_prefs_put_int ("window_pos_y", config.window_y);
