#!/bin/bash

#wget http://www.cichlid-forum.com/profiles/category.php?cat=1 -O lista1
#cat lista1 |grep medtext|cut -f4 -d'"'|sort|uniq>lista2


#for link in `cat lista2`;do
#    wget http://www.cichlid-forum.com/profiles/$link -O ryby/$link
#done

cd ryby
dos2unix *


for plik in `ls -1`;do


sname=`cat $plik|grep 'Scientific'|html2text|cut -f3 -d':'|sed s/\"//g|sed s/\'//g`
cname=`cat $plik|grep 'Common'|html2text|cut -f3 -d':'|sed s/\"//g|sed s/\'//g`
origin=`cat $plik|grep 'Origin'|html2text|cut -f3 -d':'|sed s/\"//g|sed s/\'//g`
habitat=`cat $plik|grep 'Habitat'|head -n1|grep -v spacer.gif|html2text|sed s/\"//g|cut -f3 -d':'`
#echo $habitat
      if [ "$habitat" = ' Caves among rocks' ]; then habitat='38'
    elif [ "$habitat" = ' Deep sand' ]; then habitat='39'
    elif [ "$habitat" = ' Intermediate Zone' ]; then habitat='9'
    elif [ "$habitat" = ' Open Water' ]; then habitat='40'
    elif [ "$habitat" = ' Sand' ]; then habitat='4'
    elif [ "$habitat" = ' Shallow Intermediate Zone' ]; then habitat='29'
    elif [ "$habitat" = ' shallow sand' ]; then habitat='41'
    elif [ "$habitat" = ' Among reed stands & Vallisneria beds in sediment-rich bays.' ]; then habitat='53'
    elif [ "$habitat" = ' Deep Intermediate Habitats' ]; then habitat='54'
    elif [ "$habitat" = ' Deep rocky habitats' ]; then habitat='1'
    elif [ "$habitat" = ' Deep Rocky Habitats' ]; then habitat='1'
    elif [ "$habitat" = ' Deep sandy habitat' ]; then habitat='55'
    elif [ "$habitat" = ' Deep Water' ]; then habitat='42'
    elif [ "$habitat" = ' Dwels among beds of Ceratophyllum' ]; then habitat='56'
    elif [ "$habitat" = ' Edge of the rocky habitat' ]; then habitat='57'
    elif [ "$habitat" = ' Gently sloping sandy habitats along rocky coasts' ]; then habitat='58'
    elif [ "$habitat" = ' Gently sloping sandy habitat along the rocky coast' ]; then habitat='58'
    elif [ "$habitat" = ' Intermediate habitat' ]; then habitat='47'
    elif [ "$habitat" = ' Intermediate Habitat' ]; then habitat='47'
    elif [ "$habitat" = ' Intermediate zone' ]; then habitat='46'
    elif [ "$habitat" = ' Intermediate zone among extensive beds of small pebbles' ]; then habitat='59'
    elif [ "$habitat" = ' Muddy bottoms' ]; then habitat='60'
    elif [ "$habitat" = ' Muddy bottom bay' ]; then habitat='61'
    elif [ "$habitat" = ' Open sand' ]; then habitat='4'
    elif [ "$habitat" = ' Open sand, shallow water' ]; then habitat='43'
    elif [ "$habitat" = ' Open sand & intermediate zone' ]; then habitat='48'
    elif [ "$habitat" = ' Open water' ]; then habitat='40'
    elif [ "$habitat" = ' Open water and shallow, rocky habitat' ]; then habitat='62'
    elif [ "$habitat" = ' Open water and steep sloping sandy habitat along rocky coasts' ]; then habitat='63'
    elif [ "$habitat" = ' Open water over rocky habitats' ]; then habitat='64'
    elif [ "$habitat" = ' Open water over sandy floor' ]; then habitat='65'
    elif [ "$habitat" = ' Over sand in the intermediate or sandy habtiats' ]; then habitat='66'
    elif [ "$habitat" = ' Rocks' ]; then habitat='8'
    elif [ "$habitat" = ' Rocky and intermediate zones' ]; then habitat='67'
    elif [ "$habitat" = ' Rocky habitat' ]; then habitat='8'
    elif [ "$habitat" = ' Sandy bottom bay' ]; then habitat='68'
    elif [ "$habitat" = ' Sandy habitat' ]; then habitat='4'
    elif [ "$habitat" = ' Sandy Habitat' ]; then habitat='4'
    elif [ "$habitat" = ' Sandy habitat & sometimes intermediate habitat to hunt' ]; then habitat='69'
    elif [ "$habitat" = ' Sandy intermediate habitat' ]; then habitat='70'
    elif [ "$habitat" = ' Sandy sections of the intermediate habitats in shallow water' ]; then habitat='71'
    elif [ "$habitat" = ' Sandy section of the intermediate habitat' ]; then habitat='72'
    elif [ "$habitat" = ' Sediment-free rocky habitat' ]; then habitat='3'
    elif [ "$habitat" = ' Sediment-Free Rocky Habitat' ]; then habitat='3'
    elif [ "$habitat" = ' Sediment-free rocky habitat and intermediate zone' ]; then habitat='49'
    elif [ "$habitat" = ' Sediment-free rocky habitat or rock-sand interface' ]; then habitat='73'
    elif [ "$habitat" = ' Sediment-rich biotope where rocks meet the open sand floor' ]; then habitat='74'
    elif [ "$habitat" = ' Sediment-rich rocky habitat' ]; then habitat='5'
    elif [ "$habitat" = ' Sediment-Rich Rocky Habitat' ]; then habitat='5'
    elif [ "$habitat" = ' Sediment-rich shallow water' ]; then habitat='75'
    elif [ "$habitat" = ' Shallow inshore habitat' ]; then habitat='76'
    elif [ "$habitat" = ' Shallow intermediate habitat' ]; then habitat='29'
    elif [ "$habitat" = ' Shallow muddy bays, sandy habitat, and intermediate habitat' ]; then habitat='77'
    elif [ "$habitat" = ' Shallow muddy habitat' ]; then habitat='78'
    elif [ "$habitat" = ' Shallow sand-muddy bottoms and upstream of river mouths' ]; then habitat='79'
    elif [ "$habitat" = ' Shallow sandy habitat' ]; then habitat='80'
    elif [ "$habitat" = ' Shallow sandy habitat near rocks' ]; then habitat='81'
    elif [ "$habitat" = ' Shallow Sediment-Rich Bays' ]; then habitat='51'
    elif [ "$habitat" = ' Shallow Sediment-Rich Habitat' ]; then habitat='52'
    elif [ "$habitat" = ' Shallow sediment rich bays' ]; then habitat='51'
    elif [ "$habitat" = ' Shallow vegetated habitats & the intermediate zone' ]; then habitat='82'
    elif [ "$habitat" = ' Shallow water' ]; then habitat='44'
    elif [ "$habitat" = ' Shallow water over sandy bottom' ]; then habitat='45'
    elif [ "$habitat" = ' Steep rocky shores' ]; then habitat='83'
    elif [ "$habitat" = ' Wave-washed upper habitat at steep rock coasts' ]; then habitat='84'
    else habitat='11'
    fi
diet=`cat $plik|grep 'Diet'|head -n1|html2text|cut -f3 -d':'|sed s/\ //|sed s/Carnivore/Mięsożerne/|sed s/Omnivore/Wszystkożerne/|sed s/Herbivore/Roślinożerne/`
gdiff=`cat $plik|grep 'Gender Differences'|head -n1|html2text|cut -f3 -d':'|sed s/\ //|sed s/Dimorphic/Dymorfizm/|sed s/Monomorphic/Monomorfizm/`
breed=`cat $plik|grep 'Breeding'|head -n1|html2text|cut -f3 -d':'|sed s/Maternal\ Mouthbrooder/Samica\ inkubuje\ ikrę\ w\ pysku/`
temper=`cat $plik|grep 'Temperament'|head -n1|grep -v spacer.gif|html2text|cut -f3 -d':'|sed s/X/Ekstremalnie\ Agresywny/|sed s/X/Wysoce\ Agresywny/|sed s/Peaceful/Pokojowy/|sed s/Mildly\ Aggressive/Średnio\ Agresywny/|sed s/Aggressive/Agresywny/`
ctemper=`cat $plik|grep 'Conspecific Temperament'|head -n1|grep -v spacer.gif|html2text|cut -f3 -d':'|sed s/X/Ekstremalnie\ Agresywny/|sed s/X/Wysoce\ Agresywny/|sed s/Peaceful/Pokojowy/|sed s/Mildly\ Aggressive/Średnio\ Agresywny/|sed s/Aggressive/Agresywny/`
size=`cat $plik|grep 'Maximum Size'|head -n1|grep -v spacer.gif|html2text|cut -f3 -d':'|sed s/\"//`
size=`echo "$size*2.54"|bc|awk -F\. '{if(($2/10^length($2)) >= .5) printf("%d\n",$1+1);else printf("%d\n",$1)}'`
temp=`cat $plik|grep 'Temperature'|head -n1|sed s/deg//|sed s/\\&\\;F//|html2text|cut -f3 -d':'`
tempmin=`echo $temp|cut -f1 -d'-'`
tempmin=`echo "scale=1;(5/9)*($tempmin-32)"|bc|awk -F\. '{if(($2/10^length($2)) >= .5) printf("%d\n",$1+1);else printf("%d\n",$1)}'|sed s/\ //`
tempmax=`echo $temp|cut -f2 -d'-'`
tempmax=`echo "scale=1;(5/9)*($tempmax-32)"|bc|awk -F\. '{if(($2/10^length($2)) >= .5) printf("%d\n",$1+1);else printf("%d\n",$1)}'|sed s/\ //`
ph=`cat $plik|grep 'pH'|head -n1|html2text|cut -f3 -d':'|sed s/\ //`
hard=`cat $plik|grep 'Water Hardness'|head -n1|html2text|cut -f3 -d':'|sed s/Hard/Twarda/|sed s/\ //`
diffi=`cat $plik|grep 'Difficulty'|head -n1|html2text|cut -f3 -d':'|sed s/\ //`


#echo $plik
echo "INSERT INTO gatunki (id ,typ ,nazwa ,nazwa_powsz ,pochodzenie ,srodowisko ,dieta ,plec ,rozmnazanie ,temperament ,widoczny_temperament ,wielkosc ,temperatura ,temperatura_max ,ph ,twardosc ,trudnosc)
VALUES (NULL , '3', '$sname', '$cname', '$origin', $habitat, '$diet', '$gdiff', '$breed', '$temper', '$ctemper ', '$size', '$tempmin', '$tempmax ', '$ph ', '$hard', '$diffi');"



#echo $plik
#echo $sname
#echo $cname
#echo $origin
#echo $habitat
#echo $diet
#echo $gdiff
#echo $breed
#echo $temper
#echo $ctemper
#echo $size
#echo $tempmin
#echo $tempmax
#echo $ph
#echo $hard
#echo $diffi
#echo "==="
done

