
; ----------------------
; Gestion des evenements
; ----------------------

PRO rh_command_ath_event, event
common rh_wcommand_at, s_nb, nb, ind_mois, ind_jour

  WIDGET_CONTROL,Event.Id,GET_UVALUE=Ev

  IF ev EQ 'FIN' THEN BEGIN
     widget_control,s_nb, GET_VALUE = nb
     WIDGET_CONTROL,EVENT.TOP,/DESTROY
  ENDIF

  IF ev EQ 'MOIS' THEN ind_mois = event.index
  IF ev EQ 'JOUR' THEN ind_jour = event.index
  IF ev EQ 'nb'   THEN widget_control,event.Id,GET_VALUE = nb
  
END

;-------------------------------------
;-------------------------------------

Function rh_command_ath, group = group

common rh_wcommand_at, s_nb, nb, ind_mois, ind_jour

IF N_ELEMENTS(group) EQ 0 THEN group =0

  base = WIDGET_BASE(/column, TITLE='Run at a later time', $
         group=group, /BASE_ALIGN_CENTER,space=30, xpad=20, $
         ypad=20,/modal, xoffset=1, yoffset=1)
  l = widget_label(base, value = systime(0))

  dat = bin_date(systime())
  an = dat(0)
  ind_mois=dat(1)-1
  mois=['Janvier','Fevrier','Mars','Avril','Mai','Juin', 'Juillet','Aout','Septembre']
  mois = [mois,'Octobre','Novembre','Decembre']
  ind_jour=dat(2)-1
  jour = string(indgen(31)+1)
  s_jour = widget_droplist( base, value=jour, uvalue = 'JOUR', TITLE='Jour: ')
  widget_control, s_jour,  set_droplist_select = ind_jour
  s_mois = widget_droplist( base, value=mois, uvalue = 'MOIS', TITLE='Mois: ')
  widget_control, s_mois,  set_droplist_select = ind_mois
  nb = 20
  s_nb = rh_cw_nombre( base, MINIMUM=0, MAXIMUM=23, $
         VALUE = nb, UVALUE='nb', TITLE='Heure')
  b = WIDGET_BUTTON(base, UVALUE='FIN', VALUE='OK', xs=100, ys=35)

  WIDGET_CONTROL, base, /REALIZE
  XMANAGER, 'rh_command_ath', base, group=group
  mm =['Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep', 'Oct','Nov','Dec']
  sh = string(format='(i2.2,":00 ",a3," ",i2)', nb, mm(ind_mois), ind_jour+1)
  RETURN, sh
END

;------------------------------------------------
;------------------------------------------------

Pro rh_command, nom, group = group
  

  res = widget_message([' ',' ','Run at a later Time', ' '],$
        /question, title = 'when')

  rh_chmod_fich, nom+".cmd"
         
  txt = ['',"COMMAND file:", "    "+nom+".cmd", $
         '','',"LOG file:", "    "+nom+".bil", '']

  IF res ne "Yes" THEN BEGIN
     rh_execute_cmd, nom+".cmd"
     txt = [txt, ' ', '          Run Now', ' ']
     res = widget_message(txt,/information, title = 'Run')
  ENDIF ELSE BEGIN
     sh = rh_command_ath(group=group)
     print,  "at -f "+nom+".cmd "+sh
     rh_system, "at -f "+nom+".cmd "+sh,s
     txt = [txt, ' Depart : '+sh, ' ',' ']
     rh_system, "at -l",at_job
     txt = [txt,'Liste des jobs en attente (commande at -l):']
     FOR i=0, n_elements(at_job)-1 DO txt = [txt,at_job(i)]
     txt = [txt,' ',' ']
     txt = [txt,'Pour detruire un job en attente',$
           'taper : at -r nom_du_job',' ',' ']
    res = widget_message(txt,/information, title = 'Traitement')  
  ENDELSE
  RETURN
END
  
