#!/bin/bash
#2012.10.26
# Usage: gyujtzh	OR gyujtzh [config file name]
# Format for the config file (default config file name is csvzhfileok.txt):
# Any number of lines starting with "be": input files.
# One line starting with "publ_ki": location for public output
# One line starting with "ki": location for non-public output
# One line starting with "fuzott": name for the combined file

# Set default cfg file if not given
cfgfile="$1"
if [ ! $cfgfile ]
then
 cfgfile="csvzhfileok.txt"
fi

# Check and create files/directories:
if [ ! -f $cfgfile ]
then
 echo "Nem talalom a $cfgfile filet, kiszallok."
 exit
fi

if [ ! -d zharchives ]
then
  mkdir zharchives
fi

if [ ! -d zhlastsaved ]
then
  mkdir zhlastsaved
fi

if [ ! -d zhdead ]
then
  mkdir zhdead
fi

# Extract info from csvzhfileok
helyek=`cat $cfgfile | awk ' $1 == "be" { print($2) }'`
publki=`cat $cfgfile | awk ' $1 == "publ_ki" { print($2) }'`
ki=`cat $cfgfile | awk ' $1 == "ki" { print($2) }'`
cmbfile=`cat $cfgfile | awk ' $1 == "fuzott" { print($2) }'`
# echo helyek: $helyek
# echo publki: $publki
# echo ki: $ki
# echo fuzott: $cmbfile

if [ ! -d "$publki" ]
then
 echo "Nem talalom a $publki publki konyvtarat, kiszallok."
 exit
fi

if [ ! -d "$ki" ]
then
 echo "Nem talalom a $ki ki konyvtarat, kiszallok."
 exit
fi

# If combining files requested:
if [ $cmbfile ]
then

# Check for conflicts
 for i in $helyek; do
 
 # Extract filenames without extension:
  file=${i##*/}
  fileelej=${file%.*}
  if [ "$fileelej" = "$cmbfile" ]
  then
   echo "A fuzott file neve nem egyezhet meg egyik letoltott file nevevel sem: $fileelej!! Kiszallok."
   exit
  fi
 done

# Check if .knv, .kov files exist:
 if [ ! -f "$cmbfile".kov ]
 then
  echo "Nem talalom az osszefuzeshez a $cmbfile.kov filet, kiszallok."
  exit
 fi
 
 if [ ! -f "$cmbfile".knv ]
 then
  echo "Nem talalom az osszefuzeshez a $cmbfile.knv filet, kiszallok."
  exit
 fi

# Clean for combined files:
 if [ -f "$cmbfile.csv" ]
 then
  rm "$cmbfile.csv"
 fi

# Touch combined file csv:
 touch "$cmbfile.csv"
fi

for i in $helyek; do

# Extract filenames with and without extension:
 file=${i##*/}
 fileelej=${file%.*}

# Check if the .knv and .kov files are present:
 if [ ! -f "$fileelej".knv -o ! -f "$fileelej".kov ]
 then
  read -p "Nincsen $fileelej.knv vagy $fileelej.kov; nyomj entert"
  continue
 fi

# Get files: clean first:
 if [ -f "$file" ]
 then
  rm "$file"
 fi

# Get if http:
 if [[ "$i" == http* ]]
 then
  wget "$i"
  if [ "$?" -ne "0" ]
  then
   read -p "Copy: baj van $i-vel; nyomj entert"
   continue
  fi

# Copy otherwise: 
 else
  cp "$i" ./
  if [ "$?" -ne "0" ]
  then
   read -p "Copy: baj van $i-vel; nyomj entert"
   continue
  fi
 fi

# See if file has changed, if yes, then process
 cmp -s $file zhlastsaved/$file
 if [ "$?" -ne "0" ]
  then
  ~/zhpontozo/csv2zhk $fileelej
  ~/zhpontozo/zhus $fileelej
  firefox $fileelej.html
  tetszik="a"
  while [[ "$tetszik" != "i" && "$tetszik" != "n" ]]; do
   echo "Tetszik a $fileelej.html? i/n"
   read tetszik
  done
  if [ "$tetszik" == "i" ]
  then
   echo "publ_$fileelej.html --> $publki"
   mv publ_$fileelej.html $publki
   echo "$fileelej.html --> $ki"
   cp $fileelej.html $ki

# Save file to zhlastsaved
   cp $file zhlastsaved/
   mv $fileelej.html zhlastsaved/

# Save file to zharchives
   echo "$file --> zharchives/$fileelej`date +_%m_%d_%H_%M`.csv"
   mv $fileelej.csv zharchives/$fileelej`date +_%m_%d_%H_%M`.csv
  else

# Save file to zhdead if not good
   echo "publ_$fileelej.html --> zhdead/"
   mv publ_$fileelej.html zhdead/
   echo "$fileelej.html --> zhdead/"
   mv $fileelej.html zhdead/
   echo "$file --> zhdead/"
   mv $file zhdead/
  fi
 else
  rm $file
 fi

# If combining was requested, then combine if exists in lastsaved:
 if [ $cmbfile ] && [ -f "zhlastsaved/$file" ]
 then
  echo "Fuzom a $file filet."
  cat zhlastsaved/$file >> $cmbfile.csv
  cmp -s $fileelej.kov $cmbfile.kov
  if [ "$?" -ne "0" ]
  then
   read -p "Vigyazat, $fileelej.kov es $cmbfile.kov kulonboznek; nyomj entert"
  fi
 fi 
done
 
# Process the combined file
if [ $cmbfile ]
then
 cmp -s $cmbfile.csv zhlastsaved/$cmbfile.csv
 if [ "$?" -ne "0" ]
  then
  ~/zhpontozo/csv2zhk $cmbfile
  ~/zhpontozo/zhus $cmbfile
  firefox $cmbfile.html
  tetszik="a"
  while [[ "$tetszik" != "i" && "$tetszik" != "n" ]]; do
   echo "Tetszik a fuzott $cmbfile.html? i/n"
   read tetszik
  done
  if [ "$tetszik" == "i" ]
  then
   echo "Fuzott publ_$cmbfile.html --> $publki"
   mv publ_$cmbfile.html $publki
   echo "Fuzott $cmbfile.html --> $ki"
   cp $cmbfile.html $ki
 
# Save file to zhlastsaved
   cp $cmbfile.csv zhlastsaved/
   mv $cmbfile.html zhlastsaved/
 
# Save file to zharchives
   echo "Fuzott $cmbfile.csv --> zharchives/$cmbfile`date +_%m_%d_%H_%M`.csv"
   mv $cmbfile.csv zharchives/$cmbfile`date +_%m_%d_%H_%M`.csv
  else
 
# Save file to zhdead if not good
   echo "Fuzott publ_$cmbfile.html --> zhdead/"
   mv publ_$cmbfile.html zhdead/
   echo "Fuzott $cmbfile.html --> zhdead/"
   mv $cmbfile.html zhdead/
   echo "Fuzott $cmbfile.csv --> zhdead/"
   mv $cmbfile.csv zhdead/
  fi
 else
  rm $cmbfile.csv
 fi
fi
