Browse Source

Beginnings.

Douglas William Thrift 14 years ago
commit
fa04fd8637
3 changed files with 99 additions and 0 deletions
  1. 3 0
      .gitattributes
  2. 14 0
      GNUmakefile
  3. 82 0
      apt-p2p-clean

+ 3 - 0
.gitattributes

@@ -0,0 +1,3 @@
+GNUmakefile	ident
+apt-p2p-clean	ident
+# vim: shiftwidth=8 tabstop=8

+ 14 - 0
GNUmakefile

@@ -0,0 +1,14 @@
+# APT P2P Clean
+#
+# Douglas Thrift
+#
+# $Id$
+
+.PHONY: all clean
+
+all: apt-p2p-clean.8
+
+%.8: %
+	help2man -N -s 8 $(shell realpath $<) > $@
+
+clean:

+ 82 - 0
apt-p2p-clean

@@ -0,0 +1,82 @@
+#!/usr/bin/env bash
+# APT P2P Clean
+#
+# Douglas Thrift
+#
+# $Id$
+
+function usage()
+{
+	cat <<-EOF
+	Usage: $program [options]
+
+	Options:
+	  -e, --execute     clean up the apt-p2p cache
+	  -h, --help        show this help message and exit
+	  -n, --no-execute  show what would be done to clean up the apt-p2p cache
+	EOF
+	exit $@
+}
+
+program=`basename $0`
+args=`getopt -l 'execute,help,no-execute' -n "$program" -o 'ehn' -- "$@"`
+val=$?
+
+[[ $val != 0 ]] && usage $val
+
+eval set -- "$args"
+unset args val
+
+until [[ "$1" == -- ]]; do
+	case "$1" in
+	(-e|--execute)
+		execute=1
+		;;
+	(-h|--help)
+		usage
+		;;
+	(-n|--no-execute)
+		execute=0
+		;;
+	esac
+
+	shift
+done
+
+[[ -z $execute ]] && usage
+
+set -e
+
+if [[ $execute -eq 1 ]]; then
+	echo 'Removing files:'
+else
+	echo 'Would remove files:'
+fi
+
+find /var/cache/apt-p2p/cache -name '*.deb' | (
+	freed=0
+	
+	while read deb; do
+		if [[ ! -f /var/cache/apt/archives/${deb##*/} ]]; then
+			((freed += `du -B 1 $deb | cut -f 1`))
+
+			echo ${deb##*/}
+
+			[[ $execute -eq 1 ]] && rm -f $deb
+		fi
+	done
+
+	if [[ $execute -eq 1 ]]; then
+		[[ $freed != 0 ]] && invoke-rc.d apt-p2p restart
+
+		echo -n 'Freed'
+	else
+		echo -n 'Would free'
+	fi
+
+	echo " `bc <<-EOF
+	freed=$freed
+	scale=1
+	if (freed >= 1024 ^ 3) print freed / 1024 ^ 3, "G" else if (freed >= 1024 ^ 2) print freed / 1024 ^ 2, "M" else if (freed >= 1024) print freed / 1024, "K" else freed
+	EOF`B of disk space"
+)