#!/bin/sh

#  Copyright (c) 2006-2009 Vagrant Cascadian <vagrant@freegeek.org>

#  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.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, you can find it on the World Wide
#  Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
#  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#  MA 02110-1301, USA.

if [ -e /etc/sysconfig/ltspdist ]; then
    . /etc/sysconfig/ltspdist
    VENDOR=$VENDORDEF
else
    VENDOR=$(lsb_release --id --short | tr " " "_")
fi
export BASE=${BASE:-/opt/ltsp}                       # LTSP base directory

# generic functions
find_chroots() {
    find $BASE -mindepth 1 -maxdepth 1 -type d
}

find_lts_conf() {
    chroot=$1
    chroot_name=$(basename $chroot)
    lts_conf_dirs="$chroot/etc /var/lib/tftpboot/ltsp/$chroot_name /srv/tftp/ltsp/$chroot_name /tftpboot/ltsp/$chroot_name"
    for lts_conf_dir in $lts_conf_dirs ; do
        lts_conf=$lts_conf_dir/lts.conf
        if [ -f "$lts_conf" ]; then
            echo found: "$lts_conf"
            if [ "$verbose" = "true" ]; then
                cat "$lts_conf"
            fi
            echo
        fi
    done
}

find_images() {
    if [ -d "$BASE/images" ]; then
        for image in $(find $BASE/images/ -type f -name '*.img' ); do
            echo found image: $image
            if [ "$verbose" = "true" ] && [ -x /usr/bin/file ]; then
                file $image 
            fi
            echo
        done
    fi     
}

lsb_server_info() {
    echo server information:
    lsb_release --all
    echo
}


# distro specific functions
Debian_server_packages() {
    echo server packages:
    COLUMNS=200 dpkg -l 'ltsp*' 'ldm*' | awk '/ltsp|ldm/{print $1,$2,$3}'
    echo
}

Debian_chroot_packages() {
    chroot=$1
    if [ -f $chroot/var/lib/dpkg/status ]; then
        echo "packages in chroot: $chroot"
        COLUMNS=200 dpkg --root=$chroot -l 'ltsp*' 'ldm*' | awk '/ltsp-client|ldm|ltspfs/{print $1,$2,$3}'
        echo
    fi
}

Debian_chroot_release() {
    if [ -x $chroot/usr/bin/lsb_release ]; then
        echo "chroot information: $chroot"
        ROOT=$chroot ltsp-chroot lsb_release --all
        echo
    fi
}

Gentoo_server_packages() {
    echo server packages:
    Gentoo_packages
    echo
}

Gentoo_packages() {
    chroot=$1
    cat $chroot/var/db/pkg/net-misc/ltsp*/PF
    cat $chroot/var/db/pkg/sys-fs/ltspfs-*/PF
    cat $chroot/var/db/pkg/*/ldm*/PF
}

Gentoo_chroot_packages() {
    chroot=$1
    if [ -d $chroot/var/db/pkg ]; then
        echo "packages in chroot: $chroot"
        Gentoo_packages $chroot
        echo
    fi
}

for opt in $@ ; do
    case $opt in
        --no-server-info) server_info="false" ;;
        --verbose|-v) verbose="true" ;;
    esac
done

case $VENDOR in
    Debian|Ubuntu)
        if [ "$server_info" != "false" ]; then
            lsb_server_info
            Debian_server_packages
        fi
        for chroot in $(find_chroots) ; do
            chroot_name=$(basename $chroot)
            if [ "$verbose" = "true" ]; then
                Debian_chroot_release
            fi
            Debian_chroot_packages $chroot
            find_lts_conf $chroot
        done
        find_images
        ;;
    Gentoo)
        if [ "$server_info" != "false" ]; then
            lsb_server_info
            Gentoo_server_packages
        fi
        for chroot in $(find_chroots) ; do
            chroot_name=$(basename $chroot)
            Gentoo_chroot_packages $chroot
            find_lts_conf $chroot
        done
        find_images
        ;;
    SUSE_LINUX)
       if [ x"$USER" != x"root" ]; then
            echo "You have to be root to run this script"
            exit 1
       fi
       echo "-------------------------------------------------------------"
       echo "Please paste the following information on http://pastebin.com"
       echo "post the resulting link to the mailing list or IRC channel"
       echo "-------------------------------------------------------------"
       echo "LSB information:"
       lsb_release --all
       echo "Package information:"
       rpm -qa "*kiwi*"
       rpm -qa "*ltsp*"
       rpm -qa "*ldm*"
       echo "Network information:"
       ip addr
       echo "kiwi-ltsp configuration:"
       cat /etc/sysconfig/kiwi-ltsp | grep -v \#
       kiwi-ltsp-setup -c || true
       echo "DHCPD configuration:"
       cat /etc/dhcpd.conf | grep -v \#
       echo "contents of lts.conf:"
       cat /srv/tftpboot/KIWI/lts.conf | grep -v \#
       echo "pxelinux.cfg/default contents:"
       cat /srv/tftpboot/pxelinux.cfg/default
       echo "kiwi's config.default:"
       cat /srv/tftpboot/KIWI/config.default
       ;;
esac
