(* $HOME/.whenjobs *)
 let jobsdir = ref ""
 
-(* The state. *)
+(* The state.
+ *
+ * Note that whenever this is updated, you need to consider if you
+ * should call 'save_variables ()' (which persists the variables to a
+ * file).  XXX We should replace this ref with an accessor
+ * function.
+ *)
 let state = ref Whenstate.empty
 
+(* Format used to save variables.  Note we can't allow any internal
+ * types to "escape" into this definition, else the file format will
+ * change when parts of the program change.
+ *)
+type variables_file_v1 = (string * variable_v1) list
+and variable_v1 =
+  | Vv1_unit
+  | Vv1_bool of bool
+  | Vv1_string of string
+  | Vv1_int of big_int
+  | Vv1_float of float
+
+let variable_of_variable_v1 = function
+  | Vv1_unit -> T_unit
+  | Vv1_bool b -> T_bool b
+  | Vv1_string s -> T_string s
+  | Vv1_int i -> T_int i
+  | Vv1_float f -> T_float f
+
+let variable_v1_of_variable = function
+  | T_unit -> Vv1_unit
+  | T_bool b -> Vv1_bool b
+  | T_string s -> Vv1_string s
+  | T_int i -> Vv1_int i
+  | T_float f -> Vv1_float f
+
 (* Jobs that are running: a map of PID -> (job, tmpdir, serial, start_time).
  * Note that the job may no longer exist *OR* it may have been renamed,
  * eg. if the jobs file was reloaded.
   (* Handle SIGCHLD to clean up jobs. *)
   Sys.set_signal Sys.sigchld (Sys.Signal_handle handle_sigchld);
 
-  (* Initialize the variables. *)
-  state := Whenstate.set_variable !state "JOBSERIAL" (T_int zero_big_int)
+  (* Load or initialize the variables. *)
+  let variables_file = sprintf "%s/variables" !jobsdir in
+  state :=
+    try
+      let chan = open_in variables_file in
+      let r = load_variables !state chan in
+      close_in chan;
+      r
+    with
+    | Sys_error _ ->
+      Whenstate.set_variable !state "JOBSERIAL" (T_int zero_big_int)
+
+(* Try to load the variables from the file.  If the file exists and
+ * cannot be read, raise an exception.
+ *)
+and load_variables state chan =
+  let signature = input_line chan in
+  if signature = "WHENJOBS VARIABLES VERSION 1" then (
+    let variables : variables_file_v1 = input_value chan in
+    List.fold_left (
+      fun state (n, v) ->
+        Whenstate.set_variable state n (variable_of_variable_v1 v)
+    ) state variables
+  ) else (* in future, other signatures, but for now ... *)
+    failwith (sprintf "cannot read variables file: invalid signature: %s"
+                signature)
+
+and save_variables () =
+  let variables_file = sprintf "%s/variables" !jobsdir in
+  let new_file = variables_file ^ ".new" in
+  let chan = open_out new_file in
+  fprintf chan "WHENJOBS VARIABLES VERSION 1\n";
+  let variables = Whenstate.get_variables !state in
+  let variables =
+    List.map (fun (n, v) -> n, variable_v1_of_variable v) variables in
+  output_value chan variables;
+
+  (* Try to arrange that the new file is updated atomically. *)
+  flush chan;
+  Netsys_posix.fsync (descr_of_out_channel chan);
+  close_out chan;
+  rename new_file variables_file
 
 and proc_reload_file () =
   if !debug then Syslog.notice "remote call: reload_file";
     let jobnames, state' = reevaluate_whenjobs !state jobs in
     let state' = run_whenjobs state' jobnames in
     state := state';
+    save_variables ();
 
     `ok
   with
     let job = Whenstate.get_job !state jobname in
     let state' = run_job !state job in
     state := state';
+    save_variables ();
     `ok
   with
   | Not_found -> `error "job not found"
     let jobnames, state' = reevaluate_whenjobs !state jobs in
     let state' = run_whenjobs state' jobnames in
     state := state';
+    save_variables ();
 
     `ok
   with
       fun s (name, value) -> Whenstate.set_variable s name value
     ) !state vars in
     state := s;
+    save_variables ();
 
     (* .. but don't reevaluate or run jobs. *)
 
   let jobnames, state' = reevaluate_whenjobs ~onload:true !state jobs in
   let state' = run_whenjobs state' jobnames in
   state := state';
+  save_variables ();
 
   (* Schedule the next every job to run. *)
   schedule_next_everyjob ()
         delete_timer_group ();          (* Delete the timer. *)
         let state' = List.fold_left run_job !state jobs in
         state := state';
+        save_variables ();
         schedule_next_everyjob ()
       in
       Unixqueue.weak_once esys g t_diff run_jobs;
 
--- /dev/null
+# whenjobs
+# Copyright (C) 2012 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+EXTRA_DIST = $(TESTS)
+
+TESTS = test-persistent-variables.sh
+
 
--- /dev/null
+# whenjobs
+# Copyright (C) 2012 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Test that variables are reloaded across runs.
+
+unset CDPATH
+set -e
+
+testdir=$(pwd)
+libdir=$(cd ../../lib; pwd)
+toolsdir=$(cd ../../tools; pwd)
+daemondir=$(cd ../../daemon; pwd)
+
+HOME="$testdir"
+export HOME
+
+rm -rf "$testdir/.whenjobs"
+
+PATH="$toolsdir:$daemondir:$PATH"
+export PATH
+
+# Kill the daemon and clean up when the script exits.
+function cleanup {
+    kill `cat "$testdir/.whenjobs/daemon_pid"`
+    rm -rf "$testdir/.whenjobs"
+}
+trap cleanup INT TERM QUIT EXIT
+
+# Run the daemon.
+whenjobsd -d
+
+whenjobs --lib "$libdir" --type float --set test_float=4.2
+whenjobs --lib "$libdir" --type int --set test_int=42
+whenjobs --lib "$libdir" --type string --set test_string=fortytwo
+
+whenjobs --lib "$libdir" --daemon-stop >/dev/null 2>&1 ||:
+
+# Run the daemon again and read back the variables.
+whenjobsd -d
+
+output="$(whenjobs --lib "$libdir" --variables | LANG=C sort)"
+
+whenjobs --lib "$libdir" --daemon-stop >/dev/null 2>&1 ||:
+
+trap - INT TERM QUIT EXIT
+rm -rf "$testdir/.whenjobs"
+
+if [ "$output" != "JOBSERIAL=0
+test_float=4.2
+test_int=42
+test_string=fortytwo" ]; then
+    echo "$0: unexpected variables in output:"
+    echo "$output"
+    exit 1
+fi
+
+rm -rf "$testdir/.whenjobs"