#!/bin/sh
###############################################################################
## Copyright (c) International Business Machines  Corp., 2003                ##
##                                                                           ##
## 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, write to the Free Software Foundation,  ##
## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA          ##
##                                                                           ##
###############################################################################
. daemonlib.sh

do_setup()
{
	export RHOST="localhost"
	export TEST_USER_PASSWD="eal"
	export TEST_USER_ENCRYPTED_PASSWD="42VmxaOByKwlA"
	export TEST_USER_HOMEDIR="/home/$TEST_USER"

	tst_test_cmds awk expect ftp useradd userdel vsftpd

	for vsftp_confdir in /etc/vsftpd /etc; do
		if [ -r "$vsftp_confdir/vsftpd.conf" ]; then
			VSFTP_CONF="$vsftp_confdir/vsftpd.conf"
			break
		fi
	done
	if [ ! -r "$VSFTP_CONF" ] ; then
		tst_brkm TBROK "vsftpd.conf not found."
	fi

	status_daemon vsftpd
	if [ $? -ne 0 ]; then
		start_daemon vsftpd
		if [ $? -ne 0 ]; then
			tst_brkm TBROK "Can't start vsftp"
		fi
	fi

	LOCAL_ENABLE=$(awk -F= '/^local_enable=/ {print $2}' "$VSFTP_CONF")
	if [ "$LOCAL_ENABLE" != "YES" ]; then
		LOCAL_ENABLE="NO"
	fi

	ANONYMOUS_ENABLE=$(awk -F= '/^anonymous_enable=/ {print $2}' "$VSFTP_CONF")
	if [ "$ANONYMOUS_ENABLE" != "NO" ]; then
		ANONYMOUS_ENABLE="YES"
	fi

	if [ $TEST_USER = "root" -o $TEST_USER = "anonymous" ]; then
		return
	fi

	userdel -r $TEST_USER
	sleep 1

	if ! useradd -m -p $TEST_USER_ENCRYPTED_PASSWD $TEST_USER; then
		tst_brkm TBROK "Could not add test user $TEST_USER to system $RHOST."
	fi

	# create users home diretory (SLES 8 does not do this, even when specified
	# in adduser)
	USER_UID=$(id -u $TEST_USER)
	USER_GID=$(id -g $TEST_USER)
	mkdir -p "$TEST_USER_HOMEDIR"
	chown -R $USER_UID:$USER_GID $TEST_USER_HOMEDIR
}

do_cleanup()
{
	if [ $TEST_USER != "root" -a $TEST_USER != "anonymous" ]; then
		userdel -r $TEST_USER
	fi
}