#!/bin/bash
# converts a csv-file into a *.pdb database for the pilot-db-application
# the first line in the csv-file must contain the field-names
# Syntax: t2pdb csv-file db-title

 SEP=%%

if [ -z $1 ]
then
  echo "Usage: `basename $0` csv-file db-title"
  exit
fi

if [ -z $2 ]
then
  echo "Usage: `basename $0` csv-file db-title"
  exit
fi


ifo_file=$1.ifo


echo '# Database informations' > $ifo_file
echo 'type "db"' >> $ifo_file
echo title \"$2\" >> $ifo_file

for X in $( head -n 1 $1 | tr ' ' '_' | tr ',' '\n')
# head -n 1 $1 | tr ',' '\n' --- schneidet die erste Zeile aus und setzt die Kolonnentitel in Linien. (Komma als Feldtrenner)
do
echo field \"$X\" string>> $ifo_file
done

echo 'option backup true
# CSV informations
extended off
format time "%H:%M"
format date "%Y/%m/%d"'>> $ifo_file
echo csvfile "$1">>$ifo_file
echo '# PDB informations
pdbpath ""'>>$ifo_file
clear
cat $ifo_file

csv2pdb --info="$ifo_file" --separator='%%' "$1" "$2".pdb

rm $ifo_file

exit