#! /bin/bash 

INPUT_FILE=lists/ifx_prices.csv

CSV_LINES=() 

LINE_CNT=0
COL_CNT=0
DATABASE_COLS=0

DELIMITER=';'

HEADER_STRING=""
LINE_STRING=""

PRODUCTS_ARR=(
"product_id"
"manufacturer"
"product_no"
"core_product"
"ordering_code"
"description"
"status"
"buy_cost"
"pricing_scheme"
"price_change_flag"
"currency"
"lead_time_days"
"physical_form"
"packing"
"packing_size"
"minimum_order_quantity"
"minimum_awarding_quantity"
"minimum_quote_quantity"
"proposed_margin"
"container"
"container_size"
"product_class"
"product_category"
"product_family"
"product_group"
"last_update_date"
"last_time_buy_date"
"obsolete_date"
"pollution_level"
"production_site"
"comment_manufacturer"
"comment"
)

product_id=0
manufacturer="Infineon"
product_no=2
core_product=6
ordering_code=1
description=10
status=22
buy_cost=33
pricing_scheme=20
price_change_flag=0
currency="USD"
lead_time_days=8
physical_form=25
packing=26
packing_size=27
minimum_order_quantity=29
minimum_awarding_quantity=31
minimum_quote_quantity=30
proposed_margin=25
container="box"
container_size=29
product_class=0
product_category=0
product_family=0
product_group=0
last_update_date="2022-08-14"
last_time_buy_date="0000-00-00"
obsolete_date="0000-00-00"
pollution_level="RoSh"
production_site="DE"
comment_manufacturer=44
comment="Test Upload"

####################################################################################################

rm output.csv
touch output.csv

####################################################################################################

while IFS= read -r line 
do
	CSV_LINES+=("$line")
	((LINE_CNT++))
done < $INPUT_FILE

####################################################################################################
IFS=$DELIMITER read -ra HEADER <<< "${CSV_LINES[0]}"   

####################################################################################################
for i in "${HEADER[@]}"; 
do
		((COL_CNT++))
done

for i in "${PRODUCTS_ARR[@]}"; 
do 
		((DATABASE_COLS++))
done

####################################################################################################
counter=0
for i in "${PRODUCTS_ARR[@]}";do
	((counter++))
	if [ $counter -eq  $DATABASE_COLS ]
	then
		HEADER_STRING+="$i"
	else 
		HEADER_STRING+="$i,"
	fi
done 

echo "$HEADER_STRING" >> output.csv

####################################################################################################
counter=0
for i in "${CSV_LINES[@]}";do
	IFS=$DELIMITER read -ra LINE <<< "${CSV_LINES[$counter]}"   

if [ $counter -gt 1 ]
then
LINE_STRING+="\
$product_id,\
$manufacturer,\
${LINE[$product_no]},\
${LINE[$core_product]},\
${LINE[$ordering_code]},\
${LINE[$description]},\
${LINE[$status]},\
${LINE[$buy_cost]},\
${LINE[$pricing_scheme]},\
$price_change_flag,\
$currency,\
${LINE[$lead_time_days]},\
${LINE[$physical_form]},\
${LINE[$packing]},\
${LINE[$packing_size]},\
${LINE[$minimum_order_quantity]},\
${LINE[$minimum_awarding_quantity]},\
${LINE[$minimum_quote_quantity]},\
$proposed_margin,\
$container,\
${LINE[$container_size]},\
$product_class,\
$product_category,\
$product_family,\
$product_group,\
$last_update_date,\
$last_time_buy_date,\
$obsolete_date,\
$pollution_level,\
$production_site,\
${LINE[$comment_manufacturer]},\
$comment\
\n"
fi
((counter++))
done
echo -e "$LINE_STRING" >> output.csv

sed -i '$ d' output.csv #Delete the last line
####################################################################################################

sshpass -p "$SERVER_USER_PASS" scp output.csv $SERVER_USER_NAME@$SERVER_HOST:$MARIADB_OUTSIDE_DATA
