#!/bin/bash


LISTDIR='./C++Source/boost_1_48_0'
COUNTER=0;
IFS='
'

echo 'TRUNCATE TABLE test' | mysql -u test -ptest test;
echo 'SELECT COUNT(*) FROM test' | mysql -u test -ptest test;

if [ -z "$1" -o  "$1" = "1" ]; then
  while read i; do
    xy="-$i-"
    echo "INSERT INTO test (name) VALUES ('$xy')" | mysql -u test -ptest test
#    COUNTER=`expr $COUNTER + 1`;
#    echo "$COUNTER";
  done < <(find "$LISTDIR" -type f)
else
  max=$1
  max=$(($max + 1))
  if [ "$max" -lt "2" ]; then
    echo "Fujtajbl parametr"
    exit 1
  fi
  c=0
  sql="INSERT INTO test (name) VALUES "
  while read i; do  
    xy="-$i-"
    c=$(($c+1))
    if [[ "$c" -ge "$max" ]]; then
      sql="${sql} ('$xy')"
      echo "$sql" | mysql -u test -ptest test 
      # or ### echo "$sql" | mysql -u test -ptest test &   
      c=0
      sql="INSERT INTO test (name) VALUES "
    else
      sql="${sql} ('$xy'),"
    fi
#    COUNTER=`expr $COUNTER + 1`;
#    echo "$COUNTER";
  done < <(find "$LISTDIR" -type f)
  if [[ "$c" -gt "0" ]]; then
    echo "${sql%?}" | mysql -u test -ptest test
  fi
fi

# ### wait

echo 'SELECT COUNT(*) FROM test' | mysql -u test -ptest test;


