#!/bin/bash

# 2017.10.16  v1.5.0
#  bumped version to 1.5.0  (for bias file change below)

# 2015.12.28
#  adding bias0 files for acis_build_badpix use
#  (also requires pbk0)

# 2014.07.28 -   1.4.2
#  no longer an error to not have a pbk0 file; just give a warning and continue.
#

# 2014.09.26
# 
# changed the names of asol file symlinks, to include a leading "0", so that
# the time-order is preserved in the lexical order.

# 2013.01.14 dph - v1.4.0
#
#  some minor fixes to help prevent failure if one has littered the
#  distribution directories with other files.  Make the file-matching
#  strings for a bit longer, w/ hrc or acis prefix and _<type>.fits
#  This will prevent some failures if one leaves a non-standard-name
#  _<type>.fits file in that directory.  (This is most likely to be a
#  custom, saved evt1, but include other types as well).

# 2010.11.01 dph  -  v1.3.0
#  added symlink for mtl1 file, now required by ciao 4.3.

# 2008.08.05   version 1.2
#  Arik's mods for interleaved mode.

# 2008.08.01 - v1.1.1
#  switch to bash, since no pushd in sh on solaris.


# 2007.10.18 dph  add conditional tests for file existence.
#                 Issue warning for missing files.
#                 No test for hrc-only or acis-only files.

# 2007.08.08 dph

### link archive distribution files to generic names for use by
### processing scripts.

#
# USAGE:  setup_obsdir d1[ d2[ d3[...]]]
# EXAMPLES:  
# setup_obsdir .
# setup_obsdir Data/obs_5482 Data/obs_6282
#

for i in $*

  do 

  echo $i ..............

  pushd $i

  gunzip primary/*.gz secondary/*.gz

  #
  # check for interleave mode. Occurs when there
  # is *e1_evt1.fits file
  #
  predir="."
  rootdir="."
  interleave=0

  if [ -e secondary/acis*N*e1_evt1.fits ] ; then
      echo "INFO: Interleave mode detected..."
      i=`echo $i | sed 's/\/$//'`
      mkdir ${i}_e1 ${i}_e2
      predir="${i}_e1 ${i}_e2"
      rootdir=".."
      interleave=1
      iter=1
  fi

  for subdir in $predir
  
  do
    
    if [ $interleave -eq 1 ] ; then
	ifix=e${iter}_
    else
	ifix=""
    fi
    subdir=$subdir/
    
    pushd $subdir
    
    if [ -e   ${rootdir}/secondary/[ah][cr][ic]*_*N*_${ifix}evt1.fits ] ;   then
	ln -s ${rootdir}/secondary/[ah][cr][ic]*_*N*_${ifix}evt1.fits  evt0
    else
	echo 'WARNING: no evt1 file!'
	exit 1
    fi

    if [ -e   ${rootdir}/secondary/[ah][cr][ic]*_*N*_${ifix}flt1.fits ];   then
	ln -s ${rootdir}/secondary/[ah][cr][ic]*_*N*_${ifix}flt1.fits  flt1
    else
	echo 'WARNING: no flt1 file!'      
    fi

    if [ -e   ${rootdir}/secondary/[ah][cr][ic]*_*N*_${ifix}mtl1.fits ];   then
	ln -s ${rootdir}/secondary/[ah][cr][ic]*_*N*_${ifix}mtl1.fits  mtl1
    else
	echo 'WARNING: no mtl1 file!'      
    fi

    
    if [ -e   ${rootdir}/secondary/[ah]*N*_${ifix}msk1.fits ] ;   then
	ln -s ${rootdir}/secondary/[ah]*N*_${ifix}msk1.fits  msk1
    else
	echo 'WARNING: no msk1 file!'
    fi


    # only for acis
    #
    if [ -e   ${rootdir}/secondary/acis*N*_${ifix}stat1.fits ] ;   then
	ln -s ${rootdir}/secondary/acis*N*_${ifix}stat1.fits stat1
    fi   

    

    # NOTE: bpix1 is in primary for acis, secondary for hrc:
    #
    if [ -e stat1 ] ; then
	if [ -e   ${rootdir}/primary/acis*N*_${ifix}bpix1.fits ] ;   then
	    ln -s ${rootdir}/primary/acis*N*_${ifix}bpix1.fits  bpix1
	else
	    echo 'WARNING: no bpix1 file!'
	fi
    elif [ -e ${rootdir}/secondary/hrc*N*_${ifix}bpix1.fits ] ;   then
	ln -s ${rootdir}/secondary/hrc*N*_${ifix}bpix1.fits  bpix1
    else
	echo 'WARNING: no bpix1 file!'
    fi
    
    # only for acis:
    #
    if [ -e stat1 ] ; then 
	if [ -e   ${rootdir}/secondary/acis*_pbk0.fits ] ;   then
	    ln -s ${rootdir}/secondary/acis*_pbk0.fits  pbk0
	else
	    echo 'WARNING: no pbk0 file!'
	fi
    fi
    
    
    # only for hrc:
    #
    if [ ! -e stat1 ] ; then 
	if [ -e   ${rootdir}/primary/hrc*N*_${ifix}dtf1.fits ] ;   then
	    ln -s ${rootdir}/primary/hrc*N*_${ifix}dtf1.fits dtf1
	else
	    echo 'WARNING: no dtf1 file found!'
	fi
    fi

    # should have test for asol files...
    #
    k=1
    for j in ${rootdir}/primary/*_asol1.fits
      do 
#      ln -s $j asol_$k
      ln -s $j asol_`echo $k | awk '{printf "%2.2d", $1}'`
      let "k += 1"
    done
    
    ls asol_* > asol.list


    # 2012.12.28 - include for use of acis_build_badpix
    #
    # only for acis:
    if [ -e stat1 ] ; then 
	k=1
	for j in ${rootdir}/secondary/*_bias0.fits
	do 
	    ln -s $j bias0_`echo $k | awk '{printf "%2.2d", $1}'`
	    let "k += 1"
	done
	ls bias0_* > bias0.list
    fi


    popd

    iter=$(($iter + 1))

  done

  popd 

  echo ''

done

exit 0
