#!/bin/ksh

###############################################################
# Script for                                     #
# Ray Schwartz                                                #
#                                                             #
###############################################################

cd /m1/incoming/extracts/

nawk 'FS="/t" {print "\""$2"\",\""$6"\",,,,,,\""$1"\",\""$5"\",\""substr($5, length($5)-3)"\",,"}' /m1/incoming/extracts/jlistcdrom2.out > /m1/incoming/extracts/jlistcdrom2.csv
sed "s/RAGE\",,/STORAGE\",,/g" /m1/incoming/extracts/jlistcdrom2.csv > /m1/incoming/extracts/jlistcdrom3.csv
sed "s/[A-QS-z][A-z][A-z][A-z]\",,/\",,/g" /m1/incoming/extracts/jlistcdrom3.csv > /m1/incoming/extracts/jlistcdrom4.csv
sed "s/ TO PRESENT//g" /m1/incoming/extracts/jlistcdrom4.csv > /m1/incoming/extracts/jlistcdrom5.txt
cp jlistfields.asc jlistcdrom6.txt
# print 'Title (Required),Default URL,Publisher,Public Note,Display Public Note,Location Note,Display Location Note,ISSN,Coverage Date From,Coverage Date To,Language ID,Alphabetization' > jlistcdrom6.txt
/bin/cat jlistcdrom5.txt >> jlistcdrom6.txt

nawk 'FS="|" {print "\""$2"\",\""$6"\",,,,,,\""$1"\",\""$5"\",\""substr($5, length($5)-3)"\",,"}' /m1/incoming/extracts/jlistfiche2.out > /m1/incoming/extracts/jlistfiche2.csv
sed "s/RAGE\",,/STORAGE\",,/g" /m1/incoming/extracts/jlistfiche2.csv > /m1/incoming/extracts/jlistfiche3.csv
sed "s/[A-QS-gi-z][A-su-z][A-ln-z][A-jm-z]\",,/\",,/g" /m1/incoming/extracts/jlistfiche3.csv > /m1/incoming/extracts/jlistfiche4.csv
sed "s/ TO PRESENT//g" /m1/incoming/extracts/jlistfiche4.csv > /m1/incoming/extracts/jlistfiche5.txt
cp jlistfields.asc jlistfiche6.txt
# print 'Title (Required),Default URL,Publisher,Public Note,Display Public Note,Location Note,Display Location Note,ISSN,Coverage Date From,Coverage Date To,Language ID,Alphabetization' > jlistfiche6.txt
/bin/cat jlistfiche5.txt >> jlistfiche6.txt

nawk 'FS="|" {print "\""$2"\",\""$6"\",,,,,,\""$1"\",\""$5"\",\""substr($5, length($5)-3)"\",,"}' /m1/incoming/extracts/jlistfilm2.out > /m1/incoming/extracts/jlistfilm2.csv
sed "s/RAGE\",,/STORAGE\",,/g" /m1/incoming/extracts/jlistfilm2.csv > /m1/incoming/extracts/jlistfilm3.csv
sed "s/[A-QS-gi-z][A-su-z][A-ln-z][A-jm-z]\",,/\",,/g" /m1/incoming/extracts/jlistfilm3.csv > /m1/incoming/extracts/jlistfilm4.csv
sed "s/ TO PRESENT//g" /m1/incoming/extracts/jlistfilm4.csv > /m1/incoming/extracts/jlistfilm5.txt
cp jlistfields.asc jlistfilm6.txt
# print 'Title (Required),Default URL,Publisher,Public Note,Display Public Note,Location Note,Display Location Note,ISSN,Coverage Date From,Coverage Date To,Language ID,Alphabetization' > jlistfilm6.txt
/bin/cat jlistfilm5.txt >> jlistfilm6.txt

nawk 'FS="|" {print "\""$2"\",\""$6"\",,,,,,\""$1"\",\""$5"\",\""substr($5, length($5)-3)"\",,"}' /m1/incoming/extracts/jlistpaper2.out > /m1/incoming/extracts/jlistpaper2.csv
sed "s/RAGE\",,/STORAGE\",,/g" /m1/incoming/extracts/jlistpaper2.csv > /m1/incoming/extracts/jlistpaper3.csv
sed "s/[A-QS-z][A-z][A-z][A-z]\",,/\",,/g" /m1/incoming/extracts/jlistpaper3.csv > /m1/incoming/extracts/jlistpaper4.csv
sed "s/ TO PRESENT//g" /m1/incoming/extracts/jlistpaper4.csv > /m1/incoming/extracts/jlistpaper5.txt
cp jlistfields.asc jlistpaper6.txt
# print 'Title (Required),Default URL,Publisher,Public Note,Display Public Note,Location Note,Display Location Note,ISSN,Coverage Date From,Coverage Date To,Language ID,Alphabetization' > jlistpaper6.txt
/bin/cat jlistpaper5.txt >> jlistpaper6.txt


/m1/shared/bin/perl /m1/incoming/extracts/jlist01.pl
/m1/shared/bin/perl /m1/incoming/extracts/jlist02cdrom.pl
/m1/shared/bin/perl /m1/incoming/extracts/jlist02fiche.pl
/m1/shared/bin/perl /m1/incoming/extracts/jlist02film.pl
/m1/shared/bin/perl /m1/incoming/extracts/jlist02paper.pl
grep -v "||||" /m1/incoming/extracts/jlistcdrom.out | egrep 'PerColl|CMC' > /m1/incoming/extracts/jlistcdrom1.out
grep -v "||||" /m1/incoming/extracts/jlistfiche.out | egrep 'PerColl|CMC' > /m1/incoming/extracts/jlistfiche1.out
grep -v "||||" /m1/incoming/extracts/jlistfilm.out | egrep 'PerColl|CMC' > /m1/incoming/extracts/jlistfilm1.out
grep -v "||||" /m1/incoming/extracts/jlistpaper.out | egrep 'PerColl|CMC' > /m1/incoming/extracts/jlistpaper1.out

sed  "s/\"/\'/g" /m1/incoming/extracts/jlistcdrom1.out > /m1/incoming/extracts/jlistcdrom2.out
sed  "s/\"/\'/g" /m1/incoming/extracts/jlistfiche1.out > /m1/incoming/extracts/jlistfiche2.out
sed  "s/\"/\'/g" /m1/incoming/extracts/jlistfilm1.out > /m1/incoming/extracts/jlistfilm2.out
sed  "s/\"/\'/g" /m1/incoming/extracts/jlistpaper1.out > /m1/incoming/extracts/jlistpaper2.out

(uuencode /m1/incoming/extracts/jlistcdrom6.txt jlistcdrom6.txt; uuencode /m1/incoming/extracts/jlistfiche6.txt jlistfiche6.txt; uuencode /m1/incoming/extracts/jlistfilm6.txt jlistfilm6.txt; uuencode /m1/incoming/extracts/jlistpaper6.txt jlistpaper6.txt) | mailx -s "SS AtoZ Update" schwartzr2@wpunj.edu

mv *.txt /m1/incoming/extracts/oldfils
mv *.out /m1/incoming/extracts/oldfils
mv *.csv /m1/incoming/extracts/oldfils

exit
