#!/bin/sh
#____________________________________________________________
#
#                 TuxDax - AktienCheck   v2.50
#
#               Aktiencharts und -kurse anzeigen
#
#________________________________________ by L e x y | artist
#############################################################

  DIR="/var/plugins/tuxdax"
  
  SCRIPT_TIMEOUT=50
  HTTPK="http://www.aktiencheck.de/kurse/liste.m?kl="
  HTTPN="http://www.aktiencheck.de/suche.m?type=news_analysen&suchbegriff="
  HTTPP="http://www.aktiencheck.de/news/drucke_news.m?"
  HTTP="http://www.aktiencheck.de/suche.m?type="
  URL="http://www.aktiencheck.de"
  SUCHA="&suchbegriff="
  timespan=$(grep "timespan=" $DIR/tuxdax_config)
  volumen=$(grep "volumen=" $DIR/tuxdax_config)
  sBrushWidthStock=$(grep "sBrushWidthStock=" $DIR/tuxdax_config)  
  sColorChart=$(grep "sColorChart=" $DIR/tuxdax_config)
  charttype=$(grep "charttype=" $DIR/tuxdax_config)
  cside=$(grep "cside=" $DIR/tuxdax_config)
  cname=$(grep "cname=" $DIR/tuxdax_config)
  cname_COLOR=$(grep "sColorText=" $DIR/tuxdax_config)
  sBrushWidthVol=$(grep "sBrushWidthVol=" $DIR/tuxdax_config)
  sBrushWidthMisc=$(grep "sBrushWidthMisc=" $DIR/tuxdax_config)
  TIM=$(grep "TIM=" $DIR/tuxdax_config)
  

#____________________________________________________________
##   TimeOUT
#############################################################

  kill_script() {
	echo "TimeOut von $SCRIPT_TIMEOUT Sekunden ueberschritten!\n"
	rm /tmp/dax_res2
	rm /tmp/dax_res1
	rm /tmp/dax_res
	kill $$
	exit 0;
  }

  timeOut() {
	sleep $SCRIPT_TIMEOUT && kill_script&
  }

  case "$1" in
  

#____________________________________________________________
##   Find Stock                                            <<
#############################################################
find_stock)

  if [ -z $3 ]; then
        WKN=`input l="WKN/ISIN/Symbol/Name:@@@@@@@@@@@@@@@@@@@@@" t="~YTuxDax  -  Suche" m=0 k=1 c=21`
        if [ -z $WKN ]; then
                $0
        fi
  else
        WKN=$3
  fi
  
  wget -q -O /tmp/dax_res $HTTP$2$SUCHA$WKN
  cat /tmp/dax_res | sed s/'colspan="4" align="left"><a'/'colspan="1" align="ri'/g | sed s/'colspan="2" align="right"'/'colspan="1" align="ri'/g | grep 'colspan="1" align="ri' | cut -d '>' -f 2- | cut -d '<' -f -1 | sed s/'('//g | sed s/')'//g | sed s/','/'.'/g > /tmp/dax_res1
  
  echo "FONT=/share/fonts/micron_bold.ttf" > /tmp/dax_res2
  echo "FONTSIZE=18" >> /tmp/dax_res2
  echo "LINESPP=14" >> /tmp/dax_res2
  echo "WIDTH=800" >> /tmp/dax_res2
  echo "HEIGHT=400" >> /tmp/dax_res2
  echo "MENU=~YTuxDax  -  Suchergebnis">> /tmp/dax_res2
  
  ###-> Kopfzeile <-###
  if [ ! -s /tmp/dax_res1 ]; then
        echo "COMMENT=" >> /tmp/dax_res2
        echo "DEPENDON=+Die Suche war leider erfolglos!,/tmp/dax_res2,echo 2" >> /tmp/dax_res2
        echo "COMMENT=" >> /tmp/dax_res2
        echo "DEPENDON=+Es wird empfohlen nach der ISIN / WKN zu suchen,/tmp/dax_res2,echo 2" >> /tmp/dax_res2
        echo "DEPENDON=+oder ggf. eine andere Kathegorie auszuw~ahlen.,/tmp/dax_res2,echo 2" >> /tmp/dax_res2
        echo "COMMENT=*" >> /tmp/dax_res2
  else
        echo "COMMENT=~t*~t*~t*~tAusw~ahlen und ins Depot legen!~t*~t*~t*" >> /tmp/dax_res2
  fi
  
  ###-> Suchresultat <-###
  while
        read line1
  do
        read line2
        read line3
        read line4
        echo "ACTION=&$line1, $DIR/tuxdax add_stock $line2 $line3 $2 $line1, Aktie wird hinzugef~ugt" >> /tmp/dax_res2
        echo "COMMENT=*  ISIN: $line2 ~t~t WKN: $line4 ~t~t Symbol: $line3 ~t Typ: $2" >> /tmp/dax_res2
  done < /tmp/dax_res1

  ###-> Closing Behavior <-###
  if [ -z $3 ]; then
        echo "ENDMENU=$DIR/tuxdax; rm /tmp/dax_res2; rm /tmp/dax_res1; rm /tmp/dax_res" >> /tmp/dax_res2
  else
        echo "ENDMENU=rm /tmp/dax_res2; rm /tmp/dax_res1; rm /tmp/dax_res" >> /tmp/dax_res2
  fi
  /var/plugins/shellexec /tmp/dax_res2

;;

#____________________________________________________________
##   ADD Stock                                             <<
#############################################################
add_stock)

  ST_ID="$5 $6 $7 $8"
  echo "ACTION=&$ST_ID, $DIR/tuxdax get_charts $2 $3 $4, Kurse werden geladen und berechnet" >> $DIR/tuxdax_stocks
  echo "ACTION=&$ST_ID, grep -v $2 $DIR/tuxdax_values > /tmp/tuxdax_values; grep -v $3 $DIR/tuxdax_stocks > /tmp/tuxdax_stocks; grep -v $3 $DIR/tuxdax_2kill > /tmp/tuxdax_2kill; cat /tmp/tuxdax_values > $DIR/tuxdax_values; cat /tmp/tuxdax_stocks > $DIR/tuxdax_stocks; cat /tmp/tuxdax_2kill > $DIR/tuxdax_2kill, Daten werden gel~oscht" >> $DIR/tuxdax_2kill

;;

#____________________________________________________________
##   Set Values                                            <<
#############################################################
set_value)

  if [ $3 = "New" ]; then
        NEW="    1     00"
        GHPL="2"
  else
        GIVEN=$(grep $2"=" $DIR/tuxdax_values | cut -d "=" -f 2)
        msgbox msg="~caktuellen Depotbestand~n~s" title="none" select=" Aufstocken , Editieren " default="2"
        UPG=$?
        case "$UPG" in
              1|2)
                UPG=$UPG
              ;;
              *)
                msgbox popup="~Fkeine Daten geschrieben" title="none"
                if [ $4 ]; then
                        $0 depot
                fi
                exit 1
              ;;
        esac
                
        ###-> Given Handelsplatz <-###
        GHPL=$(echo $GIVEN | cut -d "-" -f 3-)
        case "$GHPL" in
              H~ochstpreis)
                GHPL="1"
                ;;
              Frankfurt)
                GHPL="3"
                ;;
              M~unchen)
                GHPL="4"
                ;;
              D~usseldorf)
                GHPL="5"
                ;;
              Berlin-Bremen)
                GHPL="6"
                ;;
              Hannover)
                GHPL="7"
                ;;
              Hamburg)
                GHPL="8"
                ;;
              Stuttgart)
                GHPL="9"
                ;;
              XETRA|*)
                GHPL="2"
                ;;
        esac
        
        ###-> Given Cash-Kurs <-###
        NC=$(expr 7 - "$(expr length `echo "$GIVEN" | cut -d "-" -f -1`)" )
        case "$NC" in
              1)
                NEW_CASH=" "
                ;;
              2)
                NEW_CASH="  "
                ;;
              3)
                NEW_CASH="   "
                ;;
              4)
                NEW_CASH="    "
                ;;
        esac
        
        ###-> Given Depotbestand <-###
        NM=$(expr 5 - "$(expr length `echo "$GIVEN" | cut -d "-" -f 2`)" )
        case "$NM" in
              1)
                NEW_MENGE=" "
                ;;
              2)
                NEW_MENGE="  "
                ;;
              3)
                NEW_MENGE="   "
                ;;
              4)
                NEW_MENGE="    "
                ;;
        esac
        NEW=$NEW_MENGE$(echo $GIVEN | cut -d "-" -f 2)$NEW_CASH$(echo $GIVEN | cut -d "-" -f -1)
  fi
  
  ###-> Depotbestand definieren <-###
  if [ $UPG = 1 ]; then
        NEW="    1     00"
        AUF=" aufstocken!"
  fi
  VAL=`input l="St~uck:    #####Kurs:  #####,## 0 Stk = Reset" t="~YEinstiegskurs  und  Depotbestand$AUF" m=0 k=1 c=15 d="$NEW"`
  if [ -z $VAL ]; then
        msgbox popup="~Fkeine Daten geschrieben" title="none"
        if [ $4 ]; then
                $0 depot
        fi
        exit 1
  else
        ###-> Depotbestand entfernen <-###
        MENGE=$(echo $VAL | cut -d ' ' -f -1 | sed s/' '//g | sed -e 's/^000//g' -e 's/^00//g' -e 's/^0//g' -e 's/^ 0//g')  
        if [ "$MENGE" -eq "0" ]; then
                msgbox msg="~cSollen die Werte des Depotbestands~n~cf~ur dieses Wertpapiers entfernt werden?" select=" Entfernen , ~BAbbruch , Zur~uck" default="2" title="none" size="40"
                KILL=$?
                case "$KILL" in
                      
                      ###-> Entfernen <-###
                      1)
                        grep -v $2 $DIR/tuxdax_values > /tmp/tuxdax_values;
                        cat /tmp/tuxdax_values > $DIR/tuxdax_values
                        rm /tmp/tuxdax_values
                        msgbox popup="~FWerte dieses Depotbestands wurden entfernt" title="none"
                        if [ $4 ]; then
                                $0 depot
                        fi
                        exit 1
                      ;;
                      
                      ###-> Back <-###
                      3)
                        $0 $1 $2 $3 $4
                        exit 1
                      ;;
                      
                      ###-> Abbruch <-###
                      2|*)
                        msgbox popup="~Fkeine Daten geschrieben" title="none"
                        if [ $4 ]; then
                                $0 depot
                        fi
                        exit 1
                      ;;                      
                esac                      
        else
                ###-> Validierung der Eingabe <-###
                EURO1=$(echo $VAL | cut -d ' ' -f 2- | sed s/' '//g | sed -e 's/^000//g' -e 's/^00//g' -e 's/^0//g' -e 's/^ 0//g')
                EURO=$(expr substr "$EURO1" 1 $(expr `expr length "$EURO1"` - 2))
                CENT=`expr substr "$VAL" 11 2`
                TOTAL=`expr "$EURO$CENT" \* $MENGE`
                if [ "$TOTAL" -eq "" -o "0" -gt "$TOTAL" ]; then
                      msgbox popup="~c~FUng~ultige Eingabe!~n~t~nbitte erneut versuchen" title="none"
                      $0 $1 $2 $3 $4
                      exit 1
                else
                      ###-> Handelsplatz definieren <-###
                      msgbox msg="~cHandelsplatz f~ur Gewinn- / Verlustberechnung festlegen~n~s" select="  ~YEgal - H~ochstpreis!  ,XETRA,Frankfurt,M~unchen,D~usseldorf,Berlin-Bremen,Hannover,Hamburg,Stuttgart" order="3" default="$GHPL" title="none"
                      HPLI=$?
                      case "$HPLI" in
                      
                          ###-> Abbruch <-###
                          0)
                            msgbox popup="~Fkeine Daten geschrieben" title="none"
                            if [ $4 ]; then
                                    $0 depot
                            fi
                            exit 1
                          ;;
                          
                          ###-> convert Handelsplatz Eingabe <-###
                          *)                      
                            case "$HPLI" in
                                  1)
                                    HPL="H~ochstpreis"
                                    ;;
                                  2)
                                    HPL="XETRA"
                                    ;;
                                  3)
                                    HPL="Frankfurt"
                                    ;;
                                  4)
                                    HPL="M~unchen"
                                    ;;
                                  5)
                                    HPL="D~usseldorf"
                                    ;;
                                  6)
                                    HPL="Berlin-Bremen"
                                    ;;
                                  7)
                                    HPL="Hannover"
                                    ;;
                                  8)
                                    HPL="Hamburg"
                                    ;;
                                  9)
                                    HPL="Stuttgart"
                                    ;;
                            esac
                    
                            ###-> Ausgabe <-###
                            TOTAL_E=$(expr substr "$TOTAL" 1 $(expr `expr length "$TOTAL"` - 2) )
                            TOTAL_C=$(expr substr "$TOTAL" $(expr `expr length "$TOTAL"` - 1) 2)
                            if [ $UPG = 1 ]; then
                                NM=$(echo $GIVEN | cut -d "-" -f 2)
                                NC=$(echo $GIVEN | cut -d "-" -f -1)
                                OLDTOTAL=`expr $NC \* $NM`
                                UPGTOTAL=`expr $NC \* $NM + $TOTAL`
                                UPGMENGE=`expr $NM + $MENGE`
                                UPGCASH=`expr $UPGTOTAL / $UPGMENGE`
                                UPGEURO=$(expr substr "$UPGCASH" 1 $(expr `expr length "$UPGCASH"` - 2) )
                                UPGCENT=$(expr substr "$UPGCASH" $(expr `expr length "$UPGCASH"` - 1) 2)
                                UPGTOTAL_E=$(expr substr "$UPGTOTAL" 1 $(expr `expr length "$UPGTOTAL"` - 2) )
                                UPGTOTAL_C=$(expr substr "$UPGTOTAL" $(expr `expr length "$UPGTOTAL"` - 1) 2)
                                OLDEURO=$(expr substr "$NC" 1 $(expr `expr length "$NC"` - 2) )
                                OLDCENT=$(expr substr "$NC" $(expr `expr length "$NC"` - 1) 2)
                                OLDTOTAL_E=$(expr substr "$OLDTOTAL" 1 $(expr `expr length "$OLDTOTAL"` - 2) )
                                OLDTOTAL_C=$(expr substr "$OLDTOTAL" $(expr `expr length "$OLDTOTAL"` - 1) 2)
                                msgbox msg="~cf~ur  "$HPL"  berechnen~n~n~T040~S altes Depot~T250~Y Aufstockung~T450~S neues Depot~n~n~T040~S"$NM"   Stk.  ~T250~Y"$MENGE"   Stk.  ~T450~S "$UPGMENGE"   Stk.  ~n~T040~S"$OLDEURO" , "$OLDCENT"   EUR  ~T250~Y"$EURO" , "$CENT"   EUR  ~T450~S "$UPGEURO" , "$UPGCENT"   EUR  ~n~s~n~T040~S"$OLDTOTAL_E" , "$OLDTOTAL_C"   EUR  ~T250~Y"$TOTAL_E" , "$TOTAL_C"   EUR~T450~S "$UPGTOTAL_E" , "$UPGTOTAL_C"   EUR  " title="~Yneue Depotwerte ~ubernehmen?" size="30" select=" OK , Edit "
                            else
                                msgbox msg="~cf~ur  "$HPL"  berechnen~n~n~r"$MENGE"   Stk.  ~n~r"$EURO" , "$CENT"   EUR  ~n~s~n~r"$TOTAL_E" , "$TOTAL_C"   EUR" title="~YDepotwerte ~ubernehmen?" select=" OK , Edit "
                            fi
                            key=$?
                            case "$key" in
                                    
                                  ###-> ok <-###
                                  1)
                                    grep -v $2 $DIR/tuxdax_values > /tmp/tuxdax_values
                                    if [ $UPG = 1 ]; then
                                        echo $2"="$UPGEURO$UPGCENT"-"$UPGMENGE"-"$HPL >> /tmp/tuxdax_values
                                    else
                                        echo $2"="$EURO$CENT"-"$MENGE"-"$HPL >> /tmp/tuxdax_values
                                    fi
                                    cat /tmp/tuxdax_values > $DIR/tuxdax_values
                                    rm /tmp/tuxdax_values
                                    msgbox popup="~c~FDepotbestand wurde gespeichert" title="none"
                                    if [ $4 ]; then
                                            $0 depot
                                    fi
                                  ;;
                                    
                                  ###-> edit <-###
                                  2)
                                    $0 $1 $2 $3 $4
                                  ;;
                                    
                                  ###-> cancel <-###
                                  *)
                                    msgbox popup="~c~Fkeine Daten geschrieben" title="none"
                                    if [ $4 ]; then
                                            $0 depot
                                    fi
                                    exit 1
                                  ;;
                            esac
                          ;;
                      esac
                fi
        fi
  fi

;;


#____________________________________________________________
##   Get Charts                                            <<
#############################################################
get_charts)

  wget -q -O /tmp/dax_res $HTTP$4$SUCHA$2%2C+$3
  TIM=$(echo $TIM | cut -d '=' -f 2-)
  
  echo "FONT=/share/fonts/micron_bold.ttf" > /tmp/dax_res2
  if [ $TIM -eq "0" ]; then
        echo "FONTSIZE=18" >> /tmp/dax_res2
        SPA="~t~t"
        MON="EUR"
  else
        echo "FONTSIZE=17" >> /tmp/dax_res2
        SPA="~t"
        MON="E"
        grep '<td align="center">' /tmp/dax_res | sed s/'<td align="center">'//g | sed s/'<\/td>'//g | sed s/'				'//g | sed 1,3d > /tmp/dax_time
  fi
  echo "LINESPP=15" >> /tmp/dax_res2
  echo "WIDTH=800" >> /tmp/dax_res2
  echo "HEIGHT=700" >> /tmp/dax_res2
  
  ###-> Fix and Search <-###
  cat /tmp/dax_res | sed s/'colspan="4" align="left"><a'/'colspan="1" align="ri'/g | sed s/'colspan="2" align="right"'/'colspan="1" align="ri'/g | grep 'colspan="1" align="ri' | cut -d '>' -f 2- | cut -d '<' -f -1 | sed s/'('//g | sed s/')'//g | sed s/','/'.'/g > /tmp/dax_res1t
  cat /tmp/dax_res | sed s/'euro;<\/strong>'/'MarketCode'/g | sed s/'class="plus"'/'MarketCode'/g | sed s/'class="minus"'/'MarketCode'/g | sed s/'<td align="right"><span'/'<span'/g | sed s/'Stuttgart'/'Stuttgart~t'/g | sed s/'XETRA'/'XETRA~t'/g | sed s/'Berlin'/'Bln.'/g | grep 'MarketCode' | cut -d '>' -f 3- | cut -d '<' -f -1 | sed s/'\&MarketCode'/$MON/g | sed s/'\&euro;\&nbsp;'/$MON/g | sed s/'\&nbsp;'//g | sed s/^+/'~G+'/g | sed s/^-/'~Y-'/g  | sed s/','/'.'/g | sed s/'('//g | sed s/')'//g > /tmp/dax_res1
    
  ###-> allgemeine Daten erstellen <-###
  while
        read line1
  do
        read line2
        read line3
        read line4
        echo "MENU=$line1" >> /tmp/dax_res2
        echo "COMMENT=ISIN: $line2 ~t~t WKN: $line4 ~t~t Symbol: $line3 ~t Typ: $4" >> /tmp/dax_res2
        echo "COMMENT=*" >> /tmp/dax_res2
        echo "ACTION=*&  ~B+  +  +   ~SNews und Analysen   ~B+  +  +, "$DIR"/tuxdax get_stock_news $line2, News werden geladen" >> /tmp/dax_res2
        echo "COMMENT=" >> /tmp/dax_res2
        
        ###-> get Depot-Parameters <-###
        CASH=$(grep $line2 $DIR/tuxdax_values | cut -d "=" -f 2-)
        if [ ! -z $CASH ]; then
                CASH_E=$(expr substr "$CASH" 1 $(expr "$(expr length `echo "$CASH" | cut -d "-" -f -1`)" - 2))
                CASH_C=$(expr substr "$CASH" $(expr "$(expr length `echo "$CASH" | cut -d "-" -f -1`)" - 1) 2)
                CASH_M=$(echo "$CASH" | cut -d "-" -f 2)
                CASH_T=`expr "$CASH_E$CASH_C" \* $CASH_M`
                CASH_TE=$(expr substr "$CASH_T" 1 $(expr `expr length "$CASH_T"` - 2) )
                CASH_TC=$(expr substr "$CASH_T" $(expr `expr length "$CASH_T"` - 1) 2)
                KUD="ACTION=&~BDepot: ~S"$CASH_M" ~t~t ~S"$CASH_E"."$CASH_C" EUR ~t~B>>~t~S "
                KUDA=","$DIR"/tuxdax set_value "$line2" Old, Depotbestand wird geladen"
                HPL=$(echo $CASH | cut -d "-" -f 3-)
         else
                KUD="ACTION=&~B>> ~S Einstiegskurs und Depotbestand festlegen,"$DIR"/tuxdax set_value "$line2" New, Depotbestand wird angelegt"
        fi
  done < /tmp/dax_res1t
  
  ###-> Kurse berechnen <-###
  WOA="0"
  BOK="0"
  BEST_SALE="0"
  while
        read line1
  do
        read line2
        read line3
        read line4
        
        ###-> Last Trade <-###
        if [ $TIM -eq "1" ]; then
                WOA=`expr "$WOA" + 1`
                case "$WOA" in
                      1)
                        ZT=" ~S "$( sed -n '1 p' /tmp/dax_time)"  /  "$( sed -n '2 p' /tmp/dax_time)
                        ;;
                      2)
                        ZT=" ~S "$( sed -n '3 p' /tmp/dax_time)"  /  "$( sed -n '4 p' /tmp/dax_time)
                        ;;
                      3)
                        ZT=" ~S "$( sed -n '5 p' /tmp/dax_time)"  /  "$( sed -n '6 p' /tmp/dax_time)
                        ;;
                      4)
                        ZT=" ~S "$( sed -n '7 p' /tmp/dax_time)"  /  "$( sed -n '8 p' /tmp/dax_time)
                        ;;
                      5)
                        ZT=" ~S "$( sed -n '9 p' /tmp/dax_time)"  /  "$( sed -n '10 p' /tmp/dax_time)
                        ;;
                      6)
                        ZT=" ~S "$( sed -n '11 p' /tmp/dax_time)"  /  "$( sed -n '12 p' /tmp/dax_time)
                        ;;
                      7)
                        ZT=" ~S "$( sed -n '13 p' /tmp/dax_time)"  /  "$( sed -n '14 p' /tmp/dax_time)
                        ;;
                      8)
                        ZT=" ~S "$( sed -n '15 p' /tmp/dax_time)"  /  "$( sed -n '16 p' /tmp/dax_time)
                        ;;
                      9)
                        ZT=" ~S "$( sed -n '17 p' /tmp/dax_time)"  /  "$( sed -n '18 p' /tmp/dax_time)
                        ;;
                esac          
        fi          
        
        ###-> Handelsplatz Chart-Code <-###
        case "$line1" in
            XETRA~t)
                BORSE="ETR"
                HSEX=$(echo $line2 | cut -d "." -f -1)
                HSCX=$(echo $line2 | cut -d "." -f 2- | cut -d " " -f -1)
                BEST_SALEX="$HSEX$HSCX"
                BESTX=$(echo $line2 | sed 's/E$/EUR/g')
                if [ $HPL = "XETRA" ]; then
                        HSE=$HSEX
                        HSC=$HSCX
                        BEST_SALE="$HSEX$HSCX"
                        BEST=$BESTX
                        BOK="1"
                fi
              ;;
            Frankfurt)
                BORSE="FSE"
                if [ $HPL = "Frankfurt" ]; then
                        HSE=$(echo $line2 | cut -d "." -f -1)
                        HSC=$(echo $line2 | cut -d "." -f 2- | cut -d " " -f -1)
                        BEST_SALE="$HSE$HSC"
                        BEST=$(echo $line2 | sed 's/E$/EUR/g')
                        BOK="1"
                fi
              ;;
            Mnchen)
                BORSE="MUN"
                if [ $HPL = "M~unchen" ]; then
                        HSE=$(echo $line2 | cut -d "." -f -1)
                        HSC=$(echo $line2 | cut -d "." -f 2- | cut -d " " -f -1)
                        BEST_SALE="$HSE$HSC"
                        BEST=$(echo $line2 | sed 's/E$/EUR/g')
                        BOK="1"
                fi
              ;;
            Hamburg)
                BORSE="HAM"
                if [ $HPL = "Hamburg" ]; then
                        HSE=$(echo $line2 | cut -d "." -f -1)
                        HSC=$(echo $line2 | cut -d "." -f 2- | cut -d " " -f -1)
                        BEST_SALE="$HSE$HSC"
                        BEST=$(echo $line2 | sed 's/E$/EUR/g')
                        BOK="1"
                fi
              ;;
            Hannover)
                BORSE="HAN"
                if [ $HPL = "Hannover" ]; then
                        HSE=$(echo $line2 | cut -d "." -f -1)
                        HSC=$(echo $line2 | cut -d "." -f 2- | cut -d " " -f -1)
                        BEST_SALE="$HSE$HSC"
                        BEST=$(echo $line2 | sed 's/E$/EUR/g')
                        BOK="1"
                fi
              ;;
            Dsseldorf)
                BORSE="DUS"
                if [ $HPL = "D~usseldorf" ]; then
                        HSE=$(echo $line2 | cut -d "." -f -1)
                        HSC=$(echo $line2 | cut -d "." -f 2- | cut -d " " -f -1)
                        BEST_SALE="$HSE$HSC"
                        BEST=$(echo $line2 | sed 's/E$/EUR/g')
                        BOK="1"
                fi
              ;;
            Bln.-Bremen)
                BORSE="BER"
                if [ $HPL = "Berlin-Bremen" ]; then
                        HSE=$(echo $line2 | cut -d "." -f -1)
                        HSC=$(echo $line2 | cut -d "." -f 2- | cut -d " " -f -1)
                        BEST_SALE="$HSE$HSC"
                        BEST=$(echo $line2 | sed 's/E$/EUR/g')
                        BOK="1"
                fi
              ;;
            Stuttgart)
                BORSE="STU"
                if [ $HPL = "Stuttgart" ]; then
                        HSE=$(echo $line2 | cut -d "." -f -1)
                        HSC=$(echo $line2 | cut -d "." -f 2- | cut -d " " -f -1)
                        BEST_SALE="$HSE$HSC"
                        BEST=$(echo $line2 | sed 's/E$/EUR/g')
                        BOK="1"
                fi
              ;;
        esac
        
        if [ $HPL = "H~ochstpreis" ]; then
                HSE=$(echo $line2 | cut -d "." -f -1)
                HSC=$(echo $line2 | cut -d "." -f 2- | cut -d " " -f -1)
                HIGH_SALE="$HSE$HSC"
                if [ $HIGH_SALE -gt $BEST_SALE ]; then
                        BEST_SALE=$HIGH_SALE
                        BEST=$(echo $line2 | sed 's/E$/EUR/g')
                        BOK="1"
                fi
        fi
                
        echo "ACTION=&$line1 ~t~t $line2 $SPA $line3 $SPA $line4 $SPA $ZT,/var/plugins/tuxwet/tuxwetter 'PICTURE=Chart anzeigen,|GIF|http://aktchk.is-asp.com/test/flex.chart?cyside=2&$cside&$cname&legend=0&sTopYValOff=1&$sBrushWidthStock&$sBrushWidthVol&$sBrushWidthMisc&$timespan&$volumen&symbol=$3.$BORSE&sizex=450&sizey=300&$charttype&$sColorChart&$cname_COLOR'" >> /tmp/dax_res2
  done < /tmp/dax_res1
  
  ###-> Bottom Line <-###
  if [ ! -z $CASH ]; then
        if [ $BOK = "0" ]; then
                HPL="XETRA  statt "$HPL" !!"
                HSE=$HSEX
                HSC=$HSCX
                BEST_SALE=$BEST_SALEX
                BEST=$BESTX
        fi
        MARGE_A=`expr $BEST_SALE \* $CASH_M - $CASH_TE$CASH_TC`
        MARGE_E=$(echo $(expr substr "$MARGE_A" 1 $(expr `expr length "$MARGE_A"` - 2) ) | sed s/'-'/'~Y-'/g)
        if [ $MARGE_E -gt "0" ]; then
                MARGE_E="~G+"$MARGE_E
        fi
        MARGE_C=$(expr substr "$MARGE_A" $(expr `expr length "$MARGE_A"` - 1) 2)
        echo "COMMENT=" >> /tmp/dax_res2
        echo $KUD$BEST" ~t "$MARGE_E"."$MARGE_C" EUR"$KUDA >> /tmp/dax_res2
        echo "DEPENDON=+ "$CASH_TE"."$CASH_TC"  EUR  ~t gekauft bei  ~t~t Kurs f~ur  "$HPL",/tmp/dax_res2, echo 2" >> /tmp/dax_res2
  else
        echo "COMMENT=" >> /tmp/dax_res2
        echo $KUD >> /tmp/dax_res2
  fi
  echo "ENDMENU" >> /tmp/dax_res2
  echo "ENDMENU=rm /tmp/dax_res2; rm /tmp/dax_res1; rm /tmp/dax_res; rm /tmp/dax_res1t; rm /tmp/dax_time" >> /tmp/dax_res2
  /var/plugins/shellexec /tmp/dax_res2
  
;;


#____________________________________________________________
##   Do Config                                             <<
#############################################################
do_config)

  case "$2" in
          timespan)
                IN=$(echo $timespan | cut -d '=' -f 2-)
                INFO="Zeitraum @@   i=Heute"
                LIMIT="12"
              ;;
          sBrushWidthStock)
                IN=$(echo $sBrushWidthStock | cut -d '=' -f 2-)
                INFO="Breite #px 3=Standard"
                LIMIT="11"
              ;;
          sBrushWidthVol)
                IN=$(echo $sBrushWidthVol | cut -d '=' -f 2-)
                INFO="Breite #px 2=Standard"
                LIMIT="11"
              ;;
          volumen)
                IN=$(echo $volumen | cut -d '=' -f 2-)
                INFO="Anzahl der Charts: #      0 bis 2"
                LIMIT="20"
              ;;
          cside)
                IN=$(echo $cside | cut -d '=' -f 2-)
                INFO="vertikale Skala: # 1=Rechts  0=Links"
                LIMIT="18"
              ;;
          cname)
                IN=$(echo $cname | cut -d '=' -f 2-)
                INFO="Namesstatus: #  1=AN 0=AUS  "
                LIMIT="14"
              ;;
          sColorText)
                IN=$(expr substr $(echo $sColorText | cut -d '=' -f 2) 3 6)
                INFO="Farbe: @@@@@@  Hexadezimal"
                LIMIT="14"
                AD="0x"
              ;;
          cname_COLOR)
                IN=$(expr substr $(echo $sColorChart | cut -d '=' -f 2) 3 6)
                INFO="Farbe: @@@@@@  Hexadezimal"
                LIMIT="14"
                AD="0x"
              ;;
          sBrushWidthMisc)
                IN=$(echo $sBrushWidthMisc | cut -d '=' -f 2-)
                INFO="Breite #px 3=Standard"
                LIMIT="11"
              ;;
          charttype)
                IN=$(echo $charttype | cut -d '=' -f 2-)
                INFO="Chart-Type ##  1=Standard "
                LIMIT="13"
              ;;
          sColorChart)
                IN=$(expr substr $(echo $sColorChart | cut -d '=' -f 2) 3 6)
                INFO="Farbe: @@@@@@  Hexadezimal"
                LIMIT="14"
                AD="0x"
              ;;
          TIM)
                IN=$(echo $TIM | cut -d '=' -f 2-)
                INFO="Last Trade: #   1=AN 0=AUS  "
                LIMIT="14"
              ;;      
  esac
  
  CONF=`input l="$INFO" t="~YTuxDax  -  Chart-Konfigurator" m=0 k=1 c=$LIMIT d=$IN`
  if [ -z $CONF ]; then
        $0
  else
        CONF=$(echo $CONF | sed s/' '//g)
        cat $DIR/tuxdax_config | sed -e s/$2=$AD$IN/$2=$AD$CONF/g > $DIR/tuxdax_config
        $0
  fi
  
;;


#____________________________________________________________
##   Kurs Liste                                            <<
#############################################################
kurs_list)

  wget -q -O /tmp/dax_resa $HTTPK$2
  cat /tmp/dax_resa | sed -e s/'<td colspan="2" align="left" >'/'<td colspan="2" align="left" class="sup">'/g -e s/'<td align="right" >'/'<td align="right" class="sup">'/g -e s/'<td align="left" >'/'<td align="left" class="sup">'/g -e s/'<td align="center" >'/'<td align="center" class="sup">'/g -e s/'<\/span><\/span>'/'<class="sup" span>'/g | grep 'class="sup"' > /tmp/dax_resa1
  
  echo "FONT=/share/fonts/micron_bold.ttf" > /tmp/dax_resa2
  echo "FONTSIZE=18" >> /tmp/dax_resa2
  echo "LINESPP=17" >> /tmp/dax_resa2
  echo "WIDTH=550" >> /tmp/dax_resa2
  echo "HEIGHT=500" >> /tmp/dax_resa2
  echo "MENU=aktuelle ~Y"$3"~S-Kursliste" >> /tmp/dax_resa2
  while
        read line1
  do
        read line2
        read line3
        read line4
        read line5
        read line6
        read line7
        NAME=$(echo $line1 | cut -d ">" -f 4- | cut -d "<" -f -1 | sed s/'('//g | sed s/')'//g | sed s/','/'.'/g)
        NAME2=$(echo $NAME | sed s/' '/'+'/g)
        PRICE=$(echo $line2 | cut -d ">" -f 3- | cut -d "&" -f -1 | sed s/','/'.'/g)
        DIF=$(echo $line3 | cut -d ">" -f 3- | cut -d "&" -f -1 | sed s/'-'/'~Y-'/g | sed s/'+'/'~G+'/g | sed s/','/'.'/g)
        PROZ=$(echo $line4 | cut -d ">" -f 2- | cut -d "&" -f -1 | sed s/'-'/'~Y-'/g | sed s/'+'/'~G+'/g | sed s/','/'.'/g)
        echo "DEPENDON=+"$NAME",/tmp/dax_resa2, echo 2" >> /tmp/dax_resa2
        echo "ACTION=*&~t"$PRICE" EUR ~t~t"$DIF" EUR ~t~t"$PROZ","$DIR"/tuxdax find_stock STK "$NAME2",~Ubernahme wird vorbereitet" >> /tmp/dax_resa2
  done < /tmp/dax_resa1
  
  echo "ENDMENU=rm /tmp/dax_resa2; rm /tmp/dax_resa1; rm /tmp/dax_resa" >> /tmp/dax_resa2
  /var/plugins/shellexec /tmp/dax_resa2

;;


#____________________________________________________________
##   Stock-News und Analysen                               <<
#############################################################
get_stock_news)

  wget -q -O /tmp/dax_resn $HTTPN$2
  cat /tmp/dax_resn | sed s/'width="57"'/'\&menue'/g | grep "&menue" > /tmp/dax_resn1
  sed 1d /tmp/dax_resn1 > /tmp/dax_resn1a
  
  echo "FONT=/share/fonts/micron_bold.ttf" > /tmp/dax_resn2
  echo "FONTSIZE=18" >> /tmp/dax_resn2
  echo "LINESPP=12" >> /tmp/dax_resn2
  echo "WIDTH=700" >> /tmp/dax_resn2
  echo "HEIGHT=500" >> /tmp/dax_resn2
  echo "MENU=~Y+  +  +   ~SNews und Analysen   ~Y +  +  +" >> /tmp/dax_resn2
  
  while
        read line1
  do
        read line2
        echo "ACTION=&"$(echo $line1 | cut -d ">" -f 2- | cut -d "<" -f -1)"  -  "$(echo $line2 | cut -d "?" -f 2- | cut -d "<" -f -1 | sed -e s/''/'~u'/g -e s/''/'~a'/g | cut -d ">" -f 2-)", /var/plugins/tuxwet/tuxwetter 'TXTHTML=~Y"$(echo $line2 | cut -d "?" -f 2- | cut -d "<" -f -1 | sed -e s/''/'~u'/g -e s/''/'~a'/g | cut -d ">" -f 2-)",http://www.aktiencheck.de/news/drucke_news.m?"$(echo $line2 | cut -d "?" -f 2- | cut -d "&" -f -1 )"|<p ALIGN=\"JUSTIFY\">|</TD></TR>', News werden gelesen" >> /tmp/dax_resn2
  done < /tmp/dax_resn1a
  
  echo "ENDMENU=rm /tmp/dax_resn; rm /tmp/dax_resn1; rm /tmp/dax_resn1a; rm /tmp/dax_resn2" >> /tmp/dax_resn2
  /var/plugins/shellexec /tmp/dax_resn2
  
;;


#____________________________________________________________
##   News und Analysen                                     <<
#############################################################
get_news)

  wget -q -O /tmp/dax_resn $URL$2
  cat /tmp/dax_resn | sed s/'nachrichten_content2'/' href="\/artikel\/ '/g | grep 'href="/artikel/' > /tmp/dax_resn1
  
  echo "FONT=/share/fonts/micron_bold.ttf" > /tmp/dax_resn2
  echo "FONTSIZE=18" >> /tmp/dax_resn2
  echo "LINESPP=12" >> /tmp/dax_resn2
  echo "WIDTH=700" >> /tmp/dax_resn2
  echo "HEIGHT=500" >> /tmp/dax_resn2
  echo "MENU="$3" "$4 >> /tmp/dax_resn2
  
  while
        read line1
  do
        read line2
        echo "ACTION=&"$(echo $line1 | cut -d ">" -f 2- | cut -d "," -f -1)" - "$(echo $line2 | cut -d ">" -f 2- | cut -d "<" -f -1 | sed -e s/''/'~u'/g -e s/''/'~a'/g )", /var/plugins/tuxwet/tuxwetter 'TXTHTML=~Y"$(echo $line2 | cut -d ">" -f 2- | cut -d "<" -f -1 | sed -e s/''/'~u'/g -e s/''/'~a'/g )",http://www.aktiencheck.de/news/drucke_news.m?id="$(echo $line2 | cut -d "." -f -1 | cut -d "-" -f 3 )"|<p ALIGN=\"JUSTIFY\">|</TD></TR>', News werden gelesen" >> /tmp/dax_resn2
  done < /tmp/dax_resn1
  
  echo "ENDMENU=rm /tmp/dax_resn; rm /tmp/dax_resn1; rm /tmp/dax_resn2" >> /tmp/dax_resn2
  /var/plugins/shellexec /tmp/dax_resn2
    
;;


#____________________________________________________________
##   Kurs und Depotbestand                                 <<
#############################################################
depot)

  echo "FONT=/share/fonts/micron_bold.ttf" > /tmp/dax_depot
  echo "FONTSIZE=18" >> /tmp/dax_depot
  echo "LINESPP=12" >> /tmp/dax_depot
  echo "WIDTH=700" >> /tmp/dax_depot
  echo "HEIGHT=500" >> /tmp/dax_depot
  echo "MENU=Einstiegskurs und Depotbestand" >> /tmp/dax_depot

  while
        read line1
  do
        echo "DEPENDON=+"$(echo $line1 | cut -d "=" -f 2- | cut -d "," -f -1)",/tmp/dax_resa2, echo 2" >> /tmp/dax_depot
        ISIN=$(echo $line1 | cut -d "," -f 2- | cut -d " " -f 4)
        CASH=$(grep $ISIN $DIR/tuxdax_values | cut -d "=" -f 2-)
        if [ ! -z $CASH ]; then
                CASH_E=$(expr substr "$CASH" 1 $(expr "$(expr length `echo "$CASH" | cut -d "-" -f -1`)" - 2))
                CASH_C=$(expr substr "$CASH" $(expr "$(expr length `echo "$CASH" | cut -d "-" -f -1`)" - 1) 2)
                CASH_M=$(echo "$CASH" | cut -d "-" -f 2)
                CASH_T=`expr "$CASH_E$CASH_C" \* $CASH_M`
                CASH_TE=$(expr substr "$CASH_T" 1 $(expr `expr length "$CASH_T"` - 2) )
                CASH_TC=$(expr substr "$CASH_T" $(expr `expr length "$CASH_T"` - 1) 2)
                echo "ACTION=&~BDepot: ~S"$CASH_M" Stk.~t~t~S"$CASH_E"."$CASH_C" EUR~t~t~BTotal: ~S"$CASH_TE"."$CASH_TC" EUR,"$DIR"/tuxdax set_value "$ISIN" Old M, Depotbestand wird geladen" >> /tmp/dax_depot
                echo "COMMENT=*Gewinn- / Verlustberechnung f~ur  "$(echo $CASH | cut -d "-" -f 3-) >> /tmp/dax_depot
         else
                echo "ACTION=*&~B>> ~S Einstiegskurs und Depotbestand festlegen,"$DIR"/tuxdax set_value "$ISIN" New M, Depotbestand wird geladen" >> /tmp/dax_depot
         fi
  done < $DIR/tuxdax_stocks
  
  echo "ENDMENU=/var/plugins/shellexec $DIR/tuxdax_menu; rm /tmp/dax_depot" >> /tmp/dax_depot
  /var/plugins/shellexec /tmp/dax_depot

;;


#____________________________________________________________
##   TuxDAX                                                <<
#############################################################
*)
 
  /var/plugins/shellexec $DIR/tuxdax_menu

  exit 1
  esac