#!/bin/bash
if ( [ $# -ne 5 ] && [ $# -ne 6 ] ) || [ $(( $2*12+$3 )) -gt $(( $4*12+$5 )) ]; then
  echo "Usage:"
  echo "   ./GetPartOfVolc [VOLC.DAT] [start_year] [start_month] [end_year] [end_month]"
  echo "   ./GetPartOfVolc [VOLC.DAT] [start_year] [start_month] [end_year] [end_month] [VOLC_OLD.DAT]"
  exit
fi

fichier=$1
if [ $# -eq 6 ]; then 
  fichierold=$6
else
  fichierold=""
fi
start_year=$2
start_month=$3
end_year=$4
end_month=$5


nbline=$(( ${end_year}*12+${end_month}-(${start_year}*12+${start_month})+1 ))
for i in `seq 1 1 ${nbline}`
do
  #sortie=`head -n $(( (${start_year}-1)*12+${start_month}+${i}-1 )) ${fichier} | tail -n 1`
  fichier1=`head -n $(( (${start_year}-1)*12+${start_month}+${i}-1 )) ${fichier} | tail -n 1`
  if [ "${fichierold}" != "" ]; then
    fichier2=`head -n ${i} ${fichierold} | tail -n 1`
    sortie=`echo | awk -v x="${fichier2}" "BEGIN{ans=x}{print substr(ans,45,length(ans))}END{}"`
    sortie="${fichier1}${sortie}"
  else
    sortie="${fichier1}"
  fi
  echo "${sortie}"
done


