#! /bin/sh /usr/share/dpatch/dpatch-run
## 03_header_width.dpatch by Sylvain Le Gall <gildor@debian.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Compute header width based on header file content.

@DPATCH@
diff -urNad trunk~/main.ml trunk/main.ml
--- trunk~/main.ml	2008-04-24 22:55:07.685167988 +0200
+++ trunk/main.ml	2008-04-24 22:57:01.404332989 +0200
@@ -98,9 +98,16 @@
     with
       End_of_file -> close_in ic; []
   in
-  loop ()
-
-
+  let header = 
+    loop ()
+  in
+  let header_width =
+    List.fold_left 
+      (fun w line -> max (String.length line) w)  
+      0
+      header
+  in
+    header, header_width
 
 (***************************************************************************)
 (** {2 Processing files} *)
@@ -151,14 +158,14 @@
   in
   loop ()
 
-let create_header header filename =
+let create_header header header_width filename =
   let generator = find_generator filename in
   let skip_lst = find_skips filename in
   pipe_file (fun ic oc ->
     let () = Skip.skip skip_lst ic oc in
     let line = generator.Model.remove ic in
     let () = Skip.skip skip_lst ic oc in
-    generator.Model.create oc header;
+    generator.Model.create oc header header_width;
     output_string oc line;
     copy ic oc
   ) filename
@@ -184,24 +191,24 @@
 (** {2 Main loop} *)
 
 type action =
-    Create of string list
+    Create of string list * int
   | Remove
 
 
 let main () =
 
-  let action = ref (Create []) in
+  let action = ref (Create ([], 0)) in
 
   let anonymous filename =
     match !action with
-      Create header -> create_header header filename
+      Create (header, header_width) -> create_header header header_width filename
     | Remove -> remove_header filename
   in
 
   Arg.parse [
 
   "-h",
-  Arg.String (fun s -> action := Create (read_headerfile s)),
+  Arg.String (fun s -> let (header, header_width) = (read_headerfile s) in action := Create (header, header_width)),
   "<file>  Create headers with text from <file>";
 
   "-c",
diff -urNad trunk~/model.ml trunk/model.ml
--- trunk~/model.ml	2008-04-24 22:55:07.685167988 +0200
+++ trunk/model.ml	2008-04-24 22:55:50.907330341 +0200
@@ -26,7 +26,7 @@
 
 type generator =
     { remove: in_channel -> string;
-      create: out_channel -> string list -> unit;
+      create: out_channel -> string list -> int -> unit;
     } 
 
 (***************************************************************************)
@@ -92,8 +92,9 @@
       ""
   in
 
-  let create oc header =
-    let width' = width + 2 * String.length margin in
+  let create oc header header_width =
+    let real_width = max width header_width in
+    let width' = real_width + 2 * String.length margin in
     let white = String.make width' ' ' in
     let line = String.make width' line_char in
     Printf.fprintf oc "%s%s%s\n" open_comment line close_comment;
@@ -102,7 +103,7 @@
       output_string oc open_comment;
       output_string oc margin;
       output_string oc string;
-      output oc white 0 (max 0 (width - String.length string));
+      output oc white 0 (max 0 (real_width - String.length string));
       output_string oc margin;
       output_string oc close_comment;
       output_char oc '\n'
@@ -160,9 +161,10 @@
       ""
   in
 
-  let create oc header =
+  let create oc header header_width =
+    let real_width = max width header_width in
     Printf.fprintf oc "%s%s\n" open_comment 
-      (String.make (max 0 (width - String.length open_comment)) line_char);
+      (String.make (max 0 (real_width - String.length open_comment)) line_char);
     
     List.iter (function string ->
       output_string oc begin_line;
@@ -172,7 +174,7 @@
 
     Printf.fprintf oc "%s%s%s\n\n" 
       begin_last
-      (String.make (max 0 (width - String.length begin_last
+      (String.make (max 0 (real_width - String.length begin_last
 			     - String.length close_comment)) line_char)
       close_comment;
 
@@ -198,7 +200,7 @@
 let make_no () =
 
   { remove = (fun _ -> "");
-    create = (fun _ _ -> ())
+    create = (fun _ _ _ -> ())
   } 
 
 let _ =
