#!/bin/sh

# retirement is a wrapper for "orphaner" displaying with dialog a list of
# packages you haven't used in a while according to "popularity-contest". 
# Packages may be selected for removal with apt-get which is then called to
# do the work.

# Copyright (c) 2009, Christophe Lohr <clohr@users.sourceforge.net>
#
# This program is licensed under the conditions of the MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Tue, 12 May 2009 17:54:58 +0200
VERSION="0.1"



if which gettext > /dev/null; then
	. gettext.sh
else
	gettext() {
		echo "$@"
	}
fi

TEXTDOMAIN=retirementy
export TEXTDOMAIN

# xgettext:sh-format
USAGE=$(gettext 'Usage: %s [--help] [orphaner options]')'\n'
# xgettext:no-sh-format
SEE_MAN=$(gettext 'See retirement(8) and orphaner(8) for a list of valid options.')
# xgettext:sh-format
INVALID_BASENAME=$(gettext 'Invalid basename: %s.')'\n'
# xgettext:sh-format
INVALID_OPTION=$(gettext '%s: Invalid option: %s.')'\n'
# xgettext:sh-format
MISSING_DIALOG=$(gettext '%s: You need "dialog" in $PATH to run this frontend.')'\n'
# xgettext:no-sh-format
SCREEN_TOO_SMALL=$(gettext 'Screen too small or set $LINES and $COLUMNS.')

# xgettext:no-sh-format
OLDER=$(gettext 'is older than')
# xgettext:no-sh-format
SHOULD_UPDATE=$(gettext 'Should I update your popularity-contest?')
# xgettext:no-sh-format
ADVICE_YES=$(gettext 'You should answer YES.')
# xgettext:no-sh-format
ADVICE_NO=$(gettext 'You can answer NO.')
# xgettext:no-sh-format
WAIT_UPDATE_POPCON=$(gettext 'Updating popularity-contest, please wait.')



DPKG_STATUS=/var/lib/dpkg/status
POPCON_LOG=/var/log/popularity-contest

# Parse options
OPTIONS4ORPHNER=""
while test $# -gt 0 ; do
	case $1 in
	"-h"|"--help")
		printf "$USAGE" $0
		echo
		echo $SEE_MAN
		exit 0
		;;
	"-f"|"--status-file")
		OPTIONS4ORPHNER+=" $1 $2"
		DPKG_STATUS=$2
		shift
		shift
		;;
	"-c"|"--pocon-file")
		POPCON_LOG=$2
		shift
		shift
		;;
	"-k"|"--keep-file")
		OPTIONS4ORPHNER+=" $1 $2"
		shift
		shift
		;;
	"-a"|"-D"|"-H"|"-n"|"-s"|"--libdevel"|"--guess-"*|"--find-"*|"--nice-mode"|"--all-packages"|"--priority"?*|"-p"?*|"--show-section"|"-force-hold")
		OPTIONS4ORPHNER+=" $1"
		shift
		;;
	*)
		printf "$INVALID_OPTION" $0 "$1" >&2
		exit 1
		;;
	esac
done


if ! which dialog >/dev/null ; then
	printf "$MISSING_DIALOG" $0 >&2
	exit 1
fi

# Adapt to terminal size
if [ -n "${LINES:-}" -a -n "${COLUMNS:-}" ]; then
	# Can't use LINES, because it colides with magic variable
	# COLUMNS ditto
	lines=$(($LINES - 7))
	columns=$(($COLUMNS - 10))

	# unset these magic variables to avoid unwished effects
	unset LINES COLUMNS
else
	size=$(stty size)
	lines=$((${size% *} - 7))
	columns=$((${size#* } - 10))

	sigwinch_handle()
	{
		size=$(stty size)
		lines=$((${size% *} - 7))
		columns=$((${size#* } - 10))

		if [ $lines -ge 12 -a $columns -ge 50 ]; then
			LISTSIZE="$lines $columns $(($lines - 7))"
			BOXSIZE="$lines $columns"
		fi
	}

	case "${BASH_VERSION:+bash}" in
		bash) trap sigwinch_handle SIGWINCH;;
		*) trap sigwinch_handle 20 28;;
	esac
fi

if [ $lines -lt 12 -o $columns -lt 50 ]; then
	echo $SCREEN_TOO_SMALL >&2
	exit 1
fi

LISTSIZE="$lines $columns $(($lines - 7))"
BOXSIZE="$lines $columns"
                


if [ -e "$DPKG_STATUS" ]; then
	CTIME_DPKG_STATUS=$(stat -c '%Z' "$DPKG_STATUS")
else
	echo Error: invalid dpkg status file \"$DPKG_STATUS\" >&2
	exit 1
fi


if [ -e "$POPCON_LOG" ]; then
	CTIME_POPCON_LOG=$(stat -c '%Z' "$POPCON_LOG")

	if [ "$CTIME_DPKG_STATUS" -gt "$CTIME_POPCON_LOG" ]; then
		MSG="$POPCON_LOG $OLDER $DPKG_STATUS\n$ADVICE_YES\n"
		DEFAULT="--defaultyes"
	else
		MSG="$DPKG_STATUS $OLDER $POPCON_LOG\n$ADVICE_NO\n"
		DEFAULT="--defaultno"
	fi

	dialog \
		--backtitle "Retirement $VERSION" \
		--title "Retirement $VERSION" \
		$DEFAULT --yesno "$MSG\n$SHOULD_UPDATE" $BOXSIZE
	DO_UPDATE=$?
else
	DO_UPDATE=0
fi

if [ "$DO_UPDATE" -eq 0 ]; then
	dialog \
		--backtitle "Retirement $VERSION" \
		--title "Retirement $VERSION" \
		--infobox "$WAIT_UPDATE_POPCON\npopularity-contest > $POPCON_LOG" \
		$BOXSIZE
	popularity-contest > $POPCON_LOG
fi


echo Now launching orphaner with popularity-contest...
echo Options: $OPTIONS4ORPHNER
echo ...
