#!/bin/sh
#modify from dwm-bar
#origin source:https://github.com/joestandring/dwm-bar
COUNT=0
INTERFACE=$(ip addr|grep UP|grep -v lo|awk -F: '{print $2}'|awk '$1=$1'|head -n 1)
dwm_tor()
{
    NUM=$(ss -tunp|grep -E "127.0.0.1:9150|127.0.0.1:9050|127.0.0.1:9151"|wc -l)
    if [ $NUM -ne 0 ]; then
        echo "[ TOR:ON ]"
    else
        echo "[ TOR:OFF ]"
    fi
}
dwm_vpn()
{
    NUM=$(ip route|grep tun0|wc -l)
    if [ $NUM -eq 3 ]; then
        echo "[ VPN:ON ]"
    else
        echo "[ VPN:OFF ]"
    fi
}

dwm_upspeed(){
  T1=$(cat /sys/class/net/${INTERFACE}/statistics/tx_bytes)
  sleep 1
  T2=$(cat /sys/class/net/${INTERFACE}/statistics/tx_bytes)
  TBPS=$(expr $T2 - $T1)
  if [ ${TBPS} == 0 ];then
      printf  "[  ⇑ "
  else
      printf  "[ ⇧ "
  fi
}
dwm_downspeed()
{
    R1=$(cat /sys/class/net/${INTERFACE}/statistics/rx_bytes)
    sleep 1
    R2=$(cat /sys/class/net/${INTERFACE}/statistics/rx_bytes)
    RBPS=$(expr $R2 - $R1)
    if [ ${RBPS} == 0 ];then
        printf  "| ⇓  ]"
    else
        printf  "| ⇩ ]"
    fi
}

dwm_storage()
{
    while IFS=":" read -r a b; do
        case $a in
            "MemTotal") ((mem_used+=${b/kB})); mem_total="${b/kB}" ;;
            "Shmem") ((mem_used+=${b/kB}))  ;;
            "MemFree" | "Buffers" | "Cached" | "SReclaimable")
              mem_used="$((mem_used-=${b/kB}))"
            ;;
        esac
    done < /proc/meminfo

    mem_used="$((mem_used / 1024))"
    mem_total="$((mem_total / 1024))"
    echo [MEM:${mem_used}MiB/${mem_total}MiB]
}

dwm_dns()
{
   STATUS=$(cat /etc/resolv.conf|grep '127.0.0.1'|wc -l)
   if [ ${STATUS} == 1 ];then
       echo "[ SDNS|ON ]"
   else
       echo "[ SDNS|OFF ]"
   fi
}

dwm_alsa()
{
    STATUS=$(amixer get Master | tail -n1 | sed -r "s/.*\[(.*)\]/\1/")
    VOL=$(amixer get Master | tail -n1 | sed -r "s/.*\[(.*)%\].*/\1/")
    printf "[ VOL:%s|%s ]" ${VOL} ${STATUS^^}
}

dwm_monitor()
{
    DATA=$(ss -tap|grep -E "EST|CLOSE-WAIT"|grep -E "\"sh\",|\"bash\",|\"perl\",|\"openssl\",|\"nc\",|\"netcat\",|\"ssh\",|\"socat\",|\"awk\",|\"ncat\",")
    TS=$(date '+%Y-%m-%d %H:%M:%S')
    if [ "${DATA}" != "" ];then
        echo  "${DATA}${TS}" >> network_monitor.log
        echo ${DATA}|awk -F= '{print $2}'|awk -F, '{print $1}'|xargs -I % sh -c 'kill -9 % 2>/dev/null'
        speaker-test -t sine -f 1000 -l 1 >/dev/null
    fi
}

dwm_weather()
{
      DATA=$(curl -s --max-time 3 -H "Accept-Language: zh" wttr.in/France?format=%C+%t|grep -v Unknown)
      DATA2=$(echo "${DATA}"|head -n 1)
      if [ ${?} == 1 ];then
          export __DWM_BAR_WEATHER__=""
      fi

      if [ ${?} == 0 ];then
          if [ "${DATA2}" == "" ];then
              export __DWM_BAR_WEATHER__=""
          else
              export __DWM_BAR_WEATHER__="[ ${DATA} ]" 
          fi
      elif [ "${__DWM_BAR_WEATHER__}" == "" ];then
          export __DWM_BAR_WEATHER__=""
      fi
}


cputemp(){
  t=$(cat /sys/class/thermal/thermal_zone0/temp|cut -b -2)°C
  printf "[ CPU:%s ]" ${t}
}
dwm_weather
while true; do
    if [[ $COUNT < 500 ]]
    then
        COUNT=$((${COUNT}+1))
    else
        dwm_weather
        COUNT=0
    fi 
    dwm_monitor
    lowerbar=""
    lowerbar="$lowerbar$(cputemp)"
    lowerbar="$lowerbar$(dwm_storage)"
    lowerbar="$lowerbar$(dwm_upspeed)"
    lowerbar="$lowerbar$(dwm_downspeed)"
    lowerbar="$lowerbar${__DWM_BAR_WEATHER__}"
    lowerbar="$lowerbar$(dwm_alsa)"
    lowerbar="$lowerbar$(dwm_tor)"
    lowerbar="$lowerbar$(dwm_vpn)"
    lowerbar="$lowerbar$(dwm_dns)"
    xsetroot -name "$lowerbar"
    sleep 1
done