#!/bin/sh -e
# Pre-install script for the Debian wu-ftpd package.
# Original (in Perl) from Peter Tobias, later changed by Heiko
# Schlittermann, and rewritten in sh by Josip Rodin.

if [ "$1" = "install" -o "$1" = "upgrade" ]; then

if [ -L "/etc/ftpusers" -a ! -f "`readlink /etc/ftpusers`" ]; then
	echo "Your /etc/ftpusers file is currently a broken symbolic link, fixing.."
	rm /etc/ftpusers
	cat >/etc/ftpusers <<EOF
# /etc/ftpusers: list of users disallowed FTP access. See ftpusers(5).

root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
nobody
EOF
fi

# Move the config directory from /etc/wu-ftpd-academ to /etc/wu-ftpd, and
# substitute old directory names with wu-ftpd in all of them
if [ -f "/etc/wu-ftpd-academ/ftpaccess" ]; then
  if [ ! -f "/etc/wu-ftpd/ftpaccess" ]; then
    cat <<END
There are FTPd configuration files in /etc/wu-ftpd-academ . These files
may be left from a previous installation of this (old) version of wu-ftpd.

END
    echo -n "Do you want to copy these files to the /etc/wu-ftpd directory? [Y/n] "
    read an
    case "$an" in 
      [nN]*)
	echo "+ You still have some old config files in /etc/wu-ftpd-academ ."
	;;
      *)
	if [ ! -d /etc/wu-ftpd ]; then
	   mkdir /etc/wu-ftpd; chmod 0755 /etc/wu-ftpd
	fi
	echo "Copying the configuration files from /etc/wu-ftpd-academ and converting"
	echo "them to the new file locations: "
	cd /etc/wu-ftpd-academ
	for i in `find . -type f -maxdepth 1 -print`; do
	  echo -n "$i "
	  sed -e "s%/ftpd/%/wu-ftpd/%" -e "s%/wu-ftpd-academ/%/wu-ftpd/%" < $i > /etc/wu-ftpd/$i
	done
	cd /

	echo -e "\nAll of those files are now in /etc/wu-ftpd.";
	echo -n "Do you want to remove the old ones now? [Y/n] ";
	read an
	case "$an" in 
	  [nN]*) ;;
	  *) 	rm -f `find /etc/wu-ftpd-academ -type f -maxdepth 1 -print` 
	  	rmdir /etc/wu-ftpd-academ 2>/dev/null
	  ;;
	esac
	;;
    esac
  fi
fi

# Move the config directory from /etc/ftpd to /etc/wu-ftpd, and substitute
# old directory names with wu-ftpd in all of them
if [ -f "/etc/ftpd/ftpaccess" ]; then
  if [ ! -f "/etc/wu-ftpd/ftpaccess" ]; then
    cat <<END
There are FTPd configuration files in /etc/ftpd . These files may
be left from a previous installation or from another FTP daemon.

END
    echo -n "Do you want to copy these files to the /etc/wu-ftpd directory? [Y/n] "
    read an
    case "$an" in 
      [nN]*)
	echo "You still have some old config files in /etc/ftpd ."
	;;
      *)
	if [ ! -d /etc/wu-ftpd ]; then
	   mkdir /etc/wu-ftpd; chmod 0755 /etc/wu-ftpd
	fi
	echo "Copying the configuration files from /etc/ftpd and converting them"
	echo "to the new file locations: "
	cd /etc/ftpd
	for i in `find . -type f -maxdepth 1 -print`; do
	  echo -n "$i "
	  sed -e "s%/ftpd/%/wu-ftpd/%" -e "s%/wu-ftpd-academ/%/wu-ftpd/%" < $i > /etc/wu-ftpd/$i
	done
	cd /

	echo -e "\nAll of those files are now in /etc/wu-ftpd."
	echo -n "Do you want to remove the old ones now? [Y/n] "
	read an
	case "$an" in 
	  [nN]*) ;;
	  *) 	rm -f `find /etc/ftpd -type f -maxdepth 1 -print`
	  	rmdir /etc/ftpd 2>/dev/null;;
	esac
	;;
    esac
  fi
fi

# Move the transfer statistics log file to /var/log/wu-ftpd
if [ -f "/var/log/xferstats" -a ! -e "/var/log/wu-ftpd/xferstats" ]; then
    echo "+ Moving /var/log/xferstats to /var/log/wu-ftpd .";
    if [ ! -d /var/log/wu-ftpd ]; then
      mkdir /var/log/wu-ftpd
    fi
    chgrp adm /var/log/wu-ftpd
    chmod 0755 /var/log/wu-ftpd
    mv /var/log/xferstats /var/log/wu-ftpd
    chgrp adm /var/log/wu-ftpd/xferstats
fi

fi


