$input_path = userdir
$sequence_name = 'SSAUTOCAL'
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 MACLIB ********"

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

archive_file($get_path,$put_path,'archive_SSAUTOCAL')
archive_file($get_path,$put_path,'archive_file')

"SS Autocalibration TANCPX and TANCPXFLIP" 

archive_file($get_path,$put_path,'SS_AutoCal_CP')
archive_file($get_path,$put_path,'SS_1_CP')
archive_file($get_path,$put_path,'SS_2_CP')
archive_file($get_path,$put_path,'SS_3_CP')
archive_file($get_path,$put_path,'SS_4_CP')

"SS Autocalibration TANCPX for Ca,CO and ai" 

archive_file($get_path,$put_path,'SS_AutoCal_CP1')
archive_file($get_path,$put_path,'SS_1_CP1')
archive_file($get_path,$put_path,'SS_2_CP1')

"SS Autocalibration ONEPUL and CP"

archive_file($get_path,$put_path,'SS_AutoCal_DP')
archive_file($get_path,$put_path,'SS_1_DP')
archive_file($get_path,$put_path,'SS_2_DP')
archive_file($get_path,$put_path,'SS_3_DP')
archive_file($get_path,$put_path,'SS_4_DP')

"SS Autocalibration DCPTAN for Ca and CO"

archive_file($get_path,$put_path,'SS_AutoCal_DCP')
archive_file($get_path,$put_path,'SS_1_DCP')
archive_file($get_path,$put_path,'SS_2_DCP')
archive_file($get_path,$put_path,'SS_3_DCP')

"Fitting of Nutation Curves for Autocalibration"

archive_file($get_path,$put_path,'SS_AutoCal_minmaxfit')

"Phasing Arrays For Autocalibration "

archive_file($get_path,$put_path,'SS_AutoCal_aph0')
archive_file($get_path,$put_path,'SS_AutoCal_maxamp')

"Probe Template File for AutoCalibration" 

archive_file($get_path,$put_path,'SS_AutoCal_probe.template')
archive_file($get_path,$put_path,'SS_setsolids_DP')
archive_file($get_path,$put_path,'SS_getsolids_DP')
archive_file($get_path,$put_path,'SS_setsolids_CP')
archive_file($get_path,$put_path,'SS_getsolids_CP')
archive_file($get_path,$put_path,'SS_setzero_CP')
archive_file($get_path,$put_path,'SS_installprobe_ALL')
archive_file($get_path,$put_path,'SS_saveAutoTripRes_ALL')

"BioPack macros Directly used for Autocalibration"

archive_file($get_path,$put_path,'BPgetdate')
archive_file($get_path,$put_path,'BPpage')
archive_file($get_path,$put_path,'BPsvf')
archive_file($get_path,$put_path,'BPsetparams')

"BioPack Setup macros used for BPpresent='y'" 

archive_file($get_path,$put_path,'tancpx_HC')
archive_file($get_path,$put_path,'tancpx_HN')
archive_file($get_path,$put_path,'onepul_HC')
archive_file($get_path,$put_path,'onepul_HN')
archive_file($get_path,$put_path,'onepulH1_HC')
archive_file($get_path,$put_path,'onepulH1_HN')
archive_file($get_path,$put_path,'tancpx_Ca')
archive_file($get_path,$put_path,'tancpx_CO')
archive_file($get_path,$put_path,'tancpx_Nai')
archive_file($get_path,$put_path,'tancpx_Nam')
archive_file($get_path,$put_path,'tancpxt1rho_Ca')
archive_file($get_path,$put_path,'tancpxt1rho_CO')
archive_file($get_path,$put_path,'tancpxt1rho_Nai')
archive_file($get_path,$put_path,'dcptan_HNC')
archive_file($get_path,$put_path,'dcptan_HNCa')
archive_file($get_path,$put_path,'dcptan_HNCO')

"BioPack macros used for Autocalibration for BPpresent='y'"

archive_file($get_path,$put_path,'BPrtppar')
archive_file($get_path,$put_path,'BPgetpars')
archive_file($get_path,$put_path,'BPfixup')
archive_file($get_path,$put_path,'BP_SS_update_from_probefile')
archive_file($get_path,$put_path,'BPcheckprobefile')
archive_file($get_path,$put_path,'BP_SS_getsrate')

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

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

archive_file($get_path,$put_path,'SS_AutoCal_CP')
archive_file($get_path,$put_path,'SS_AutoCal_DP')

archive_file($get_path,$put_path,'SSpreface_Ca_CP')
archive_file($get_path,$put_path,'SSpreface_CO_CP')
archive_file($get_path,$put_path,'SSpreface_Nai_CP')
archive_file($get_path,$put_path,'SSpreface_Nam_CP')
archive_file($get_path,$put_path,'SSpreface_Ca_CP1')
archive_file($get_path,$put_path,'SSpreface_CO_CP1')
archive_file($get_path,$put_path,'SSpreface_Nai_CP1')
archive_file($get_path,$put_path,'SSpreface_Nam_CP1')
archive_file($get_path,$put_path,'SSpreface_Ca_DCP')
archive_file($get_path,$put_path,'SSpreface_CO_DCP')

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

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

archive_file($get_path,$put_path,'onepul_C13.par')
archive_file($get_path,$put_path,'onepul_N15.par')
archive_file($get_path,$put_path,'tancpx_C13.par')
archive_file($get_path,$put_path,'tancpx_N15.par')
archive_file($get_path,$put_path,'tancpxt1rho_C13.par')
archive_file($get_path,$put_path,'tancpxt1rho_N15.par')
archive_file($get_path,$put_path,'dcptan_HNC.par')

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

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')





