if [ ${TERM} == "dumb" ]; then
    return
fi

export ARCH=arm
export VDPAU_DRIVER=sunxi

OUT=""
LOAD=$(w | sed -n 1p  | awk '{print $(NF-2), $(NF-1),$(NF-0)}')
OUT="${OUT}Load: ${LOAD} "

## getting temperature from USB termometer 
## http://www.dx.com/p/81105
##
if which temper >/dev/null; then
   TEMPER=$(temper -c)
   if echo $TEMPER | egrep -qv "Couldn't find the USB device"; then
      TEMPER=$(echo "scale=1;${TEMPER}/1" | bc)
      TEMPER="- Ambient: ${TEMPER}"
   else
      TEMPER=""
   fi
fi

CEL=$(awk 'BEGIN { print "\302\260C"; }')

if [ -d "/sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/" ]; then
TEMP=$(cat /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input)
TEMP=$(echo "scale=1;${TEMP}/1000" | bc)  
fi

# if we are reading A20 temp with daemon
if [ -f "/run/soc-temp" ]; then 
TEMP=$(cat /run/soc-temp)
fi

# if we are reading from A20
if [ -d "/sys/devices/platform/a20-tp-hwmon/" ]; then
TEMP=$(cat /sys/devices/platform/a20-tp-hwmon/temp1_input)
TEMP=$(echo "scale=1;${TEMP}/1000" | bc)
fi

# if we are reading from A20 on newer kernel
if [ -d "/sys/devices/virtual/thermal/thermal_zone0/" ]; then
TEMP=$(cat /sys/devices/virtual/thermal/thermal_zone0/temp)
TEMP=$(echo "scale=1;${TEMP}/1000" | bc)
fi

# thermal zone on lemaker guitar
if [[ -d "/sys/devices/virtual/thermal/thermal_zone1/" && $TEMP == "0" ]]; then
TEMP=$(cat /sys/devices/virtual/thermal/thermal_zone1/temp)
TEMP=$(echo "scale=1;${TEMP}/1000" | bc)
fi

if [[ -n $TEMP ]]; then
OUT="${OUT}- Board: ${TEMP}${CEL} "
fi

if [[ -n $TEMPER ]]; then
OUT=$OUT"${TEMPER}${CEL} "
fi

if [ "$(cat /proc/partitions |grep sd)" != "" ];then
SDA_TYPE=$(/sbin/udevadm info --query=all --name=sda | grep ID_BUS=)
SDA_TYPE=${SDA_TYPE#*=}
fi

# if we have a hard drive
if [[ -e '/dev/sda' && $SDA_TYPE == "ata" ]] ; then
   HDDTEMP=$(/usr/sbin/hddtemp /dev/sda 2>&1)
   if [ $? -eq 0 ]; then
       HDDTEMP=$(echo ${HDDTEMP} | awk '{print $NF}')
       HDDFREE=$(df -h /dev/sda1 | grep sda | awk '{ print " / " $(NF-2)}')
       if [ "${HDDFREE}" != "" ]; then
          HDDFREE="${HDDFREE}"b
       fi
       OUT="${OUT}- Drive: ${HDDTEMP}${HDDFREE} "
   fi
fi


MEMFREE=$(free | sed -n 2p | awk '{print $(NF-3)}')
MEMBUFFERS=$(free | sed -n 2p | awk '{print $(NF-1)}')
MEMCACHED=$(free | sed -n 2p | awk '{print $(NF)}')
MEM=$(echo "(${MEMFREE}+${MEMBUFFERS}+${MEMCACHED})/1024" | bc)
OUT="${OUT}- Memory: ${MEM}Mb"

# Battery info for Allwinner
HARDWARE=$(cat /proc/cpuinfo | grep Hardware | awk '{print $3}')

# for root users which have acces to hw and allwinner and old kernel
if [[ "$UID" == 0 && "$(uname -r | cut -c 1-3)" == "3.4" && ($HARDWARE = "sun7i" || $HARDWARE = "Allwinner") ]]; then

if [ "$(printf '0x%X' $(($(i2cget -y -f 0 0x34 0x82) & 0xC3)))" != "0xC3" ]; then
	i2cset -y -f 0 0x34 0x82 0xC3
fi
# read power OPERATING MODE register @01h
POWER_OP_MODE=$(i2cget -y -f 0 0x34 0x01)
BAT_EXIST=$(($(($POWER_OP_MODE&0x20))/32))  # divide by 32 is like shifting rigth 5 times
CHARG_IND=$(($(($POWER_OP_MODE&0x40))/64))  # divide by 64 is like shifting rigth 6 times

if [ "$BAT_EXIST" == "1" ]; then
	#read battery voltage   79h, 78h        0 mV -> 000h,   1.1 mV/bit      FFFh -> 4.5045 V
	BAT_VOLT_LSB=$(i2cget -y -f 0 0x34 0x79)
	BAT_VOLT_MSB=$(i2cget -y -f 0 0x34 0x78)

	BAT_BIN=$(( $(($BAT_VOLT_MSB << 4)) | $(($(($BAT_VOLT_LSB & 0xF0)) >> 4)) ))
	BAT_VOLT=$(echo "($BAT_BIN*1.1)"|bc)

	# store maximum battery voltage to compare to
	if [ -f "/etc/default/battery" ]; then
        source "/etc/default/battery"
	else
        echo "MAX=$BAT_VOLT" > /etc/default/battery
		echo "MIN=3484" >> /etc/default/battery
		source "/etc/default/battery"
	fi

	# integer is enough, cut down the decimals
	MAX=${MAX%.*}
	BAT_VOLT=${BAT_VOLT%.*}

	# if we have new max value, alter defaults
	if [ "$BAT_VOLT" -gt "$MAX" ]; then 
		MAX=$BAT_VOLT
		sed -e 's/MAX=.*/MAX='$BAT_VOLT'/g' -i /etc/default/battery
	fi

	# if we have new min value, alter defaults
	if [ "$BAT_VOLT" -lt "$MIN" ]; then
		MIN=$BAT_VOLT
		#sed -e 's/MIN=.*/MIN='$BAT_VOLT'/g' -i /etc/default/battery
	fi

	# calculate percentage
	percent=$(echo "($BAT_VOLT-$MIN)*100/($MAX-$MIN)"|bc)

	# colorize output under certain percentage
	if [ $percent -le 15 ]; then
		color="31"
	fi

	BATT=" - Batt: "
	# dispay charging / percentage
	if [ "$CHARG_IND" == "1" ]; then
		BATT=$BATT"charging $percent%"
	else
		BATT=$BATT"\e["$color"m$percent%\x1B[0m"
	fi
fi
OUT="${OUT}${BATT}"

fi

echo ""
echo -e ${OUT}
echo ""