#!/bin/sh

export DATE="3.10.2006";SCRIPT=${0#/rom}
export TITLE="Verwaltung:
Pakete"
. ${SCRIPT%/*}/cgi-bin-pre.sh

cat<<EOF
<H1>Verwaltung:
Pakete</H1>

EOF
echo "<!--"
IFS=\&
set $QUERY_STRING
while [ $1 ]; do eval $1; shift; done
unset IFS
echo "-->"
if [ "$action" = "update" ]; then   
        echo "<pre>"
        ipkg update
        echo "update erfolgreich ausgeführt"
        echo "</pre>"
elif [ "$action" = "install" ]; then        
        echo "<pre>"
        ipkg update
        yes n | ipkg install `echo "$pkg" | sed -e 's, ,+,g'` 
        echo "$pkg installiert"
        echo "</pre>"
elif [ "$action" = "remove" ]; then 
        echo "<pre>"
        ipkg remove $pkg
        echo "$pkg gelöscht"
        echo "</pre>"
fi
cat<<EOF
<table border=0>
<tr>
<td valign="top">
<table><tr><td><b>Installiert</b></td><td><b></b></td></tr>
EOF
cat /usr/lib/ipkg/status|sed -ne'
/^Package:/{
s/.* \([A-Za-z -~]\+\).*/\1 - /
h
g
}
/^Version:/{
s/.* \([0-9a-zA-Z -~]\+\).*/\1 - ka/
H
g
s/\n/ /g
p
}
'| egrep -v "(base-files|bridge|busybox|uclibc|kernel|Done\.)" | awk -F ' ' '
$2 !~ /terminated/ {       
        link=$1
       gsub(/\+/,"%2B",link)
       desc=$5 " " $6 " " $7 " " $8 " " $9 " " $10 " " $11 " " $12 " " $13 " " $14 " " $15 " " $16 " " $17 " " $18 " " $19 " " $20 " " $21 " " $22 " " $23 " " $24 " " $25 " " $26 " " $27
       print "<tr><td><a href=\"pakete.html?action=remove&pkg=" link "\">Deinstallieren</td><td>" $1 "</td></tr>"       
}
'
cat<<EOF
  </table>
</td><td valign="top">
<table><tr><td><b>Verfügbar</b></td><td><b>(non-free)</b></td></tr>
EOF
egrep 'Package:|Description:' /usr/lib/ipkg/status /usr/lib/ipkg/lists/non-free 2>&- | sed -e 's, ,,' -e 's,/usr/lib/ipkg/lists/non-free,,' | awk -F: '
$1 ~ /status/ {
        installed[$3]++;
}
($1 !~ /terminated/) && ($1 !~ /\/status/) && (!installed[$3]) && ($2 !~ /Description/) {
        if (current != $1) print "<tr><th>" $1 "</th></tr>"
        link=$3
        gsub(/\+/,"%2B",link)         
        getline descline
        split(descline,desc,":")
        print "<tr><td><a href=\"pakete.html?action=install&pkg=" link "\">Installieren</td><td>" $3 "</td></tr>"
        current=$1
}
'
cat<<EOF
</table>
<table><tr><td><b>Verfügbar</b></td><td><b>(Freifunk)</b></td></tr>
EOF
egrep 'Package:|Description:' /usr/lib/ipkg/status /usr/lib/ipkg/lists/freifunk 2>&- | sed -e 's, ,,' -e 's,/usr/lib/ipkg/lists/freifunk,,' | awk -F: '
$1 ~ /status/ {
        installed[$3]++;
}
($1 !~ /terminated/) && ($1 !~ /\/status/) && (!installed[$3]) && ($2 !~ /Description/) {
        if (current != $1) print "<tr><th>" $1 "</th></tr>"
        link=$3
        gsub(/\+/,"%2B",link)         
        getline descline
        split(descline,desc,":")
        print "<tr><td><a href=\"pakete.html?action=install&pkg=" link "\">Installieren</td><td>" $3 "</td></tr>"
        current=$1
}
'
cat<<EOF
</table>
</td><td valign="top">
<table><tr><td><b>Verfügbar</b></td><td><b>(Whiterussian)</b></td></tr>
EOF
egrep 'Package:|Description:' /usr/lib/ipkg/status /usr/lib/ipkg/lists/whiterussian 2>&- | sed -e 's, ,,' -e 's,/usr/lib/ipkg/lists/whiterussian,,' | awk -F: '
$1 ~ /status/ {
        installed[$3]++;
}
($1 !~ /terminated/) && ($1 !~ /\/status/) && (!installed[$3]) && ($2 !~ /Description/) {
        if (current != $1) print "<tr><th>" $1 "</th></tr>"
        link=$3
        gsub(/\+/,"%2B",link)         
        getline descline
        split(descline,desc,":")
        print "<tr><td><a href=\"pakete.html?action=install&pkg=" link "\">Installieren</td><td>" $3 "</td></tr>"
        current=$1
}
'
cat<<EOF
</table>
</td></tr></table>
EOF

. ${SCRIPT%/*}/cgi-bin-post.sh