proc init { } { 
	global serviceName
	global unityNumber
	set unityNumber "5000"	
    set serviceName "Unity Redirect Service"

    puts "$serviceName Started (26.06.2010)"
}

proc prc_PlayMedia { } {
	global serviceName
	
	puts "$serviceName - prc_PlayMedia start"
	leg setupack leg_incoming
    leg proceeding leg_incoming
    leg connect leg_incoming
	media play leg_incoming flash:unity.wav
	puts "$serviceName - prc_PlayMedia end"
}

proc prc_RedirectToUnity { } {
	global serviceName
	global unityNumber
	
	set dnis [infotag get leg_dnis]
    set ani [infotag get leg_ani]
	
	puts "$serviceName - ani : $ani, dnis : $dnis"
	
	puts "$serviceName - prc_RedirectToUnity start"
	leg setup $unityNumber leg_incoming
    fsm setstate PLACE_CALL
	puts "$serviceName - prc_RedirectToUnity end"
}

proc prc_CallSetupDone { } { 
	global serviceName	

	log "$serviceName - prc_CallSetupDone start"
    set status [infotag get evt_status] 
    set event  [infotag get evt_event]
   
    if { $status != "ls_000"} {
        call close 
    } 
	puts "$serviceName - Status : $status, Event : $event"
	puts "$serviceName - prc_CallSetupDone end"
} 

proc prc_Cleanup { } {
	global serviceName

	puts "$serviceName - prc_Cleanup start"
    set status [infotag get evt_status] 
    set event  [infotag get evt_event]
    
    call close
	puts "$serviceName - Status : $status, Event : $event"
	puts "$serviceName - prc_Cleanup end"
} 

proc prc_Abort { } {
	global serviceName
	set status [infotag get evt_status] 
    set event  [infotag get evt_event]
	
	call close
    log "$serviceName \[ABORT\] - Event : $event, Status :  $status"    
}

proc prc_Empty { } {
	global serviceName
    set status [infotag get evt_status] 
    set event  [infotag get evt_event]
	
    log "$serviceName \[WARNING\] - Event : $event, Status :  $status"    
}
init
#---------------------------------- 
#   State Machine 
#----------------------------------
  set fsm(CALL_INIT,ev_setup_indication) "prc_PlayMedia,MEDIA_WAIT_STATE"
  set fsm(MEDIA_WAIT_STATE,ev_media_done) "prc_RedirectToUnity,REDIRECT_WAIT_STATE"
  set fsm(PLACE_CALL,ev_setup_done) "prc_CallSetupDone,CALL_ACTIVE"
  set fsm(CALL_ACTIVE,ev_disconnected) "prc_Cleanup,same_state"  
  set fsm(any_state,ev_disconnected) "prc_Abort,same_state"   
  set fsm(any_state,ev_any_event) "prc_Empty,same_state"
  fsm define fsm CALL_INIT 