#! /bin/sh /usr/share/dpatch/dpatch-run
## 02_randomtheme.patch
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: No description.

@DPATCH@

--- bla/src/main.c.orig	Tue Oct  2 08:34:17 2001
+++ bla/src/main.c	Fri Oct 26 16:24:08 2001
@@ -92,6 +92,7 @@
   char rectangular_windows = 0;
   char load_message = 0;
   char all_themes = 0;
+  char random_theme = 0;
 
   theme_names = malloc(2 * sizeof(char *));
   theme_names[0] = DEFAULT_THEME;
@@ -206,6 +207,9 @@
     else if (LongArgumentIs("-all")) {
       all_themes = 1;
     }
+    else if (LongArgumentIs("-random-theme")) {
+      random_theme = 1;
+    }
     else if (LongArgumentIs("-id")) {
       if (argc > ++n) {
 	Window id;
@@ -276,6 +280,32 @@
   else if (describe_theme) {
     DescribeThemes(theme_names);
   }
+  else if (random_theme) {
+    char **tmp_list = xpenguins_list_themes(NULL);
+    if (tmp_list) {
+      srand(getpid() ^ time(NULL));
+
+      /* we need to count how many themes there are */
+      for(nthemes = 0; tmp_list[nthemes] != NULL; nthemes++) {
+      }
+      
+      free(theme_names);
+
+      theme_names = malloc(2 * sizeof(char *));
+      nthemes = rand() % nthemes;
+      theme_names[0] = strdup(tmp_list[nthemes]);
+      theme_names[1] = NULL;
+
+      /* we probably should free tmp_list and its contents */
+      /* not sure why but we get a segfault if we free the contents */
+      free(tmp_list);
+
+      if (xpenguins_verbose) {
+	fprintf(stderr, _("Selected random theme - '%s'\n"),
+	        theme_names[0]);
+      }
+    }
+  }
 
   if (ignore_popups) {
     xpenguins_ignorepopups(1);
@@ -412,6 +442,7 @@
 	    "  -a, --no-angels                   Do not show any cherubim\n"
 	    "  -s, --squish                      kill penguins with mouse\n"
 	    "      --all                         Run all available themes simultaneously\n"
+	    "      --random-theme                Choose a random theme\n"
 	    "      --id             <window id>  Send penguins to window with this ID\n"
 	    "      --nice           <ld1> <ld2>  Start killing penguins when load reaches\n"
 	    "                                       <ld1>, kill all if load reches <ld2>\n"
--- bla/xpenguins.1.orig	2001-10-02 01:07:03.000000000 +0200
+++ bla/xpenguins.1	2004-02-11 17:22:58.000000000 +0100
@@ -50,6 +50,9 @@
 .B "\-t"
 option to select the theme to describe.
 .TP 8
+.B "\-\-random\-theme"
+Start with a random theme.
+.TP 8
 .B "\-l, \-\-list\-themes"
 List the available themes, one on each line, and exit.
 .TP 8
