--- swapd-0.2.orig/debian/control
+++ swapd-0.2/debian/control
@@ -0,0 +1,21 @@
+Source: swapd
+Section: admin
+Priority: optional
+Maintainer: Aigars Mahinovs <aigarius@debian.org>
+Build-Depends: debhelper (>=4.0.0)
+Standards-Version: 3.5.8
+
+Package: swapd
+Architecture: any
+Depends: ${shlibs:Depends}
+Description: Swap demon for dynamic swap file creation
+ Swapd is a dynamic swapping manager for Linux. It provides the system with
+ as much swap space (virtual memory) as is required at a particular time by
+ dynamically creating swap files. This is more convenient than using fixed swap
+ files and/or partitions because they (a) are unused most of the time and are
+ just taking up disk space; and (b) provide a limited amount of virtual memory.
+ .
+ On systems that have constant need for virtual memory it would still be
+ wise to use a swap partition in parallel with dynamic swapping, since swap
+ partitions provide much faster access than swap files.
+  
--- swapd-0.2.orig/debian/prerm
+++ swapd-0.2/debian/prerm
@@ -0,0 +1,37 @@
+#! /bin/sh
+# prerm script for #PACKAGE#
+#
+# see: dh_installdeb(1)
+
+set -e
+
+# summary of how this script can be called:
+#        * <prerm> `remove'
+#        * <old-prerm> `upgrade' <new-version>
+#        * <new-prerm> `failed-upgrade' <old-version>
+#        * <conflictor's-prerm> `remove' `in-favour' <package> <new-version>
+#        * <deconfigured's-prerm> `deconfigure' `in-favour'
+#          <package-being-installed> <version> `removing'
+#          <conflicting-package> <version>
+# for details, see /usr/doc/packaging-manual/
+
+case "$1" in
+    remove|upgrade|deconfigure)
+#       install-info --quiet --remove /usr/info/#PACKAGE#.info.gz
+        ;;
+    failed-upgrade)
+        ;;
+    *)
+        echo "prerm called with unknown argument \`$1'" >&2
+        exit 0
+    ;;
+esac
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+exit 0
+
+
--- swapd-0.2.orig/debian/rules
+++ swapd-0.2/debian/rules
@@ -0,0 +1,77 @@
+#!/usr/bin/make -f
+# Sample debian/rules that uses debhelper.
+# GNU copyright 1997 to 1999 by Joey Hess.
+
+# Uncomment this to turn on verbose mode.
+#export DH_VERBOSE=1
+
+# This is the debhelper compatability version to use.
+export DH_COMPAT=4
+
+build: build-stamp
+build-stamp:
+	dh_testdir
+
+	./configure --prefix=/usr --mandir=\$${prefix}/share/man
+	# Add here commands to compile the package.
+	$(MAKE)
+
+	touch build-stamp
+
+clean:
+	dh_testdir
+	dh_testroot
+	rm -f build-stamp
+
+	# Add here commands to clean up after the build process.
+	-$(MAKE) distclean
+
+	dh_clean
+
+install: build
+	dh_testdir
+	dh_testroot
+	dh_clean -k
+	dh_installdirs
+
+	# Add here commands to install the package into debian/tmp.
+	$(MAKE) install prefix=`pwd`/debian/swapd/usr
+
+
+# Build architecture-independent files here.
+binary-indep: build install
+# We have nothing to do by default.
+
+# Build architecture-dependent files here.
+binary-arch: build install
+#	dh_testversion
+	dh_testdir
+	dh_testroot
+#	dh_installdebconf	
+	dh_installdocs
+#	dh_installexamples
+#	dh_installmenu
+#	dh_installemacsen
+#	dh_installpam
+	dh_installinit
+#	dh_installcron
+	dh_installman swapd.8
+#	dh_installinfo
+#	dh_undocumented
+	dh_installchangelogs 
+	dh_link
+	dh_strip
+	dh_compress
+	dh_fixperms
+	# You may want to make some executables suid here.
+#	dh_suidregister
+	dh_makeshlibs
+	dh_installdeb
+#	dh_perl
+	dh_shlibdeps
+	dh_gencontrol
+	dh_md5sums
+	dh_builddeb
+
+binary: binary-indep binary-arch
+.PHONY: build clean binary-indep binary-arch binary install
--- swapd-0.2.orig/debian/changelog
+++ swapd-0.2/debian/changelog
@@ -0,0 +1,105 @@
+swapd (0.2-10) unstable; urgency=low
+
+  * Fixed a multiline string variable FTBFS bug (Closes: #195042)
+
+ -- Aigars Mahinovs <aigarius@debian.org>  Fri, 11 Apr 2003 20:10:45 +0200
+
+swapd (0.2-9) unstable; urgency=low
+
+  * Fixed /proc/meminfo parsing to be able to work with
+    2.5 kernels (Thanks Wladimir Mutel and Nick Holloway)
+
+ -- Aigars Mahinovs <aigarius@debian.org>  Fri, 11 Apr 2003 20:10:45 +0200
+
+swapd (0.2-8) unstable; urgency=low
+
+  * Do not kill the parent if in debug mode (Closes: #175332)
+  * Updated standarts version to 3.5.8
+  
+ -- Aigars Mahinovs <aigarius@debian.org>  Sun, 05 Jan 2003 14:10:45 +0200
+ 
+swapd (0.2-7) unstable; urgency=low
+
+  * Updates standarts version to 3.5.6
+  * only remove swap dir on purge (Closes: #143357)
+  * fix error on upgrade if swapd is not running (Closes: #145082)
+  * read /proc/swaps blockwise (Closes: #143278)
+    
+ -- Aigars Mahinovs <aigarius@debian.org>  Tue, 19 Nov 2002 16:43:23 +0200
+
+swapd (0.2-6) unstable; urgency=low
+
+  * /proc/swaps fix ( Closes: #141212 )
+    ( Thanks to Jordan and Charles for patches ) 
+  * some additional options to start-stop-daemon ( Closes: #142668, #142275 )
+    
+ -- Aigars Mahinovs <aigarius@debian.org>  Tue, 16 Apr 2002 23:03:23 +0200
+
+swapd (0.2-5) unstable; urgency=low
+
+  * now really fixed 93766 by going to sleep if making a swafile fails
+  * reduced default pause between wake-up's
+  * reduce pauses between wake-up's if close to memlimit
+
+ -- Aigars Mahinovs <aigarius@debian.org>  Wed, 10 Apr 2002 20:12:00 +0200
+
+swapd (0.2-4) unstable; urgency=low
+
+  * added debugging option ( Closes: #108491 )
+  * fixed description spelling ( Closes: #125402 )
+  * made pause between wake-up's increase ( Closes: #93766 )
+  
+ -- Aigars Mahinovs <aigarius@debian.org>  Mon, 01 Apr 2002 20:00:24 +0200
+
+swapd (0.2-3) unstable; urgency=low
+
+  * Some work toward fixing #93766 and #92006
+  * Changing maxswaps to show maximum swaps in kernel.
+    See README for details.
+  * Setting default maxswaps to 8
+
+ -- Aigars Mahinovs <aigarius@debian.org>  Sat, 02 Jun 2001 12:30:23 +0200
+
+swapd (0.2-2) unstable; urgency=low
+
+  * Added Build-Depends
+  * Updated Standart version to 3.5.2
+  * Fixed typos in README.Debian ( Closes: #92298, #92299, #92302 )
+  * Fixed a typo in postrm ( Closes: #92770 )
+
+ -- Aigars Mahinovs <aigarius@debian.org>  Wed, 28 Mar 2001 12:43:22 +0200
+
+swapd (0.2-1) unstable; urgency=low
+
+  * New upstream release
+  * First upload to Debian
+  * [Upstream] Fixed possible memory leaks
+  * [Upstream] Swapd no longer ignores termination requests 
+    when retrying to check memory
+  * [Upstream] Swapd now runs in its own process group
+  * [Upstream] Included the 'pause' option for specifying the 
+    pause between memory checks (enables lower CPU usage)
+
+ -- Aigars Mahinovs <aigarius@debian.org>  Mon, 12 Mar 2001 09:44:33 +0200
+
+swapd (0.1-3) unstable; urgency=low
+
+  * Added init.d script
+
+ -- Aigars Mahinovs <aigarius@zednet.lv>  Thu, 16 Mar 2000 21:17:20 +0000
+
+swapd (0.1-2) unstable; urgency=low
+
+  * Fixed some typos    
+
+ -- Aigars Mahinovs <aigarius@zednet.lv>  Thu, 16 Mar 2000 21:17:20 +0000
+ 
+swapd (0.1-1) unstable; urgency=low
+
+  * Initial Release.
+
+ -- Aigars Mahinovs <aigarius@zednet.lv>  Thu, 16 Mar 2000 21:17:20 +0000
+
+Local variables:
+mode: debian-changelog
+End:
--- swapd-0.2.orig/debian/postinst
+++ swapd-0.2/debian/postinst
@@ -0,0 +1,48 @@
+#! /bin/sh
+# postinst script for swapd
+#
+# see: dh_installdeb(1)
+
+set -e
+
+# summary of how this script can be called:
+#        * <postinst> `configure' <most-recently-configured-version>
+#        * <old-postinst> `abort-upgrade' <new version>
+#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
+#          <new-version>
+#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
+#          <failed-install-package> <version> `removing'
+#          <conflicting-package> <version>
+# for details, see /usr/doc/packaging-manual/
+#
+# quoting from the policy:
+#     Any necessary prompting should almost always be confined to the
+#     post-installation script, and should be protected with a conditional
+#     so that unnecessary prompting doesn't happen if a package's
+#     installation fails and the `postinst' is called with `abort-upgrade',
+#     `abort-remove' or `abort-deconfigure'.
+
+case "$1" in
+    configure)
+    mkdir -p /swap
+    true
+    ;;
+
+    abort-upgrade|abort-remove|abort-deconfigure)
+
+    ;;
+
+    *)
+        echo "postinst called with unknown argument \`$1'" >&2
+        exit 0
+    ;;
+esac
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+exit 0
+
+
--- swapd-0.2.orig/debian/init.d
+++ swapd-0.2/debian/init.d
@@ -0,0 +1,61 @@
+#! /bin/sh
+#
+# Version:	@(#)skeleton  1.8  03-Mar-1998  miquels@cistron.nl
+#
+# This file was automatically customized by dh-make on Thu, 16 Mar 2000 21:17:20 +0000
+
+PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
+DAEMON=/usr/sbin/swapd
+NAME=swapd
+DESC=swapd
+
+test -f $DAEMON || exit 0
+
+set -e
+
+case "$1" in
+  start)
+	start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
+		--exec $DAEMON
+	;;
+  stop)
+	echo -n "Stopping $DESC: "
+	start-stop-daemon --stop --quiet --signal 9 --oknodo --pidfile /var/run/$NAME.pid \
+		--exec $DAEMON
+	echo "$NAME."
+	;;
+  #reload)
+	#
+	#	If the daemon can reload its config files on the fly
+	#	for example by sending it SIGHUP, do it here.
+	#
+	#	If the daemon responds to changes in its config file
+	#	directly anyway, make this a do-nothing entry.
+	#
+	# echo "Reloading $DESC configuration files."
+	# start-stop-daemon --stop --signal 1 --quiet --pidfile \
+	#	/var/run/$NAME.pid --exec $DAEMON
+  #;;
+  restart|force-reload)
+	#
+	#	If the "reload" option is implemented, move the "force-reload"
+	#	option to the "reload" entry above. If not, "force-reload" is
+	#	just the same as "restart".
+	#
+	echo "Restarting $DESC: "
+	start-stop-daemon --stop --quiet --oknodo --pidfile \
+		/var/run/$NAME.pid --exec $DAEMON
+	sleep 1
+	start-stop-daemon --start --quiet --pidfile \
+		/var/run/$NAME.pid --exec $DAEMON
+	echo "$NAME."
+	;;
+  *)
+	N=/etc/init.d/$NAME
+	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
+	echo "Usage: $N {start|stop|restart|force-reload}" >&2
+	exit 1
+	;;
+esac
+
+exit 0
--- swapd-0.2.orig/debian/README.Debian
+++ swapd-0.2/debian/README.Debian
@@ -0,0 +1,25 @@
+The first thing you should know is that Linux supports only 8 swaps (either
+files or partitions) by default. If you intend to take full advantage of
+dynamic swapping, you should recompile your kernel to support more swaps, or
+get one which already does.
+
+
+RECOMPILING THE KERNEL
+
+1. Make sure you are logged in as root.
+2. Install one of "kernel-source" and the "kernel-package" packages.
+3. Unpack your kernel source ( it is in /usr/src ).
+4. In include/linux/swap.h under Linux source directory change the value 
+   of MAX_SWAPFILES from 8 to 256. This defines how many swaps you will be 
+   able to have.
+5. Follow the instructions in kernel-package readme to configure, compile,
+   package and install a new kernel.
+6. Edit /etc/swapd.conf and inrease maxswaps to 256.
+
+Please bear in mind that these changes will take effect after a reboot.
+
+Take a look at /etc/swapd.conf and change what is necessary
+(please refer to section 'PERFORMANCE' in the man page of swapd).
+
+As of version 0.2-3, swapd also counts your swapfiles ( those not
+made by swapd itself ) when looking at maxswaps parameter.
--- swapd-0.2.orig/debian/postrm
+++ swapd-0.2/debian/postrm
@@ -0,0 +1,37 @@
+#! /bin/sh
+# postrm script for swapd
+#
+# see: dh_installdeb(1)
+
+set -e
+
+# summary of how this script can be called:
+#        * <postrm> `remove'
+#        * <postrm> `purge'
+#        * <old-postrm> `upgrade' <new-version>
+#        * <new-postrm> `failed-upgrade' <old-version>
+#        * <new-postrm> `abort-install'
+#        * <new-postrm> `abort-install' <old-version>
+#        * <new-postrm> `abort-upgrade' <old-version>
+#        * <disappearer's-postrm> `disappear' <r>overwrit>r> <new-version>
+# for details, see /usr/doc/packaging-manual/
+
+case "$1" in
+    purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
+        ;;
+    purge)
+	-rmdir --ignore-fail-on-non-empty /swap
+	;;
+
+    *)
+        echo "postrm called with unknown argument \`$1'" >&2
+        exit 0
+
+esac
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+
--- swapd-0.2.orig/debian/copyright
+++ swapd-0.2/debian/copyright
@@ -0,0 +1,16 @@
+This package was debianized by Aigars Mahinovs <aigarius@zednet.lv> on
+Thu, 16 Mar 2000 21:17:20 +0000.
+
+It was downloaded from : http://freshmeat.net
+
+Upstream Author: Neven Lovric <nlovric@jagor.srce.hr>
+
+Copyright:
+
+swapd v0.2 (May 11, 2000), copyright 2000 Neven Lovric <nlovric@jagor.srce.hr>
+
+This program is free software released under the terms of the GNU General
+Public License. Please refer to file '/usr/share/common-licenses/GPL' 
+for more information.
+
+
--- swapd-0.2.orig/swapd.8
+++ swapd-0.2/swapd.8
@@ -0,0 +1,147 @@
+.TH swapd 8 "May 21, 2000" swapd
+.SH NAME
+swapd - dynamic swapping manager
+.SH SYNOPSIS
+\fBswapd\fR [\fB-h\fR] [\fB--help\fR] [\fB--copyright\fR] \
+[\fB--config\fR \fIfile\fR] [\fB--maxswaps\fR \fIn\fR] \
+[\fB--memlimit\fR \fIkb\fR] [\fB--mkswap\fR \fIpath\fR] \
+[\fB--pause\fR \fImsec\fR] [\fB--pidfile\fR \fIfile\fR] \
+[\fB--swapdir\fR \fIdir\fR] [\fB--swapsize\fR \fIkb\fR] \
+[\fB--timeout\fR \fIsec\fR]
+.SH DESCRIPTION
+swapd is a dynamic swapping manager. It provides the system with \
+as much swap space (virtual memory) as is required at a particular time by \
+dynamicly creating swap files. This is more convinient than using fixed swap \
+files and/or partitions because they (a) are unused most of the time and are \
+just taking up disk space; and (b) provide a limited amount of virtual memory.
+.SH OPTIONS
+All values given on the command line will override config file values.
+.TP 16
+\fB-h\fR,\ \fB--help\fR
+Displays the command line help.
+.TP 16
+\fB--copyright\fR
+Displays the copyright notice.
+.TP 16
+\fB--config\fR\ \fIfile\fR
+Location of an alternate configuration file.
+.TP 16
+\fB--maxswaps\fR\ \fIn\fR
+Maximum number of swap files. No more than \fIn\fR swap files will be used. \
+The default is 8 (as many as the default kernel will allow).
+.TP 16
+\fB--memlimit\fR\ \fIkb\fR
+Memory limit in kilobytes. When the total amount of free memory gets below \
+this number, swapd creates a new swap file.
+.TP 16
+\fB--mkswap\fR\ \fIpath\fR
+Location of \fImkswap(8)\fR, usually /sbin/mkswap.
+.TP 16
+\fB--pause\fR\ \fImsec\fR
+Pause between memory checks in miliseconds (when the total amount of free \
+memory is above \fImemlimit\fR).
+.TP 16
+\fB--pidfile\fR\ \fIfile\fR
+Location of the PID file, usually /var/run/swapd.pid.
+.TP 16
+\fB--swapdir\fR\ \fIdir\fR
+Swap directory where all the swap files are kept.
+.TP 16
+\fB--swapsize\fR\ \fIkb\fR
+Swap file size (>=64k).
+.TP 16
+\fB--timeout\fR\ \fIsec\fR
+Timeout. If the last created swap file is unused for \fIsec\fR seconds, it \
+will be removed. The last created swapfile is considered unused when there \
+are more than \fImemlimit\fR + \fIswapsize\fR kb of free memory (physical + swap).
+.SH CONFIGURATION
+This is an example configuration file:
+
+.nf
+# swapd.conf - config file for swapd
+#
+# Copyright 2000 Neven Lovric <nlovric@jagor.srce.hr>
+#
+
+# Memory limit in kilobytes.
+# When the total amount of free memory gets below this number, swapd creates
+# a new swap file.
+# 16384 or more recommended
+memlimit 16384
+
+# Pause between memory checks in miliseconds.
+# When the total amount of free memory is above <memlimit>, swapd will pause
+# for <pause> miliseconds before checking memory again.
+# 1000 should be ok for most systems
+pause 1000
+
+# Swap file size in kilobytes.
+# >= 64, 4096 recommended
+swapsize 4096
+
+# Maximum number of swap files.
+# No more than <maxswaps> swap files will be used.
+# 0 = unlimited (as many as the kernel will allow)
+# 8 = how many a default kernel allows
+maxswaps 8
+
+# Timeout in seconds.
+# If the last created swap file is unused for <sec> seconds, it will be
+# removed. The last created swapfile is considered unused when there are
+# more than <memlimit> + <swapsize> kb of free memory (physical + swap).
+# 60 is nice
+timeout 60
+
+# Swap directory where all the swap files are kept.
+swapdir /swap
+
+# PID file (where the currently running swapd stores it's PID so a new swapd
+# can find it)
+pidfile /var/run/swapd.pid
+
+# Full path to mkswap.
+mkswap /sbin/mkswap
+.fi
+.SH PERFORMANCE
+The most important parameter which may drasticly effect performance is the \
+memory limit. If the memory limit is low, (1) there may not be enough memory \
+ready for a program to allocate at once or (2) swapd may not be able to \
+create new swap files before we run out of memory. If your programs are
+running out of memory, it will primarily be due to a low memory limit. If \
+you really want to have a low memory limit, you should decrease the swap \
+size and pause.
+
+The second important parameter is the swap size. If you choose to use bigger \
+swap files, you may experience slowdowns when swap files are created. On the \
+other hand, small swap files may lower performance when there are many of \
+them. The important thing is not to make them too big to create before we \
+run out of memory.
+
+Another important parameter is the pause, which should be shorter on faster \
+systems. If it is too long, swapd may fail to detect rapid memory changes \
+and therefore fail to create new swap files when they are needed. However, \
+if you notice swapd is constantly using too much CPU, increase the pause.
+.fi
+.SH FILES
+.B /etc/swapd.conf
+.br
+.B /var/run/swapd.pid
+.br
+.B /etc/init.d/swapd
+.br
+.B SWAPDIR/linux*.swp
+.br
+.SH BUGS
+Might fail if swapping over NFS
+.SH SEE ALSO
+\fBmkswap\fR(8), \fBswapon\fR(8), \fBswapoff\fR(8), \fBfree\fR(8)
+.SH MAINTAINER
+Aigars Mahinovs <aigarius@debian.org>
+.SH AUTHOR
+Neven Lovric <nlovric@linux.hr>
+
+The latest upstream version is available via anonymous ftp:
+
+.RS
+\fIftp://ftp.linux.hr/pub/swapd\fR
+.RE
--- swapd-0.2.orig/swapd.c
+++ swapd-0.2/swapd.c
@@ -1,4 +1,5 @@
 /* swapd.c, copyright Neven Lovric <nlovric@linux.hr> */
+/* Modifications by Aigars Mahinovs <aigarius@debian.org>, GPL */
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -30,7 +31,7 @@
 unsigned long swapsizevar;
 unsigned long maxswaps = 0;
 unsigned long timeout;
-unsigned long pausevar = 1000000;
+unsigned long pausevar = 100000;
 char swapdir[256];
 char pidfile[256] = PIDFILE;
 char conffile[256] = CONFFILE;
@@ -38,6 +39,9 @@
 unsigned long unusedswap = -1;
 int die = 0;
 unsigned long set = 0;
+int debug = 0;		/* Added for debbugging */
+int idle = 0;		/* How many cycles has the deamon been idle 
+			minimum 0 maximum 16 */
 
 int main(int argc, char **argv)
 {
@@ -50,6 +54,7 @@
    unsigned long oldspace = 0;
    unsigned long sum;
    int meminfofd;
+   unsigned long kswaps = 0;
 
    printf("swapd 0.2 (May 21, 2000), copyright Neven Lovric <nlovric@linux.hr>\n");
    if (!geteuid()) {
@@ -97,13 +102,13 @@
       perror("swapd: /proc/meminfo");
       exit(1);
    }
-   if (sfork()) {
+   if (!debug && sfork()) {
       signal(SIGINT, exitonsig);
       wait(NULL);
       exit(0);
    }
    usleep(1);
-   setpriority(PRIO_PROCESS, 0, -1);
+   setpriority(PRIO_PROCESS, 0, -19);
    setprocname(argc, argv, PROCNAME);
    if (chkpidfile())
       exit(1);
@@ -120,7 +125,8 @@
    }
    if (swaps)
       printf("swapd: %lu swap files (%lukb total) found and reused.\n", swaps, oldspace >> 10);
-   kill(getppid(), SIGINT);
+   if (!debug)			/* Do not kill the parent if in debug*/
+      kill(getppid(), SIGINT);
    setsid();
    signal(SIGINT, handlesig);
    signal(SIGHUP, handlesig);
@@ -132,14 +138,16 @@
       }
       if ((mem = freemem(meminfofd)) == -1)
          continue;
-      if (mem < memlimit && (!maxswaps || swaps < maxswaps)) {
+      kswaps = usedswaps();
+      if (mem < memlimit && (!maxswaps || kswaps < maxswaps)) {
          swaps = newswap(swaps);
+	 idle = 0;
          lastused = srealloc(lastused, swaps * sizeof(time_t));
          lastused[swaps - 1] = 0;
          swapspace = srealloc(swapspace, swaps * sizeof(time_t));
          swapspace[swaps - 1] = swapsize(swapname(swaps - 1));
       }
-      if (swaps) {
+      else if (swaps) {
          for (n = 1, sum = 0; n <= swaps; n++) {
             sum += swapspace[swaps - n];
             if (mem > memlimit + sum) {
@@ -156,8 +164,10 @@
             swapspace = srealloc(swapspace, swaps * sizeof(time_t));
          }
       }
-      if (mem > memlimit)
-         usleep(pausevar);
+     if (mem < memlimit*2) 
+        idle = (idle>1) ? idle-2 : idle ;
+     idle = (idle<16) ? idle+1 : idle ; 
+     usleep(pausevar*idle);
    }
 }
 
@@ -210,9 +220,10 @@
 
 void phelp(char **argv)
 {
-   printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
+   printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
       "\nUsage: ", argv[0], " [options]\n\n",
       "   -h, --help        you are reading it!\n",
+      "   -d, --nofork      do not fork to background, debugging mode\n",
       "   --copyright       display the copyright notice\n",
       "   --config file     config file (default: ", CONFFILE, ")\n",
       "   --maxswaps n      maximum number of swap files (0 = unlimited, default: 0)\n",
@@ -238,6 +249,11 @@
          phelp(argv);
          return(1);
       }
+      if (!strcmp(argv[n], "-d") || !strcmp(argv[n], "--nofork")) {
+         debug = 1;
+         continue;
+      }
+      
       if (!strcmp(argv[n], "--copyright")) {
          printf("\n%s\n", copyright);
          return(1);
@@ -481,6 +497,7 @@
             remove(swapfile);
             return(swaps);
          }
+      fsync(fd); /* Added for NFS bug */
       if (close(fd) == -1) {
          remove(swapfile);
          return(swaps);
@@ -490,14 +507,20 @@
          return(swaps);
       }
    }
+   
    if (swapon(swapfile, 0) == -1 && errno != EINVAL) {
       if (errno != ENOENT)
          unusedswap = swaps;
       else
          unusedswap = -1;
+      remove(swapfile);  /* Added for endless swapfile generation bug */
+      idle = (idle<12) ? idle+4 : idle ; /* Sleep more if we cann't make a swapfile
+                                            as we've hit the limit and cann't help anymore */
       return(swaps);
    } else
       unusedswap = -1;
+   if(debug)
+	fprintf(stdout, "Added swapfile nr:%li, file:%s \n",swaps+1,swapfile);
    return(swaps + 1);
 }
 
@@ -515,16 +538,42 @@
    if (doswapoff(swapfile) == -1 && errno != ENOENT && errno != EINVAL)
       return(swaps);
    remove(swapfile);
+   if(debug)
+	fprintf(stdout, "Removed swapfile nr:%li, file:%s \n",swaps-1,swapfile);
    return(swaps - 1);
 }
 
 void handlesig(int sig)
 {
+   if(debug)
+	fprintf(stdout, "Caught signal:%i\n",sig);
    signal(sig, handlesig);
    die = 1;
 }
 
 void exitonsig(int sig)
 {
+   if(debug)
+	fprintf(stdout, "Exiting on signal:%i\n",sig);
    exit(0);
 }
+
+int usedswaps(void) /* Returns number of lines /proc/swaps-1 = number of swaps */
+{
+   int file;
+   char buf[512];
+   int count;
+   count=-1;
+   
+   if ((file = open("/proc/swaps", O_RDONLY)) >= 0){
+      int n ;
+      while ((n = read (file, buf, sizeof (buf)-1)) > 0 ){
+         char *p = buf ;
+         buf [n] = '\0' ;
+         while ((p = strchr (p, '\n')) != 0)
+            count++, p++ ;
+      };
+      close (file);
+   };
+   return count;
+}
--- swapd-0.2.orig/swapd.h
+++ swapd-0.2/swapd.h
@@ -25,5 +25,6 @@
 extern unsigned long delswap(unsigned long);
 extern void handlesig(int);
 extern void exitonsig(int);
+extern int usedswaps(void);
 
 #endif /* _SWAPD_H */
--- swapd-0.2.orig/config.h
+++ swapd-0.2/config.h
@@ -1,5 +1,5 @@
 /* Default location of the configuration file */
-#define CONFFILE "/usr/local/etc/swapd.conf"
+#define CONFFILE "/etc/swapd.conf"
 
 /* Default path to mkswap */
 #define MKSWAP "/sbin/mkswap"
--- swapd-0.2.orig/Makefile.in
+++ swapd-0.2/Makefile.in
@@ -2,7 +2,12 @@
 MAKE = @MAKE@
 INSTALL = @INSTALL@
 WARN = -Wall -Wstrict-prototypes -Wmissing-prototypes
-CFLAGS = -O3 $(WARN) -g -I.
+CFLAGS = $(WARN) -g -I.
+ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
+CFLAGS += -O0
+else
+CFLAGS += -O3
+endif
 LDFLAGS = $(WARN) -g @LIBS@
 OBJS = swapd.o fileinfo.o meminfo.o memops.o procname.o procops.o swapinfo.o
 BINFILE = swapd
@@ -14,13 +19,15 @@
 all:	$(BINFILE)
 
 install: all
+ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
 	strip $(BINFILE)
+endif
 	$(INSTALL) -d @sbindir@
 	$(INSTALL) $(BINFILE) @sbindir@
 	$(INSTALL) -d @sysconfdir@
 	$(INSTALL) --mode 644 $(BINFILE).conf @sysconfdir@
-	$(INSTALL) -d @mandir@/man8
-	$(INSTALL) --mode 644 $(BINFILE).8.gz @mandir@/man8
+	$(INSTALL) -d @mandir@/man8
+	$(INSTALL) --mode 644 $(BINFILE).8 @mandir@/man8
 
 $(BINFILE): $(OBJS)
 	$(CC) $(LDFLAGS) $(OBJS) -o $(BINFILE)
--- swapd-0.2.orig/copyright.h
+++ swapd-0.2/copyright.h
@@ -1,8 +1,8 @@
 #ifndef _COPYRIGHT_H
 #define _COPYRIGHT_H
 
-char *copyright = "\
-    This program is free software; you can redistribute it and/or modify
+char *copyright = " GPL v2 ";
+/*  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.
@@ -15,6 +15,6 @@
     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.,
     59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
-";
+*/
 
 #endif /* _COPYRIGHT_H */
--- swapd-0.2.orig/swapd.conf
+++ swapd-0.2/swapd.conf
@@ -22,7 +22,8 @@
 # Maximum number of swap files.
 # No more than <maxswaps> swap files will be used.
 # 0 = unlimited (as many as the kernel will allow)
-maxswaps 0
+# 8 = maximum number of swaps in the default kernel
+maxswaps 7
 
 # Timeout in seconds.
 # If the last created swap file is unused for <sec> seconds, it will be
--- swapd-0.2.orig/copyright
+++ swapd-0.2/copyright
@@ -0,0 +1,16 @@
+This package was debianized by Aigars Mahinovs <aigarius@zednet.lv> on
+Thu, 16 Mar 2000 21:17:20 +0000.
+
+It was downloaded from www.freshmeat.com
+
+Upstream Author(s): Neven Lovric <nlovric@jagor.srce.hr>
+
+Copyright:
+
+swapd v0.1 (Mar 4, 2000), copyright 2000 Neven Lovric <nlovric@jagor.srce.hr>
+
+This program is free software released under the terms of the GNU General
+Public License. Please refer to file '/usr/share/common-licenses/GPL' 
+for more information.
+
+
--- swapd-0.2.orig/meminfo.c
+++ swapd-0.2/meminfo.c
@@ -1,4 +1,7 @@
 /* meminfo.c, copyright Neven Lovric <nlovric@linux.hr> */
+/* Integrated patches from Nick Holloway <Nick.Holloway@alfie.demon.co.uk>
+   by Aigars Mahinovs <aigarius@debian.org> 
+*/
 
 #include <stdio.h>
 #include <sys/types.h>
@@ -11,6 +14,7 @@
 #include <sys/mman.h>
 #include "meminfo.h"
 
+/* Old version (bad for 2.5 kernels
 unsigned long freemem(int fd)
 {
    unsigned long mem, swap;
@@ -20,7 +24,78 @@
       return(UINT_MAX);
    if (read(fd, buf, 1024) == -1)
       return(UINT_MAX);
+      
+   
+
    if (sscanf(buf, "%*s%*s%*s%*s%*s%*s%*s%*s%*s%lu%*s%*s%*s%*s%*s%*s%lu", &mem, &swap) != 2)
       return(UINT_MAX);
    return(mem + swap);
 }
+*/
+
+unsigned long freemem(int fd)
+{
+/*    static  int     fd	= -1;*/		/* fd to read meminfo from */
+    static  char    buffer [ 10240 ];	/* enough to slurp meminfo into */
+    char   *cp;
+    long    memfree, buffers, cached, swapfree;
+    int     n;
+
+    if ( fd < 0 ) {
+        fd = open ( "/proc/meminfo", O_RDONLY );
+        if ( fd < 0 ) {
+        /*syslog ( LOG_ERR, "can't open \"/proc/meminfo\": %m" );*/
+            exit ( UINT_MAX );
+    }
+    }
+
+    if ( lseek ( fd, 0, SEEK_SET ) == EOF ) {
+        /*syslog ( LOG_ERR, "lseek failed on \"/proc/meminfo\": %m" );*/
+        exit ( UINT_MAX );
+    }
+    if ( ( n = read ( fd, buffer, sizeof(buffer)-1 ) ) < 0 ) {
+    /*syslog ( LOG_ERR, "read failed on \"/proc/meminfo\": %m" );*/
+    exit ( UINT_MAX );
+    }
+    buffer[n] = '\0';				/* null terminate */
+
+    memfree = -1;
+    buffers = 0;
+    cached  = 0;
+    swapfree = 0;
+
+    cp = buffer;
+    while ( cp )
+    {
+    if ( strncmp( cp, "MemFree:", 8 ) == 0 )
+    {
+        memfree = atol( cp+9 ) * 1024;
+        /*printf( "MemFree: %ld\n", memfree );*/
+    }
+    else if ( strncmp( cp, "Buffers:", 8 ) == 0 )
+    {
+        buffers = atol( cp+9 ) * 1024;
+        /*printf( "Buffers: %ld\n", buffers );*/
+    }
+    else if ( strncmp( cp, "Cached:", 7 ) == 0 )
+    {
+        cached = atol( cp+8 ) * 1024;
+        /*printf( "Cached: %ld\n", cached );*/
+    }
+    else if ( strncmp( cp, "SwapFree:", 9 ) == 0 )
+    {
+        swapfree = atol( cp+10 ) * 1024;
+	/*printf( "SwapFree: %ld\n", swapfree );*/
+    }
+
+    /* move onto next line */
+    cp = strchr( cp, '\n' );
+    if ( cp )
+    {
+        cp++;
+    }
+    }
+
+    return ( memfree + swapfree + ( buffers + cached ) / 2 );
+}
+																																																				
\ No newline at end of file
