#!/bin/sh
#------------------------------------------------------------------------------
# /usr/local/htdocs/spaceinfo.cgi - space information
#
# Copyright (c) 2003-2010  Marcus Herleb, info(at)herleb(dot).de
# Copyright (c) 2011-2024  The Eisfair Team, team(at)eisfair(dot)org
#
# Creation:     2003-10-11 mgh
# Last Update:  $Id$
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version..
#------------------------------------------------------------------------------

# get kernel version
KV=${KV="`cat /proc/sys/kernel/osrelease`"}

echo "Content-Type: text/html; charset=iso-8859-1"
echo "Cache-control: no-store" # HTTP/1.1 (or no-cache?)
echo "Pragma: no-cache"        # HTTP/1.0
echo "Expires: `date -Ru`"     # Expires now!
echo "Refresh: 30"
echo

echo "<html><head>"
echo "<LINK REL=STYLESHEET TYPE=\"text/css\" HREF=\"../main.css\">"
echo "<title>Space Info</title></head><body>"

echo "<h2>Speicherplatz</h2>"
KV="`echo $KV|cut -b-3`"
echo "<table border='0' width='300' cellpadding='2' cellspacing='1'  bgcolor='#000000'>"

cat /proc/meminfo > meminfo.txt

if [ -f meminfo.txt ]
then
    swp="used"
    tdbg="red"

    if [ $KV = "2.4" ]    # Kernel 2.4.x Routine
    then
        while read a b c d e f g
        do
            if [ $a = "Swap:" -a $b = "0" ]
            then
                swp="not installed"
                tdbg="yellow"
            else
                if [ $a = "Swap:" -a $a = "0" ]
                then
                    swp="not&nbsp;used"
                    tdbg="#e9e9e9"
                fi
            fi
            if [ $a = "MemFree:" ]
            then
                echo "<tr bgcolor='#efefef'><td colspan=2>$b&nbsp;KB&nbsp;RAM&nbsp;free</td><td bgcolor=$tdbg>Swap&nbsp;$swp</td></tr>"
              # echo $b > /../../tmp/mem.tmp
            fi
        done < meminfo.txt
    else   # Kernel != 2.4
        while read a b c
        do
            if [ $a = "SwapTotal:" -a $b = "0" ]
            then
                swp="not installed"
                tdbg="yellow"
                echo "<td colspan=4 bgcolor=$tdbg>Swap&nbsp;$swp</td>"
            else
                if [ $a = "MemFree:" ]
                then
                    echo "<td  bgcolor=#e9e9e9 colspan=3>$b&nbsp;KB&nbsp;RAM&nbsp;free</td>"
                  # echo $b > /../../tmp/mem.tmp
                fi
                if [ $a = "SwapTotal:" -a $a = "0" ]
                then
                    swp="not&nbsp;used"
                    tdbg="#e9e9e9"
                fi
            fi
        done < meminfo.txt
    fi
fi

echo "<table border='0' width='300' cellpadding='2' cellspacing='1'  bgcolor='#000000'>"
echo "<tr>"
echo "<td align='center' bgcolor='#cfcfcf' width="100"><b>MOUNTPOINT</b></td>"
echo "<td align='center' bgcolor='#cfcfcf' width="100"><b>BELEGT</b></td>"
echo "<td align='center' bgcolor='#cfcfcf' width="100"><b>FREI</b></td>"
echo "</tr>"
echo "</table>"


dd=`df -h`

i=7

mdevice=true
while [ -n "$mdevice" ]
do
    bga=#efefef
    bgb=cyan
    bgc=#00FF00

    i=`expr $i + 1`

    mdevice=$(echo `df` | cut -d" " -f$i)

    if [ -n "$mdevice" ] # Abbruchbedingung
    then
        echo "<table border='0' width='300' cellpadding='2' cellspacing='1'  bgcolor='#000000'>"
        echo "<tr>"

        a=5
        a=`expr $a + $i`

        echo "<td bgcolor='$bga' width='40'><a title='Drive: $mdevice'>$(echo $dd | cut -d" " -f$a)</a></td>" # Mountpoint

        b=4
        b=`expr $b + $i`
        belegt=$(echo $dd | cut -d" " -f$b | cut -d "%" -f1)

        c=1
        c=`expr $c + $i`
        d=3
        d=`expr $d + $i`

        if [ "$belegt" -le "19" ]
        then
            echo "<td bgcolor='$bgb' width='$belegt%'>$belegt&nbsp;%</td><td bgcolor='$bgc' width='33'>&nbsp;of&nbsp;&nbsp;$(echo $dd | cut -d" " -f$c)&nbsp;</td><td bgcolor='$bgc' align='right'>$(echo $dd | cut -d" " -f$d)</td>"
        fi
        if [ "$belegt" -le "50" -a "$belegt" -ge "20" ]
        then
            echo "<td bgcolor='$bgb' width='$belegt%'>$belegt&nbsp;%&nbsp;of&nbsp;</td><td bgcolor='$bgc' width='33'>&nbsp;$(echo $dd | cut -d" " -f$c)&nbsp;</td><td bgcolor='$bgc' align='right'>$(echo $dd | cut -d" " -f$d)</td>"
        fi
        if [ "$belegt" -le "69" -a "$belegt" -ge "51" ]
        then
            echo "<td bgcolor='$bgb' width='$belegt%'>$belegt&nbsp;%&nbsp;of&nbsp;&nbsp;$(echo $dd | cut -d" " -f$c)&nbsp;</td><td bgcolor='$bgc' align='right'>$(echo $dd | cut -d" " -f$d)</td>"
        fi
        if [ "$belegt" -ge "70" -a "$belegt" -le "84" ]
        then
            bgb=#ccccff
            bgc=yellow
            echo "<td bgcolor='$bgb' width='$belegt%'>$belegt&nbsp;%&nbsp;of&nbsp;&nbsp;$(echo $dd | cut -d" " -f$c)&nbsp;&nbsp;&nbsp;</td></td><td bgcolor='$bgc' align='right'>$(echo $dd | cut -d" " -f$d)</td>"
        fi

        if [ "$belegt" -ge "85" -a "$belegt" -le "100" ]
        then
            bgb=#ff6699
            bgc=red
            echo "<td bgcolor='$bgb' width='$belegt%'>$belegt&nbsp;%&nbsp;of&nbsp;&nbsp;$(echo $dd | cut -d" " -f$c)&nbsp;&nbsp;</td><td bgcolor='$bgc' align='right'>$(echo $dd | cut -d" " -f$d)</td>"
        fi

        i=`expr $i + 5`

        echo "</tr></table>"
    else
        echo "</body></html>"
    fi
done