/******************/
/* MaintFiles.ems */
/***************************************/
/* $VER: MaintFiles.ems 1.0 (29.08.93) */
/***************************************/

options results
signal on error
signal on syntax

parse arg area_pattern ask .

if( ~show( 'l', "rexxsupport.library" ) ) then
do
   if( ~addlib( "rexxsupport.library", 0, -30, 0 ) )then
   do
      say "Could not open rexxsupport.library"
      exit 10
   end
end

if( ~show( 'l', "ems_rexx.library" ) ) then
do
   if( ~addlib( "ems_rexx.library", 0, -30, 0 ) )then
   do
      say "Could not open ems_rexx.library"
      exit 10
   end
end


files.0 = 0

/*
** Build the list of pending files.
*/
call EMS_Domains( 'doms' )

do i=1 to doms.0

   call EMS_Flow_Get( doms.i, 'file', 'addr', 'kind' )

   do j=1 to file.0

      if GetFileName( file.j ) ~= '' then call EMS_Add_To_Stem( 'files', file.j, 'UNIQUE' )

   end

end

drop doms. file. addr. kind.


/*
** Scan areas.
*/
call EMS_Areas( 'areas' )
pos = 0
ask = upper( ask )

select
   when ask = 'YES'     then nop
   when ask = 'NO'      then nop
   when ask = 'PRETEND' then nop
   when ask = 'LIST'    then nop
   otherwise                 ask = 'YES'
end

do forever

   pos = EMS_Search_In_Stem( 'areas', area_pattern, pos ); if pos = 0 then leave

   area = areas.pos
   path = GetAreaPath( area )

   if path = '' then iterate

   /*
   ** Don't delete the files with an entry in the database.
   */
   exclude.0 = 0

   if EMS_Area_Msg_DBname( area ) ~= '' then
   do

      call EMS_Area_Item_List_Flagged( area, 'MSG', 'msg', 'FATT' )
      do i=1 to msg.0

         file = EMS_Area_Item_Header_AttachedFile( area, 'MSG', msg.i )      
         if file ~= '' then call EMS_Add_To_Stem( 'exclude', file, 'UNIQUE' )

      end

   end

   if EMS_Area_File_DBname( area ) ~= '' then
   do

      call EMS_Area_Item_List_Flagged( area, 'FILE', 'msg', 'FATT' )
      do i=1 to msg.0

         file = EMS_Area_Item_Header_AttachedFile( area, 'FILE', msg.i )      
         if file ~= '' then call EMS_Add_To_Stem( 'exclude', file, 'UNIQUE' )

      end

   end

   /*
   ** Scan all the files in the area.
   */
   del_list.0 = 0
   dir_files  = showdir( path, 'F' )
   do while length( dir_files ) ~= 0

      parse var dir_files file dir_files

      if file = "EMS_duplicates_database" then iterate

      if EMS_Search_In_Stem( 'exclude', file ) ~= 0 then iterate

      full_file = EMS_Area_FullFileName( area, file )
      if full_file = '' | EMS_Search_In_Stem( 'files', full_file ) ~= 0 then iterate

      call EMS_Add_To_Stem( 'del_list', file, 'UNIQUE' )

   end


   call EMS_Sort_Stem( 'del_list' )

   ask2 = ask
   do i=1 to del_list.0

      if ask2 = 'YES' then
      do

         res = EMS_Do_Request( "Delete '"del_list.i"' from area '"area"'?", 'Yes|No|List|All|None' )

         if res = 'None' then leave
         if res = 'No'   then iterate

         if res = 'All'  then ask2 = 'NO'

         if res = 'List' then ask2 = 'LIST'

      end

      if ask2 = 'LIST' then
      do

         num = 0
         do j=i to del_list.0

            num        = num + 1
            file.num   = del_list.j
            status.num = 'TRUE'

         end
         file.0   = num
         status.0 = num

         res = EMS_Do_Choice_Multi( 'Choose files to delete from' area, 'file', 'status' )

         if res = 'OK' then
         do

            do j=1 to file.0

               if status.j = 'TRUE' then call DeleteOneFile( area, file.j, 'YES' )

            end

         end

         leave

      end
      else
      do

         call DeleteOneFile( area, del_list.i, ask2 )

      end

   end

end

call EMS_FreeScriptData()
exit 0

/*************************************************************/
error:
syntax:

error_text = EMS_LastError()

if error_text = '' then error_text = rc ErrorText( rc )

say '| ***BREAK: error at' sigl error_text

call EMS_FreeScriptData()
exit rc


GetFileName: procedure

   parse arg fullfile

   temp = strip( translate( fullfile, '  ', ':/' ), 'B' )
   len  = words( temp )

   if len <= 1 then return ''

   return word( temp, len )

GetAreaPath: procedure

   parse arg name

   path = ''

   if EMS_Area_File_DBname( name ) ~= '' then
   do
                        path = EMS_Area_File_AltPath( name )
      if path = '' then path = EMS_Area_File_Path(    name )
   end

   if path = '' & EMS_Area_Msg_DBname( name ) ~= '' then
   do
                        path = EMS_Area_Msg_AltPath( name )
      if path = '' then path = EMS_Area_Msg_Path(    name )
   end

   return path

DeleteOneFile: procedure

   parse arg area , file , doit

   full_file = EMS_Area_FullFileName( area, file )

   if full_file = '' then return

   say 'Deleting - area' area ' file' file

   if doit = 'YES' then call DELETE( full_file )

   return
