#/bin/bash

docker exec -it oracle21c sqlplus GRIESMAYER/oracle@//localhost/XEPDB1 @/opt/oracle/loader/src/GRIESMAYER_CREATE_TABLE.sql

for FILENAME in `ls /home/docker/GRIESMAYER_loader/data/FACT_SALES_GRIESMAYER_*.csv`
do
     echo $FILENAME
     export FILENAME_BASE=`basename $FILENAME`
     echo $FILENAME_BASE
     export FILENAME_NOEXT=`basename $FILENAME | sed -e 's/\..*$//'`
     echo $FILENAME_NOEXT
     dos2unix $FILENAME
     cat $FILENAME | sed -e 's/"//g' | sed -e 's/ //g' | sed -e 's/,/./g' >/home/docker/GRIESMAYER_loader/data/FACT_SALES_GRIESMAYER.csv

     docker exec -it oracle21c sqlldr GRIESMAYER/oracle@//localhost/XEPDB1 data=/opt/oracle/loader/data/FACT_SALES_GRIESMAYER.csv control=/opt/oracle/loader/src/GRIESMAYER_loader.ldr log=/opt/oracle/loader/log/$FILENAME_NOEXT.log bad=/opt/oracle/loader/log/$FILENAME_NOEXT.bad errors=20

     wget -U 'Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0' https://wetter.orf.at/oes/
     export TEMP=`cat index.html | grep temperature | head -n 1 | sed -e 's/^[^>]*>//' | sed -e 's/&thinsp.*//' | sed -e 's/,/./'`

     echo "INSERT INTO GRIESMAYER_SALES" >/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "SELECT SALES_DATE,"       >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "       SALES_TIME,"       >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "       SALES_CHANNEL,"    >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "       PRODUCT_ID,"       >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "       CUSTOMER_ID,"      >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "       PIECES,"           >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "       REVENUE,"          >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "       DISCOUNT,"         >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "       TAX,"              >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "       $TEMP"             >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "FROM   GRIESMAYER_SALES_STAGING;"    >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo ""                         >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql
     echo "exit;"                    >>/home/docker/GRIESMAYER_loader/src/GRIESMAYER_INSERT.sql

     rm index.html

     docker exec -it oracle21c sqlplus GRIESMAYER/oracle@//localhost/XEPDB1 @/opt/oracle/loader/src/GRIESMAYER_INSERT.sql

done




