 
 SYMBOL: builder-recipients
 
+: tag-subject ( str -- str ) `{ "builder@" ,[ host-name ] ": " , } concat ;
+
+: email-string ( subject -- )
+  `{ "mutt" "-s" ,[ tag-subject ] %[ builder-recipients get ] }
+  [ ] with-process-stream drop ;
+
 : email-file ( subject file -- )
   `{
     { +stdin+ , }
-    { +arguments+ { "mutt" "-s" , %[ builder-recipients get ] } }
+    { +arguments+
+      { "mutt" "-s" ,[ tag-subject ] %[ builder-recipients get ] } }
   }
   >hashtable run-process drop ;
 
-: email-string ( subject -- )
-  `{ "mutt" "-s" , %[ builder-recipients get ] }
-  [ ] with-process-stream drop ;
+! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+: run-or-notify ( desc message -- )
+  [ [ try-process ]        curry ]
+  [ [ email-string throw ] curry ]
+  bi*
+  recover ;
+
+: run-or-send-file ( desc message file -- )
+  >r >r [ try-process ]         curry
+  r> r> [ email-string throw ] 2curry
+  recover ;
 
 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 


! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

SYMBOL: build-status

: build ( -- )

  "running" build-status set-global

  datestamp >stamp

  "/builds/factor" cd
  
  {
    "git"
    "pull"
    "--no-summary"
    "git://factorcode.org/git/factor.git"
    "master"
  }
  run-process process-status
  0 =
  [ ]
  [
    "builder: git pull" email-string
    "builder: git pull" throw
  ]
  if

  {
    "git" "pull" "--no-summary"
    "http://dharmatech.onigirihouse.com/factor.git" "master"
  } run-process drop

  "/builds/" stamp> append make-directory
  "/builds/" stamp> append cd

  { "git" "clone" "../factor" } run-process drop

  "factor" cd

  { "git" "show" } <process-stream> [ readln ] with-stream " " split second
  "../git-id" log-object

  { "make" "clean" } run-process drop

  ! "vm" build-status set-global

  `{
     { +arguments+ { "make" ,[ target ] } }
     { +stdout+    "../compile-log" }
     { +stderr+    +stdout+ }
   }
  >hashtable run-process process-status
  0 =
  [ ]
  [
    "builder: vm compile" "../compile-log" email-file
    "builder: vm compile" throw
  ] if

  [ my-arch download-image ]
  [ ]
  [ "builder: image download" email-string ]
  cleanup

  ! "bootstrap" build-status set-global

  `{
     { +arguments+ {
                     ,[ factor-binary ]

     { +stdout+   "../boot-log" }
     { +stderr+   +stdout+ }
   }
  >hashtable ;

: builder-test ( -- desc ) `{ ,[ factor-binary ] "-run=builder.test" } ;
  
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

SYMBOL: build-status

: build ( -- )

  "running" build-status set-global

  "/builds/factor" cd

  git-pull "git pull error" run-or-notify

  enter-build-dir
  
  git-clone "git clone error" run-or-notify

  "factor" cd

  record-git-id

  make-clean "make clean error" run-or-notify

  make-vm "vm compile error" "../compile-log" run-or-send-file

  retrieve-boot-image

  bootstrap "bootstrap error" "../boot-log" run-or-send-file

  builder-test "builder.test fatal error" run-or-notify
  
  "../load-everything-log" exists?
  [ "builder: load-everything" "../load-everything-log" email-file ]
