if [ "$OPT_PFTPD" = yes ]
then
  begin_script PFTPD "starting pure-ftpd"
  
  # Create FTP-System User
  if [ `grep ftpgroup /etc/group`x != "ftpgroup:x:200:"x ]
  then
    echo "ftpgroup:x:200:" >>/etc/group
    echo "ftpuser:x:200:200:ftpuser for pftpd::/bin/true" >>/etc/passwd
#    password=`/usr/local/bin/crypt ftppasswd`
#    echo "ftpuser:$password:9999:0:10000::::" >>/etc/shadow
    echo "ftpuser:*:0:0:10000::::" >>/etc/shadow
  fi

  eval pftpd_switches=''

  if [ "$PFTPD_EXPERT" = yes ]
  then
    /usr/local/bin/colecho "  expert mode" gn
    pftpd_switches=$PFTPD_EXPERT_SWITCHES
  else
    /usr/local/bin/colecho "  normal mode" gn

    # daemonize on
    pftpd_switches=$pftpd_switches' -B '

    # port
    if [ "$PFTPD_PORT" != "" -a "$PFTPD_PORT" -gt 0 ]
    then
            pftpd_switches=$pftpd_switches' -S '$PFTPD_PORT
    fi
    
    # number of clients
    if [ "$PFTPD_NUM_OF_CLIENTS" != "" -a "$PFTPD_NUM_OF_CLIENTS" -gt 0 ]
    then
            pftpd_switches=$pftpd_switches' -c '$PFTPD_NUM_OF_CLIENTS
    fi
    
    # number of connections per ip
    if [ "$PFTPD_MAX_CON_PER_IP" != "" -a "$PFTPD_MAX_CON_PER_IP" -gt 0 ]
    then
            pftpd_switches=$pftpd_switches' -C '$PFTPD_MAX_CON_PER_IP
    fi
    
    # timeout
    if [ "$PFTPD_TIMEOUT" != "" -a "$PFTPD_TIMEOUT" -gt 0 ]
    then
            pftpd_switches=$pftpd_switches' -I '$PFTPD_TIMEOUT
    fi
    
    # avoid dns
    if [ "$PFTPD_AVOID_DNS" = yes ]
    then
            pftpd_switches=$pftpd_switches' -H '
    fi

    # PFTPD_LIMIT
    if [ "$PFTPD_LIMIT" != "" -a "$PFTPD_LIMIT" -gt 0 ]
    then
            pftpd_switches=$pftpd_switches' -k '$PFTPD_LIMIT
    fi

    # PFTPD_BANDWIDTH
    if [ "$PFTPD_BANDWIDTH" != "" ]
    then
            pftpd_switches=$pftpd_switches' -T '$PFTPD_BANDWIDTH
    fi

# PFTPD_ANONYMOUS
    if [ "$PFTPD_ANONYMOUS" = "no" ]
    then
            pftpd_switches=$pftpd_switches' -E '
    else
       /usr/local/bin/colecho " setting up anonymous ftp " gn
	if [ "`grep anonymous /etc/passwd`"x != 'ftp:x:201:200:anonymous ftp:'$PFTPD_DEFAULT_FTP_PATH' :/bin/true'x ]
	then
         echo "ftp:x:201:200:anonymous ftp:$PFTPD_DEFAULT_FTP_PATH:/bin/true" >>/etc/passwd
	fi    
	    if [ ! -d "$PFTPD_DEFAULT_FTP_PATH" ]
	     then
	      $PFTPD_DEFAULT_FTP_PATH
	      $PFTPD_DEFAULT_FTP_PATH
	      $PFTPD_DEFAULT_FTP_PATH

	    fi

	    if [ "$PFTPD_ANONYMOUS_UPLOAD" = "yes" ]
	       then
	       if [ ! -d "$PFTPD_DEFAULT_FTP_PATH" ]
	        then
		 $PFTPD_DEFAULT_FTP_PATH
		 $PFTPD_DEFAULT_FTP_PATH
		 $PFTPD_DEFAULT_FTP_PATH
  	       fi
 	    fi

	    if [ "$PFTPD_ANONYMOUS_WAREZ_PROTECTION" = "yes" ]
	      then
	       pftpd_switches=$pftpd_switches' -s '
	    fi
    fi


    # PFTPD_LOG
    if [ "$PFTPD_LOG" = yes ]
    then
      pftpd_switches=$pftpd_switches' -O '$PFTPD_LOG_FORMAT':'$PFTPD_LOG_PATH
    fi

    # virtual users
    pftpd_switches=$pftpd_switches' -l puredb:/etc/pureftpd.pdb '

    /usr/local/bin/colecho "  setup from virtual users " gn
    idx=1
    while [ "$idx" -le "$PFTPD_VUSER_N" ]
    do
      eval vuser='$PFTPD_VUSER_'$idx
      eval vpass='$PFTPD_VPASS_'$idx
      eval vdir='$PFTPD_VDIR_'$idx
      eval vlimit_sessions='$PFTPD_VLIMIT_SESSIONS_'$idx
      eval vlimit_ip='$PFTPD_VLIMIT_IP_'$idx
      eval vlimit_time='$PFTPD_VLIMIT_TIME_'$idx
      
      # write entry into $PFTPD_PASSWD_FILE
      (echo "$vpass"; echo "$vpass") | pure-pw useradd "$vuser" -u ftpuser -d "$vdir" -f "$PFTPD_PASSWD_FILE" 2>/dev/null >/dev/null
      pure-pw usermod "$vuser" -y "$vlimit_sessions" -r "$vlimit_ip" -z "$vlimit_time" -f "$PFTPD_PASSWD_FILE" 2>/dev/null >/dev/null
      echo "  $vuser"
      idx=`/usr/bin/expr $idx + 1`
    done
    if [ -f "$PFTPD_PASSWD_FILE" ]
    then
      pure-pw mkdb /etc/pureftpd.pdb -f $PFTPD_PASSWD_FILE
    else
      > $PFTPD_PASSWD_FILE
      pure-pw mkdb /etc/pureftpd.pdb -f $PFTPD_PASSWD_FILE
    fi
          
    if [ "$OPT_PFTPD_CONTROL" = yes ]
    then
      if [ -f "/var/run/pftpd.conf" ]
      then
        /usr/local/bin/colecho "  configuration exists, skipping configuration file creation." gn
      else
        /usr/local/bin/httpd-menu.sh add pftpd.cgi PureFTP "" pftpd
        echo "#--------------------------------------" > /var/run/pftpd.conf
        echo "# Config File for PFTPD-User-Management" > /var/run/pftpd.conf
        echo "#--------------------------------------" > /var/run/pftpd.conf
        echo "PWF='"$PFTPD_PASSWD_FILE"'" >> /var/run/pftpd.conf
        echo "FTPDIR='"$PFTPD_DEFAULT_FTP_PATH"'" >> /var/run/pftpd.conf
        echo "PFTPD_SWITCHES='"$pftpd_switches"'" >> /var/run/pftpd.conf
        if [ "$PFTPD_LOG" = yes ]
        then
          echo "PFTPD_LOG_PATH='"$PFTPD_LOG_PATH"'" >> /var/run/pftpd.conf
          echo "PFTPD_LOG_FORMAT='"$PFTPD_LOG_FORMAT"'" >> /var/run/pftpd.conf
          echo "PFTPD_BANDWIDTH='"$PFTPD_BANDWIDTH"'" >> /var/run/pftpd.conf
        fi
      fi
    fi
  fi
    
  # allow system user?
  if [ "$PFTPD_ALLOW_SYSTEM_USER" = yes ]
  then
    pftpd_switches=$pftpd_switches' -l unix -A '
  fi

  # only listen on IPv4 addresses?
  if [ "$PFTPD_ONLY_IPV4" = yes ]
  then
    pftpd_switches=$pftpd_switches' -4 '
  fi
  
  # use version with --with-virtualchroot?
  if [ "$PFTPD_ALLOW_SYMLINKS" = yes ]
  then
    /usr/local/sbin/pure-ftpds $pftpd_switches &
  else
    /usr/local/sbin/pure-ftpd $pftpd_switches &
  fi
  
  end_script
fi
