'Sendmail.bas - Demonstration Sarian ScriptBasic Program
'Shows:
' function calls, with and without return value
' loop constructs
' conditional branching
' string handling and string replacement function
' date and time functions
' file write
' system calls including saving RAM file to FLASH, email generation and file attachement

function sineplot(sinstart, sinend, sinstep, fileno)
 local i
 for i=sinstart to sinend step sinstep
   print #fileno, space((sin(i)+1.0)*30),"*"
   print #fileno,"\r\n"
 next i
end function

function syscommand(c$, yield)
 local res
 res=system(c$)
 if yield then
   sleep (yield)
 end if 
 ' show result of system command  
 if res then
   print "System command: ",c$,", accepted OK\r\n"
 else
   print "System command: ",c$,", failed!\r\n"
 end if
 syscommand=res
end function

' and now the main code
' log the start time in seconds
start_time=now()
' set up the email template by creating a RAM file called bastemp.eml
open "bastemp.eml" for output as #1
print #1,"\r\n"
print #1,"This is an email automatically generated and sent by Sarian ScriptBasic "
print #1,"running on an IR2420 Router\r\n"
print #1,"The template to the mail was itself generated dynamically \r\n"
print #1,"The local date/time is: ",Day,".",Month,".",Year," ",Hour,":",Minute,":",Sec,"\r\n"
print #1,"And now a short loop...\r\n"
'some loop examples
for x = 1 to 4 
   print #1,"Hello from Sarian ScriptBasic ", x,"\r\n"
next x
while x<8
   x+=1
   print #1,"Hello from Sarian ScriptBasic ",x, "\r\n"
wend
repeat
   print #1,"Hello from Sarian ScriptBasic ",x, "\r\n"
   x+=1
until x>10

print #1, "And now a sine plot \r\n"
' call the sineplot function

sineplot(-pi, pi, pi/20.0, 1)

print #1, "The basic program itself is attached\r\n"

close #1

' maybe also copy the program to a text file
dummy=syscommand("copy sendmail.bas sendmail.txt",5)

'construct the email command
mycmd$="email t.cook@sarian.co.uk sarianbasic@sarian.co.uk \34Basic Test Email\34 bastemp.eml sendmail.bas sendmail.txt"
' actually lets add a CC to someone else
' mycmd$=replace(mycmd$,"support@sarian.co.uk","support@sarian.co.uk;t.cook@sarian.co.uk",1,1)

dummy=syscommand(mycmd$,0)
end_time=now()
elapsed_time=end_time-start_time

print "\r\nFinished in ", elapsed_time, " seconds \r\n"




