#!/bin/bash

#see Usage by typing ./lcthumb

NAME=lcthumb

#obtaining base dir for script source
FIL=`ls -l $0 | tac -s \  | head -1`
DIR=`dirname $FIL`
if [ -z $DIR ]
then
  DIR="."
fi
BASE=$DIR/

#user supplied macro
if [ -z $LCTHUMB_MACRO ]; then
  LCTHUMB_MACRO=$BASE$NAME.user_macro.sm
fi

#Usage
if [ $# -lt 1 ]; then
  echo
  echo "Usage: $NAME LIST [NLINES=5] [NCOLS=1]"
  echo "  LIST of light curves (absolute paths, or relative to cwd)"
  echo 
  echo "  This program generates postscript miltipage file with NLINESxNCOLS light curves plots (using SM)"
  echo "  # author Jan Skowron"
  echo
  echo "  Uses files: lcthumb, lcthumb.fix_sm_pages, lcthumb.sm"
  echo "  user specified ploting SM macro thumb from: lcthumb.user_macro.sm"
  echo "  and programs: sm, awk, grep"
  echo "  Generates: lcthumb.list and lcthumb.ps"
  echo "  Base directory for script source is $BASE"
  echo "  User suplied macro found in LCTHUMB_MACRO=$LCTHUMB_MACRO"
  echo
  echo "  List can include both absolute paths or relative to current working directory"
  echo "  Please modify lcthumb.user_macro.sm for your convinience."
  echo "  Alternatively you can set (export or setenv) variable LCTHUMB_MACRO leeding to your"
  echo "  own file, eg. LCTHUMB_MACRO=/home/my_macro.sm containig definition of macro: thumb 1"
  echo
  #author Jan Skowron
  exit
fi

#initialization 
NLINES=
NCOLS=
if [ $# -ge 2 ]; then
  NLINES=$2
fi
if [ $# -ge 3 ]; then
  NCOLS=$3
fi

#file lists names
LISTA=$1
RYS=$NAME.list
TEMP=$RYS.tmp
DEL=$RYS.todel

#prepare cwd for generating symbolic links to data files. It is needed cause Super Mongo doesnt understand long file names
#so this script generates in current directory symlinks to data whereever they are. First we cleat cwd from files 0000.lcthumb.dat
ls [0-9].$NAME.dat [0-9][0-9].$NAME.dat [0-9][0-9][0-9].$NAME.dat [0-9][0-9][0-9][0-9].$NAME.dat >$TEMP 2>/dev/null
rm `cat $TEMP` 2>/dev/null || true
rm $RYS 2>/dev/null || true
rm $TEMP 2>/dev/null || true
rm $DEL 2>/dev/null || true

#creating syblinks to data and list LISTA of link name and file name for Super Mongo macro
cat $LISTA | cut -f 1 -d \  >$TEMP
I=0
for PLIK in `cat $TEMP`
do
  I=$(($I+1))
  echo $PLIK "->" $I.$NAME.dat
  ln -s $PLIK $I.$NAME.dat
  FILENAME=`echo $PLIK | tac -s "/" | head -1`
  echo $I.$NAME.dat $FILENAME  >> $RYS
  echo $I.$NAME.dat >> $DEL
done

#Super Mongo macro will draw NLINESxNRCOLS thumbnails (plots) of light curves from LISTA
#genetrated ps file will be lcthumb.ps
echo macro read \"$LCTHUMB_MACRO\" macro read \"$BASE$NAME.sm\" $NAME $RYS $LISTA $NLINES $NCOLS
sm macro read \"$LCTHUMB_MACRO\" macro read \"$BASE$NAME.sm\" $NAME $RYS $LISTA $NLINES $NCOLS
#fix postscript generater by SM
#every multipage postscript ganerated be SM is broken. This script will patch postcript file to be
#viewable and printable. It is not perfect but works.
$BASE$NAME.fix_sm_pages $NAME.ps

#cleanup
#remove all created symbolic links, remove temporary lists
rm `cat $DEL` 2>/dev/null
rm $TEMP 2>/dev/null || true
rm $DEL 2>/dev/null || true

