#!/bin/sh

# for Linux:
# libdir=/opt/drweb/lib
# pkgdatadir=/opt/drweb/doc/samba

libdir=/opt/drweb/lib
scriptdir=/opt/drweb/doc/samba
datarootdir=${prefix}/share
me=$0

find_smbd()
{
    echo /usr/*bin/smbd /usr/local/*bin/smbd /usr/local/samba/*bin/smbd /opt/samba/smbd
}

get_smbd_version()
{
    $1 -V | sed 's/Version //'
}

get_smbd_libdir()
{
    # at first use MODULESDIR (for samba >= 3.3) else (if not found) use LIBDIR
    ($1 -b | grep MODULESDIR || $1 -b | grep LIBDIR) | sed 's/.*DIR: //'
}

get_smbd_config()
{
    $1 -b | grep CONFIGFILE | sed 's/.*CONFIGFILE: //'
}

# update links for one Samba
update_for()
{
    echo
    echo "--------------------------------------------------------"
    echo "Update links for" $1;
    smbd_version=`get_smbd_version $1`;
    smbd_libdir=`get_smbd_libdir $1`/vfs;
    smbd_config=`get_smbd_config $1`;

    num=`sh $scriptdir/vfs-versions.sh $smbd_version`;
    if [ -n "$num" ] ; then
	if [ -h $smbd_libdir/smb_spider.so ] ; then
	    echo "Found old symlink in $smbd_libdir/smb_spider.so. Removing..."
	    rm -f $smbd_libdir/smb_spider.so
	fi
	if [ -f $smbd_libdir/smb_spider.so ] ; then
	    echo "$smbd_libdir/smb_spider.so is a regular file. Cannot create symlink."
	    return 1
	fi
	echo "create symlink" $libdir/libsmb_spider.so.$num "-->" $smbd_libdir/smb_spider.so;
	if (ln -s $libdir/libsmb_spider.so.$num $smbd_libdir/smb_spider.so);then
	    echo "Please, update your config" $smbd_config;
	    echo "--------------------------------------------------------"
	else
	    echo $me: "Failed to create link!" 1>&2;
	    return 1
	fi
    else
	echo $me: "Could not determine SAMBA version" 1>&2
	exit 1;
    fi
}

remove_for()
{
    so_link=`get_smbd_libdir $1`/vfs/smb_spider.so

    if ([ -f $so_link ] || [ -h $so_link ]);then
	echo "Remove link" $so_link;
	(rm $so_link) && echo "Please, update your config" `get_smbd_config $1`;
    fi
}

print_help()
{
    echo "Create links for smb_spider.so"
    echo "Usage:" $0 "[options]" "[smbd pathes]";
    printf "%-20s%s\n" "-h, --help" "show help";
    printf "%-20s%s\n" "-r, --remove" "remove links";
    printf "%-20s%s\n" "-s, --smbd-path" "for the chosen smbd only";
}

smbd_list="";
action="";
installed_any="";
found_any="";

# get options
while [ x"$1" != x"" ] 
do 
    case $1 in
	--help | -h) print_help; exit 1;;
	--remove | -r) action="remove";;
	--smbd-path | -s) shift; smbd_list=`echo $smbd_list $1`;;
	-*) echo "unknown option:" $1 1>&2; exit 1;;
	*) smbd_list=`echo $smbd_list $1`;;
    esac
    shift;
done

# get smbd list
if [ x"$smbd_list" = x"" ];then
    smbd_list=`find_smbd`
fi

# choose and do action
if [ x"$action" = x"remove" ];then
    for i in $smbd_list;do
	[ -f $i ] && remove_for $i;
    done
    exit 0;
else
    for i in $smbd_list;do
	[ -f $i ] && { found_any="yes"; } && (update_for $i) && { installed_any="yes"; };
    done

    if [ x"$found_any" != x"yes" ];then
	echo $me: "cannot find any smbd, use parameter --smbd-path" 1>&2
    fi

    if [ x"$installed_any" = x"yes" ];then
	echo
	echo "Example:"
	echo "[drweb_audit]"
	echo "   comment = Directory protected by Dr.WEB (R) Samba SpIDer"
	echo "   path = /mnt/shares/protected_stuff"
	echo "   vfs objects = smb_spider"
    fi
fi
