#!/bin/sh
##
## Show the Samba information
## Copyright (c) 2008 SATOH Fumiyasu @ OSS Technology Inc.
##               <http://www.osstech.co.jp/>
##
## License: GNU General Public License version 2 or later
## Date: 2008-11-10, since 2008-11-10
##

PATH="/opt/osstech/sbin:/opt/osstech/bin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH"
LC_ALL=C
export PATH LC_ALL

renice 1 "$$" >/dev/null 2>&1 || :
ionice -c 3 -p "$$" >/dev/null 2>&1 || :

sysconfdir="/opt/osstech/etc"

flag_progress=""
cmd_usage="Usage: $0 [OPTIONS]

Options:
 -p
    Print the running command to standard error
    e.g. \`$0 -p >sambainfo.txt\`
 -h
    Print this help message
"

while getopts ph opt; do
    case "$opt" in
    p)
	flag_progress="yes"
	;;
    h)
	echo "$cmd_usage"
	exit 0
	;;
    *)
	exit 1
	;;
    esac
done

echo_title()
{
    echo "==== SAMBA INFO: $(date '+%Y-%m-%d %H:%M:%S'): $*"
    if [ -n "$flag_progress" ]; then
      echo "SAMBA INFO: $* ..." 1>&2
    fi
}

info_run()
{
    echo_title "$@"
    echo "---- command type: $(type "$1")"
    "$@" 2>&1
    echo
}

info_eval()
{
    echo_title "$1"
    sh -c "$1" 2>&1
    echo
}

info_cat()
{
    for _info_cat in "$@"; do
	echo_title "$_info_cat"
	cat  "$_info_cat" 2>&1
	echo
    done
}

## ======================================================================

echo_title version
sed -n '/^#.*Date: /{s/,.*$//;s/^.* //;p;q;}' "$0"

info_run date '+%Y-%m-%d %a %H:%M:%S %z'
info_run uname -a

info_cat "$sysconfdir"/samba/*.conf
info_run testparm -s
info_run testparm -sv
info_run net getlocalsid
info_run net getdomainsid
info_run smbstatus --processes
info_run smbstatus --byterange

info_cat "$sysconfdir"/openldap/*.conf \
|sed -E -e 's/^(rootpw\s+|credentials=).*/\1 *** HIDDEN by SAMBA INFO/' \
;
info_cat "$sysconfdir"/smbldap-tools/smbldap.conf
info_cat "$sysconfdir"/smbldap-tools/smbldap_bind.conf \
|sed -E -e 's/^([a-z]+Pw=).*/\1 *** HIDDEN by SAMBA INFO/' \
;

echo_title Done.
