#!/bin/csh
##########################################
# Methode:
cat <<===afaire
1) Copier le message dans Mail (cpMail file_message)
2) cd plu2
3) munpack ../Mail/fichier_message
4) sauvegarder version courante de ReadMe, pdbi.dat et pdbi_pi.dat
   dans sous-repertoire versions
5) lancer convert
===afaire
confirm_no "ok"
##########################################
# Create the Log from the CD-ROM files
# Arguments = List of files (not headers)
# Original files in directories plus (<2007)
# et plus2 (>=2007)
##########################################

#set verbose

chmod 751 .
rm -f lof tmp1
rm -f c[12]??? 

if ((-f pdbi.dat) || (-f pdbi.dat.gz)) then
    echo "****File 'pdbi.dat' exists, remove it first!"
    exit 1
endif

#ls ori/pdbi-???-[12]???.dat 
#ls plus/pdbi-???-[12]???-2.dat \
#   plu2/pdbi-???-2???.dat \
#   plu2/pdbi-???-2???-2.dat \

ls plu?/pdbi-*.dat | fgrep -v header \
| sort -t- -k 3 -k 2 > lof

# Verify duplicates?
set dup = `acut -d/ -f2 lof | acut -d- -f1-3 | acut -d. -f1 | sort | uniq -d`
if ($#dup > 0) then
    echo "#***There are duplicated files: $dup"
    foreach f ($dup)
        ls -l plu?/$f*
    end
    exit 1
endif

cat `cat lof` | trim | gawk -f $0.awk > tmp1

echo ".... tmp1 ready: `wc -l < tmp1` lines."
#set verbose

#gawk '{ if (substr($0,57,4) == 1950) print > "c1950"; \
#   else print > "c2000"}' tmp1 
#gawk '{Ep = substr($0,57,4); print > "c" Ep }' tmp1
gawk '{Ep = substr($0,61,4); print > "c" Ep }' tmp1
echo .... prepared c[12]??? 
wc c[12]???
foreach f (c[12]???)
    if ($f == c2000) continue
    set eq = `echo $f | tr c B | sed s/B2/J2/`
    #cat $f | astropos -4+ -c62-89 -r $eq J2000 \
    cat $f | astropos -4+ -c66-93 -r $eq J2000 \
    | sed 's/,.................//' >> c2000
end

#sort -t\; -k 1.21 c2000 | trcol -b51 | acut -c1-74 -c75-88%14.4S -c89- \
#| trcol -t61-88 '*:' '  ' | trcol -t69,84 ' ' .  | sed 's/  [.] /    /g' \
#| trim > pdbi.dat
sort -t\; -k 1.21 c2000 |sed 's/DEC2013[: ]  /+00 00 00./g'| trcol -b55 | acut -c1-78 -c79-92%14.4S -c93- \
| trcol -t65-92 '*:' '  ' | trcol -t73,88 ' ' .  | sed 's/  [.] /    /g' \
| trim > pdbi.dat

echo1 ".... pdbi.dat ready: `wc -l < pdbi.dat` lines."
echo1 " --- Corriger .Summary: \cRecords, \vizExplain}{ date}"
confirm "====Look for PIs (convert.pi)" && ./convert.pi

rm -f c????
rm -f lof tmp1
