$input_path = userdir
$sequence_name = 'VNMRSSOLIDSDOCS2'
format($sequence_name,'upper'):$name_upper
format($sequence_name,'lower'):$name_lower

$archive_path =  userdir + '/ARCHIVE'
$output_path = $archive_path + '/' + $name_upper
$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)

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

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

archive_file($get_path,$put_path,'SolidsPack.README')
archive_file($get_path,$put_path,'vnmrssolids2.README')

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

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

archive_file($get_path,$put_path,'archive_ALSOLIDSPACK')
archive_file($get_path,$put_path,'archive_VNMRSSOLIDSDOCS2')

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

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

"Manual Pages for Pulse Sequence Development" 

archive_file($get_path,$put_path,'AllParGroups')
archive_file($get_path,$put_path,'AllSeqTitles')
archive_file($get_path,$put_path,'SolidsPack')
archive_file($get_path,$put_path,'SolidsPackChanges')

"******** Create TEMPLATES/VNMRJ/INTERFACE **********"

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

archive_file($get_path,$put_path,'SolidStateExpts.xml')
archive_file($get_path,$put_path,'SolidStateExpts21b.xml')
archive_file($get_path,$put_path,'SolidStateExpts21b.xml')
archive_file($get_path,$put_path,'Solids1DExpts.xml')
archive_file($get_path,$put_path,'HX2DExpts.xml')
archive_file($get_path,$put_path,'HXYExpts.xml')
archive_file($get_path,$put_path,'MultipulseExpts.xml')
archive_file($get_path,$put_path,'QuadrupoleExpts.xml')
archive_file($get_path,$put_path,'ProtocolLabels.xml')   "Needed for 2.2C only" 

"******** Create ADM/USERS **********"

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

archive_file($get_path,$put_path,'protocolListWalkup.xml')  "Available for 2.1B only" 

"******** Create ADM/USERS/USERPROFILES **********"
"          carries over from adm/users            "

$get_path = $input_path + '/adm/users/userProfiles'
$put_path = $put_path +'/userProfiles'
shell('mkdir ' + $put_path)

archive_file($get_path,$put_path,'AllSolids2.txt')  "Available for 2.2C Only"
archive_file($get_path,$put_path,'AllSolids2.xml')  "Available for 2.2C Only"

"******** 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
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 + ' manual'
$directories = $directories + ' -C ' + $output_path + ' templates'
$directories = $directories + ' -C ' + $output_path + ' README'
$directories = $directories + ' -C ' + $output_path + ' adm'

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') // README not needed here. 
 
write('alpha','ARCHIVE COMPLETE')
write('line3','ARCHIVE COMPLETE')





