/***************************************************************************/
/*                                                                         */
/* FileHatch.spot       ©1993/94 Stefan Zeiger       2:244/6302.15@fidonet */
/*                                                                         */
/* $VER: FileHatch.spot 1.6 (30.5.94)                                      */
/*                                                                         */
/* [Based roughly on SkyHatch.spot by Stu Churchill]                       */
/*                                                                         */
/* This script will hatch a file using FHatch or SkyTick and post an       */
/* announcement in the appropriate area.                                   */
/*                                                                         */
/* Yes! Hatching a file is really *that* easy! ;)                          */
/*                                                                         */
/***************************************************************************/
/*                                                                         */
/* Instructions: - Install FTick/FHatch or SkyTick, if you haven't done    */
/*                 this already.                                           */
/*                                                                         */
/*               - Modify the settings below. All variables' meanings      */
/*                 should be quite obvious                                 */
/*                                                                         */
/*               - Add FileHatch.spot in Spot's ARexx menu or start it     */
/*                 directly via rx.                                        */
/*                                                                         */
/*               - Have fun with this script! ;)                           */
/*                                                                         */
/***************************************************************************/
/*                                                                         */
/* 20.09.1993  1.0    First release                                        */
/* 21.09.1993  1.1    Largely enhanced                                     */
/* 24.09.1993  1.2    Hatched file is now copied with 'clone'              */
/*             1.3    FileHatch can now use both FHatch and SkyTick        */
/*             1.4    Generic list requester procedures added              */
/* 25.09.1993  1.5    Bugs fixed                                           */
/* 30.05.1994  1.6    Long file description creation added                 */
/*                                                                         */
/***************************************************************************/


fhatch_path   = 'mail:bin/FHatch'
ftick_cfg     = 'mail:tick.cfg'

skytick_path  = 'mail:skytick/SkyTick'
skytick_cfg   = 'mail:sky'				/*  -> mail:skyTICK.CFG */

used_program  = 'fhatch'				/* [fhatch|skytick|none] */

from_name     = 'Stefan Zeiger'
host_name     = '2:244/6302.15'
to_name       = 'All'
subj_line     = 'File Arrival Announcement'
origin        = 'FileHatch.spot, (c) 1993/94 by Stefan Zeiger'
ldesc_quote   = '   '
use_origin    = 'yes'

temp_dir      = 'T:'					/* Must end in ':' or '/' */
spot_path     = 'sys:comm/spot/Spot'
editor_path   = 's:keepced'

file_path     = 'outgoing:'
keep_original = 'yes'

call newlist()
call addlist('SPOT_BETA')
call addlist('UPLOAD.ALLG')
call addlist('UPLOAD.AMIGA')
call addlist('UPLOAD.DTP')
call addlist('UPLOAD.MOD')
call addlist('UPLOAD.PIX')
call addlist('UPLOAD.RAY')


/***************************************************************************/
/* Set up system                                                           */
/***************************************************************************/

options results

CR = '0d'x

temp_file = temp_dir'FileHatch.spot.TMP'
desc_file = temp_dir'FileHatch.spot.DESC'

if ~show(l, 'rexxsupport.library') then do
  if ~addlib('rexxsupport.library', 0, -30) then do
    say 'Please install the rexxsupport.library in your libs: directory'
  end
end

if ~show('ports','SPOT') then do
  address command spot_path
  'WaitForPort SPOT'
  if rc = 5 then do
    call cleanup()
  end
  quit = 'yes'
end

'isiconified'
if rc = 0 then do
  icon = 'yes'
  'uniconify'
end


/***************************************************************************/
/* Ask for the file, area and description                                  */
/***************************************************************************/

address spot

'requestfile title "Hatch file..." path 'file_path
if rc>0 then call cleanup()
hatchfile=result

filearea_name=picklist("Hatch in...")
if filearea_name='CANCELLED' then call cleanup()
if filearea_name='' then call cleanup()

'requestarea title "Announce in..."'
if rc>0 then call cleanup()
echo_name=result

address command
call statef(hatchfile)
parse var result dummy filelength dummy dummy dummy dummy dummy description
description=substr(description,2)

address spot

'requeststring title "Description..." prompt "Please enter the description" center default "'description'"'
if rc>0 then call cleanup()
description=result

'requestresponse title "FileHatch request" prompt "Do you want to enter a long description?'||CR||CR||'(If you enter a description, you should'||CR||'not use more than 60 characters per line)" gadgets "_Yes|_No" center'
if rc>0 then do
  use_desc='yes'
  address command
  editor_path desc_file
end
else use_desc='no'


/***************************************************************************/
/* Create the announcement                                                 */
/***************************************************************************/

address command

call delete(temp_file)
hatchdate=date()', 'time()
separator=lastpos('/',hatchfile)
if separator=0 then separator=lastpos(':',hatchfile)
if separator=0 then hatchfile_base=hatchfile
else hatchfile_base=substr(hatchfile,separator+1)

call open('TEMP',temp_file,'W')
call writeln('TEMP','')
call writeln('TEMP','')
call writeln('TEMP',' The following file has just been hatched at 'host_name' :')
call writeln('TEMP','')
call writeln('TEMP','')
call writeln('TEMP',' File name   : 'hatchfile_base)
call writeln('TEMP',' Length      : 'filelength)
call writeln('TEMP',' Description : 'description)
call writeln('TEMP',' File area   : 'filearea_name)
call writeln('TEMP',' Origin      : 'host_name)
call writeln('TEMP',' Date, Time  : 'hatchdate)
if use_desc='yes' then do
  call writeln('TEMP','')
  call writeln('TEMP',' Long descr. :')
  call writeln('TEMP','')
  call open('DESC',desc_file,'R')
    descline=readln('DESC')
    do until EOF('DESC')
      call writech('TEMP',ldesc_quote)
      call writeln('TEMP',descline)
      descline=readln('DESC')
    end
  call close('DESC')
end
call writeln('TEMP','')
call writeln('TEMP','')
call writeln('TEMP',' Information supplied by FileHatch.spot 1.6  (c) 1993/94 by Stefan Zeiger')
call writeln('TEMP','')
call close('TEMP')


/***************************************************************************/
/* Hatch the file                                                          */
/***************************************************************************/

select
  when used_program='skytick' then call hatch_with_skytick()
  when used_program='fhatch' then call hatch_with_fhatch()
  otherwise nop
end


/***************************************************************************/
/* Write the file announcement message into the specified area             */
/***************************************************************************/

address spot
'lockgui'
'arealist'
'gotoarea NAME 'echo_name
if rc ~= 0 then do
  'requestnotify PROMPT "Could not find area'||CR||echo_name'"'
  call cleanup()
end
'messagelist'
if use_origin='yes' then 'write TO "'to_name'" FROM "'from_name'" SUBJECT "'subj_line'" FILE 'temp_file' ORIGIN "'origin'" NOEDIT NOGUI NOSIG'
else 'write TO "'to_name'" FROM "'from_name'" SUBJECT "'subj_line'" FILE 'temp_file' NOEDIT NOGUI NOSIG'
'arealist'
'unlockgui'


/***************************************************************************/
/* Exit gracefully                                                         */
/***************************************************************************/

if icon = 'yes' then do
  'iconify NOREQ'
end

if quit = 'yes' then do
  'quitspot NOREQ'
end

call delete(temp_file)
call delete(desc_file)
call deletelist()

call cleanup()


/***************************************************************************/
/* Hatch the file using FHatch                                             */
/***************************************************************************/

hatch_with_fhatch:

  address command

  if keep_original='yes' then 'copy 'hatchfile' to 'temp_dir||hatchfile_base' clone'

  fhatch_path' "'ftick_cfg'" "'hatchfile'" 'filearea_name' "'description'" >CON:40/40/590/100/FHatch/AUTO/CLOSE/DONOTWAIT/SCREENSPOT'

  if keep_original='yes' then do
    'copy 'temp_dir||hatchfile_base' to 'hatchfile' clone'
    call delete(temp_dir||hatchfile_base)
  end

  if rc ~= 0 then do
    address spot
    'requestnotify PROMPT "Could not hatch file'||CR||hatchfile'"'
    call cleanup()
  end

return


/***************************************************************************/
/* Hatch the file using SkyTick                                            */
/***************************************************************************/

hatch_with_skytick:

  address command

  if keep_original='yes' then skytick_path' "-C'skytick_cfg'" "-F'hatchfile_base'" "-R'hatchfile_base'" "-P'file_path'" "-A'filearea_name'" "-D'description'" -H HATCH >CON:40/40/590/100/SkyTick/AUTO/CLOSE/DONOTWAIT/SCREENSPOT'
  else skytick_path' "-C'skytick_cfg'" "-F'hatchfile'" "-R'hatchfile'" "-A'filearea_name'" "-D'description'" -H -Q HATCH >CON:40/40/590/100/SkyTick/AUTO/CLOSE/DONOTWAIT/SCREENSPOT'

  if rc ~= 0 then do
    address spot
    'requestnotify PROMPT "Could not hatch file'||CR||hatchfile'"'
    call cleanup()
  end

return


/***************************************************************************/
/* A generic list requester                                                */
/***************************************************************************/

newlist: procedure
  address command
  'makedir t:list-requester >NIL:'
return


deletelist: procedure
  address command
  'delete t:list-requester all >NIL:'
return


addlist: procedure
  parse arg name
  address command
  'echo "" noline >"t:list-requester/'name'"'
return


picklist: procedure
  parse arg titlestring
  address spot
  'requestfile title "'titlestring'" path t:list-requester'
  name=result
  if rc>0 then name='CANCELLED'

  separator=lastpos('/',name)
  if separator=0 then separator=lastpos(':',name)
  if separator=0 then basename=name
  else basename=substr(name,separator+1)

return basename


/***************************************************************************/
/* Clean up                                                                */
/***************************************************************************/

cleanup: procedure
  address spot
  'unlockgui'
  call deletelist()
  exit
return
