/* Event   1008, Changed */

/*Arg window self*/
/*gravar con la creacion o modificacion del nuevo porg*/
/*usado por MODIFY Y CREATION*/
call refresca_discos

intro=x2c('0D0A')
i=0
Lin.0=0
newi=0
/*recoger datos a crear*/
nameprog=vpgetitemvalue(window,1007)
/*say nameprog length(nameprog)*/
savein=vpgetitemvalue(window,1001)
/*say savein*/

/*verificar la entrada correcta de datos*/
if nameprog = "" then
do
response=VpMessageBox(window,'Error in name program!','The name of program can not be null')
return
end
else
do
	if length(nameprog)<7 then
	do
	
	end
	

end


if savein = "" then
do
response=VpMessageBox(window,'Error in save in path!','The name of the path can not be null')
return
end
if substr(savein,length(savein),1)='\' then
do
savein=substr(savein,1,length(savein)-1)
end


/*verificar si existe el fichero a crear*/
/***quitar solo para modificar**ini**/
newp=directory()||"\TheBacks\"||nameprog||".cmd"
if modiocre='1008' then 
do
    
     /*say newp 'newp'*/
     call SysFileTree newp,"file","f"
     do index = 0 to file.0
       if file.0 >< 0 then 
       do
       response=VpMessageBox(window,'This program is repeat!!','Select a diferent name for your program, found this program in:' newp)
       return
       end /* end do */
     end
end     
/***quitar solo para modificar**fin**/

/*incluir ficheros list books*/
regn=VpGetItemCount(window, 1009) 
do i= 1 to regn
incluir.i=vpgetitemvalue(window,1009,i)
incluir.0=i
/*say incluir.i*/
end /* end do */

/*excluir ficheros container*/
regn=VpGetItemCount(window,modiocre)
/*say regn '1008'*/
CALL VpSelect window,modiocre, 'ALL', 1  
CALL VpGetIndex window,modiocre,'ALLSELECTED','INDEXES.' 
do i= 1 to regn
excluir.i=vpgetitemvalue(window,modiocre,indexes.i)
excluir.0=i
end 
if regn=0 then
do
excluir.0=0
excluir.1=''
end

/*SAY excluir.0 excluir.1 excluir.2*/

/*leer los datos basicos del inirar*/
rarprog = SysIni(progini, "rarvalues", "rarprog")
if rarprog = "ERROR:" then rarprog = ""
compres = SysIni(progini, "rarvalues", "compres")
if compres = "ERROR:" then compres = ""
properti = SysIni(progini, "rarvalues", "properti")
if properti = "ERROR:" then properti = ""

if rarprog='' | compres="" | properti="" then
do
response=VpMessageBox(window,'The compressor program need more values','(the program will be created but do not work ok) Do you want continue? ','YESNO')
end

if response='NO' then return /* retorna a la ficha de nuevo */
/*YES - continuar */
/* generar el fichero *.cmd */

Lin.1="/*"nameprog".cmd*/"
Lin.2="nameprog="'"'nameprog'"'
Lin.3="call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'"
Lin.4="call SysLoadFuncs"
Lin.5="numeric digits(15)"
Lin.6="initime=time('S') ; hoy=date('B')"
Lin.7="signal on error"
newp=directory()||"\TheBacks\"||nameprog||".cmd"
namecorto=nameprog
if length(nameprog)>5 then
do
namecorto=substr(nameprog,1,5)
end
Lin.8="A_CREAR = SYSTEMPFILENAME('" || savein || "\" || namecorto || "???')"
Lin.9="intro=x2c('0D0A')"
Lin.10=""
Lin.11="mkdir A_CREAR"
do i=12 to 19
Lin.i=""
end /* end do */

Lin.20="if rc=0 then"
Lin.21="do"
newi=22
mas='||'
if incluir.0='INCLUIR.0' then incluir.0=0
if incluir.0=0 then
do
/*no se generara nada por no tener datos*/
response=VpMessageBox(window,'Error, no select','There are not nothing to backup')
return
end
   do i = 1 to incluir.0 by 1
   newi=newi+1
   if incluir.0=i then mas=''
	if i=1 then
	do
	Lin.newi="positiv=" '"' || incluir.i ||'"intro' mas','
	end
        else
        do
        Lin.newi='"' || incluir.i ||'"intro' mas','
        end
   end /* end do */

newi=newi+1   
Lin.newi="/* no quitar este comentario, o suprimir la ultima coma*/" /*mantener este espacio*/
newi=newi+1
lin.newi='call crear_tempin /* genera un fichero temporal con los files a incluir */'
mas='||'
     do i = 1 to excluir.0 by 1
     newi=newi+1
     if excluir.0=i then mas=''

	if i=1 then
	do
	Lin.newi="negativ=" '"' || excluir.i ||'"intro' mas','
	end
        else
        do
        Lin.newi='"' || excluir.i ||'"intro' mas','
        end
     end /* end do */
newi=newi+1
Lin.newi="/* no quitar este comentario, o suprimir la ultima coma*/" 
newi=newi+1
Lin.newi='call crear_tempout /* genera files a excluir */     '
A_CREAR='A_CREAR'
newi=newi+1
Lin.newi="'"rarprog compres "'" A_CREAR || "'"'\'|| substr(nameprog,1,7) "'," 
newi=newi+1
este='@'||nameprog||'Tempin.txt'
Lin.newi="'"este"',"
newi=newi+1
Lin.newi="'" properti "',"
newi=newi+1
este='-x@'||nameprog||'Tempout.txt'
Lin.newi="'"este"'"
newi=newi+1
Lin.newi="call errorRar(rc) "
newi=newi+1
Lin.newi="end"
newi=newi+1
Lin.newi="else"

newi=newi+1
Lin.newi="do"
newi=newi+1
Lin.newi="say 'Do not created the path, abort the copy 'nameprog intro 'Error operation canceled'  "
newi=newi+1
Lin.newi="end"
newi=newi+1
call generador_files /*carga la variable rutinas */
Lin.newi=rutinas
newi=newi+250 /*********** ojoj este es el numero de lineas para el generador_files**/
/*gravarlo too*/
call save_prog_too

CALL VpWindow window,'CLOSE' 


return

exit

generador_files:
rutinas='exit'intro,
'/*****error***********/'intro,
'error:'intro,
" call errorRar(rc)"intro,
"call mostrar_tiempo"intro,
" say 'Push any key...'"intro,
'pull'intro,
'return'intro,
" /*****errorRar***********/"intro,
"  errorRar:"intro,
"  arg rec"intro,
"select"intro,
"  when rec=255 then do ; err='   USER BREAK(255)  User stopped the process';end"intro,
"  when rec= 9  then do ; err='  CREATE ERROR(9)   Create file error';end"intro,
"  when rec= 8  then do ; err=' MEMORY ERROR(8)    Not enough memory for operation';end"intro,
"  when rec= 7  then do ; err=' USER ERROR(7)      Command line option error';end"intro,
"  when rec= 6  then do ; err=' OPEN ERROR(6)      Open file error';end"intro,
"  when rec= 5  then do ; err=' WRITE ERROR(5)     Write to disk error';end"intro,
'  when rec= 4  then do ; err=" LOCKED ARCHIVE(4)  Attempt to modify an archive previously locked by the -k- command";end'intro,
'  when rec= 3  then do ; err=" CRC ERROR(3)       A CRC error occurred when unpacking";end'intro,
'  when rec= 2  then do ; err=" FATAL ERROR(2)     A fatal error occurred";end'intro,
'  when rec= 1  then do ; err=" WARNING(1)         Non fatal error(s) occurred";end'intro,
'  when rec= 0  then do ; err=" SUCCESS(0)         Successful operation (User exit)";end'intro,
'  when rec=-1  then do ; err="LAN:(-1)            No password specified for user ID.";end'intro,
'  when rec=-2  then do ; err="LAN:(-2)            Network logon was unsuccessful.";end'intro,
'  otherwise do'intro,
"  err='Unknown error with number ' rec ; end"intro,
"end"intro,
"say err 'in program 'nameprog'.cmd'"intro,
"if rec=0 then "intro,
"do"intro,
"call mostrar_tiempo"intro,
"  say 'Push any key...'"intro,
" pull"intro,
"end"intro,
"erase nameprog||'Tempin.txt'"intro,
"erase nameprog||'Tempout.txt'"intro,
"return"intro,
'/*****crear_tempin********/'intro,
'crear_tempin:'intro,
'arg nada'intro,
"filename=directory()||'\'||nameprog||'Tempin.txt'"intro,
"signal off error"intro,
"erase filename"intro,
"if rc=2 | rc=1 then rc=0"intro,
"signal on error"intro,
"/*se posiciona en la primera linea para escribir*/"intro,
"rc=LINEOUT(filename,,1)"intro,
"rc=LINEOUT(filename,translate(positiv),1)"intro,
"	if rc<>0  then"intro,
"	do"intro,
"	say 'Bad file save *'nameprog||'Tempin.txt*, with rc= 'rc intro'Error saving file'"intro,
"	end"intro,
"rc=Stream(FileName,'c','close')"intro,
"return nada"intro,
" "intro,
'/*****crear_tempout********/'intro,
"crear_tempout:"intro,
"arg nada"intro,
"if negativ='NEGATIV' then negativ=''"intro,
"filename=directory()||'\'||nameprog||'Tempout.txt'"intro,
"signal off error"intro,
"erase filename"intro,
"if rc=2 | rc=1 then rc=0"intro,
"signal on error"intro,
"/*se posiciona en la primera linea para escribir*/"intro,
"rc=LINEOUT(filename,,1)"intro,
"rc=LINEOUT(filename,translate(negativ),1)"intro,
"	if rc<>0  then"intro,
"	do"intro,
"	say 'Bad file save *'nameprog||'Tempout.txt*, with rc= 'rc intro'Error saving file'"intro,
"	end"intro,
"rc=Stream(FileName,'c','close')"intro,
"return nada"intro,
"/******* calcular tiempo ejecucion ********/"intro,
"mostrar_tiempo:"intro,
"endtime=time('S')"intro,
"manana=date('B')"intro,
"if initime>endtime then"intro,
"do"intro,
"total=initime /*mas de un dia */"intro,
"end"intro,
"else"intro,
"do"intro,
"total=endtime-initime"intro,
"end"intro,
"dias=manana-hoy"intro,
"horas=(total//(24*60*60))/60%60"intro,
"minutos=(total//(24*60*60))/60//60%1"intro,
"segundos=(total//(24*60*60))/60//60//1*60%1"intro,
"Say 'Tiempo d:h:m:s = 'dias':'horas':'minutos':'segundos"intro,
"return nada"intro,
"exit"intro,
"/******end program automatic generation*******/"intro,
"/********************************************************/"intro
return rutinas







save_prog_too:
filename=newp
/*se posiciona en la primera linea para escribir*/
rc=LINEOUT(filename,,1)
do i=1 to newi
if lin.i="LIN."||i then Lin.i=""
IF lin.i="" then ITERATE
rc=Lineout(FileName,Lin.i)
/*say i lin.i*/
	if rc<>0  then
	do
	response=VpMessageBox(window,'Error saving file','Bad file save 'nameprog'.cmd, with rc= 'rc)	
	end
end /* end do */

/*aadido para eliminar las lineas de basura del anterior programa */
/*pone a blanco las lineas de basura*/
do while lines(filename)<>0
rc=lineout(filename,'')
end /* end do */



rc=Stream(FileName,'c','close')
/*rc=Lineout(Filename)*/
lc=0
Lin.0=0

return
