#!/bin/bash
INPUT=telescopius_search_result.csv
echo "donne le nom du fichier de sortie"
read SORTIE
# ------------------------------------------
# Entrées CSV :
# 0: Catalogue Entry,
# 1: Familiar Name,
# 2: Alternative Entries, (inner csv)
# 3: Type,
# 4: Constellation,
# 5: Right Ascension,
# 6: Declination,
# 7: Magnitude,
# 8: Size,
# 9: Surface Brightness,
# 10: Rises over 10º,
# 11: Transit Time,
# 12: Sets below 10º,
# 13: Maximum Altitude
# 13: Position Angle (East)
# 13: Notes
cp $INPUT $SORTIE
# nettoyage du fichier Telescopius
sed -i ':a;s/^\(\([^"]*,\?\|"[^",]*",\?\)*"[^",]*\),/\1 /;ta' "$SORTIE"
sed -i 's/"//' "$SORTIE"
sed -i 's/"//' "$SORTIE"
sed -i 's/""/"/g' "$SORTIE"
# remplacement par variables utilisables
sed -i -e "s/Catalogue Entry/Ref/" "$SORTIE"
sed -i -e "s/Familiar Name/Nom/" "$SORTIE"
sed -i -e "s/Alternative Entries/Altnom/" "$SORTIE"
sed -i -e "s/Right Ascension/Rasc/" "$SORTIE"
sed -i -e "s/Surface Brightness/MagVis/" "$SORTIE"
sed -i -e "s/Rises over 5º/Riseover/" "$SORTIE"
sed -i -e "s/Transit Time/Transit/" "$SORTIE"
sed -i -e "s/Sets below 5º/Setbelow/" "$SORTIE"
sed -i -e "s/Maximum Altitude/Maxalt/" "$SORTIE"
sed -i -e "s/Position Angle (East)/Posangle/" "$SORTIE"
OLDIFS=$IFS
IFS=','
[ ! -f $SORTIE ] && { echo "$SORTIE file not found"; exit 99; }
cp $SORTIE $SORTIE.csv
file=$SORTIE.skylist
> $file
echo "SkySafariObservingListVersion=3.0" >> $file
let "Ind = 0"
while read Ref Nom Altnom Type Constellation Rasc Declinaison Magnitude Size Sbright Riseover Trtime Setbelow Maxalt Posangle Notes
do
	echo "SkyObject=BeginObject" >> $file
	echo -e "\tObjectID=4,-1,-1" >> $file
	echo -e "\tCatalogNumber=$Ref" >> $file
	echo $Ref
	echo -e "\tCommonName=$Nom" >> $file
	echo -e "\tDefaultIndex="$Ind >> $file
	echo "EndObject=SkyObject" >> $file
	let "Ind = $Ind + 1"
done < $SORTIE
sed -i -e '2,7d' "$file"
IFS=$OLDIFS
rm $SORTIE
exit 0
