$input_path = userdir
$sequence_name = $1
format($sequence_name,'lower'):$name_lower
substr($name_lower,1,1):$lower_character
format($sequence_name,'upper'):$name_upper
substr($name_upper,1,1):$upper_character
length($1):$length
substr($sequence_name,2,$length - 1):$last_characters
$name_protocol = $upper_character + $last_characters
$name_lower = $lower_character + $last_characters

$archive_path =  userdir + '/ARCHIVE'
$output_path = $archive_path + '/' + $name_protocol
$backup_path = 'none'

exists($archive_path,'directory'):$e
if ($e < 0.5) then 
   shell('mkdir ' + $archive_path)
endif
   
exists($output_path,'directory'):$e1
if ($e1 > 0.5) then
   $i = 0
   $j = 0
   while (($i< 20) and ($j<20)) do 
      $j = $j + 1
      $i = $i + 1
      $istring=''
      format($i,1,0):$istring
      $backup_path = $output_path + '_' + $istring
      exists( $backup_path,'directory'):$e2
      if ($e2 > 0.5) then 
         if ($i = 20) then
            write('error','Too Many Backups: Abort')
            abort 
         endif
      else 
         shell('mv ' + $output_path + ' ' + $backup_path)
         write('error','Backup %s created',$istring)
         $i = 20
      endif
   endwhile
endif

shell('mkdir ' + $output_path)

"******** Archive Filenames that are Arguments 2 to $# *********" 

$index = 2
$index2 = 1
$indexstring = ''
$value = '' 
$pathkey = 'PKM' 
while ($index <= $#) do
   format($index,1,0):$indexstring 
   $indexstring = '$' + $indexstring
   $value = {$indexstring}
   substr($value,1,2):$pathstring
   if ($pathstring = 'PK') then
      $pathkey = $value
   else
      $file_array[$index2] = $value
      $key_array[$index2] = $pathkey
      archive_file($input_path,$output_path,$file_array[$index2],$key_array[$index2])
      $index2 = $index2 + 1
   endif
   $index = $index + 1
endwhile

"******** Create MACLIB ********"

$get_path = $input_path + '/maclib'
$put_path = $output_path +'/maclib'
shell('mkdir ' + $put_path)

archive_file($get_path,$put_path,$name_lower + '_pars')
archive_file($get_path,$put_path,$name_protocol)

"******** Create PSGLIB ********"

$get_path = $input_path + '/psglib'
$put_path = $output_path +'/psglib'
shell('mkdir ' + $put_path)

archive_file($get_path,$put_path,$name_lower + '.c')

"******** Create PARLIB ********"

$get_path = $input_path + '/parlib'
$put_path = $output_path +'/parlib'
shell('mkdir ' + $put_path)

archive_file($get_path,$put_path,$name_protocol + '.par')

"******** Create MANUAL *********************"

$get_path = $input_path + '/manual'
$put_path = $output_path +'/manual'
shell('mkdir ' + $put_path)

archive_file($get_path,$put_path,$name_lower)

"******** Create TEMPLATES/LAYOUT ***********"

$get_path = $input_path + '/templates/layout'
$put_path = $output_path +'/templates'
shell('mkdir ' + $put_path)
$put_path = $put_path +'/layout'
shell('mkdir ' + $put_path)

archive_file($get_path,$put_path,$name_lower)

"******** Create TEMPLATES/VNMRJ/PROTOCOLS ********VJ***"

$get_path = $input_path + '/templates/vnmrj/protocols'
$put_path = $output_path +'/templates/vnmrj'
shell('mkdir ' + $put_path)
$put_path = $put_path +'/protocols'
shell('mkdir ' + $put_path)

archive_file($get_path,$put_path,$name_protocol + '.xml')

"******** Create README ********"

$get_path = $input_path + '/README'
$put_path = $output_path +'/README'
shell('mkdir ' + $put_path)

archive_file($get_path,$put_path,$name_lower+'.README')

"******** Add Current .DATE file ********"

$get_path = $backup_path
$put_path = $output_path 
$date_name = $name_lower + '.DATE'
$date_file = $backup_path + '/' + $date_name

exists($date_file,'file'):$e4
if ($e4 > 0.5) then
   archive_file($backup_path, $output_path, $date_name)
else
   $date_file = $output_path + '/' + $date_name
   write('file',$date_file,'%s:',$name_upper)
   endif
$date_file = $output_path + '/' + $date_name
$date='' shell('date +%Y%m%d%H%M%S'):$date
write('file',$date_file,'LAST ARCHIVE DATE: %s  %s',$date, userdir)

   
"******** Add a TAR file of the directories ********"

$tar_file = $output_path + '/' + $name_lower + '.tar'
$directories = ''
$directories = $directories + ' -C ' + $output_path + ' maclib'
$directories = $directories + ' -C ' + $output_path + ' psglib'
$directories = $directories + ' -C ' + $output_path + ' parlib'
$directories = $directories + ' -C ' + $output_path + ' execpars'
$directories = $directories + ' -C ' + $output_path + ' wavelib'
$directories = $directories + ' -C ' + $output_path + ' templates'
$directories = $directories + ' -C ' + $output_path + ' psg' 
$directories = $directories + ' -C ' + $output_path + ' shapelib' 
$directories = $directories + ' -C ' + $output_path + ' manual'
$directories = $directories + ' -C ' + $output_path + ' wtlib'
$directories = $directories + ' -C ' + $output_path + ' manualpp'
$directories = $directories + ' -C ' + $output_path + ' Simplexer'
$directories = $directories + ' -C ' + $output_path + ' bin'
$directories = $directories + ' -C ' + $output_path + ' README'

shell('tar cfz ' + $tar_file + '.Z ' + $directories)

$expoutput_path = $archive_path + '/EXP_OUTPUT'
exists($expoutput_path,'directory'):$e
if ($e < 0.5) then 
   shell('mkdir ' + $expoutput_path)
endif
$readme_file = $output_path + '/README/' + $name_lower + '.README'
shell('cp ' + $tar_file + '.Z  ' + $expoutput_path + '/' + $name_lower + '.tar.Z') 
shell('cp ' + $readme_file + ' ' + $expoutput_path + '/' + $name_lower + '.README')
 
write('alpha','ARCHIVE COMPLETE')
write('line3','ARCHIVE COMPLETE')





