#!/bin/bash
#
# Copyright 2006 Mike Pagano
# Distributed under the terms of the GNU General Public License v2
#
# $Header$
# Author: Mike Pagano <mpagano@gmail.com>
#
# Portions written ripped from
# - etc-update, by Brandon Low <lostlogic@gentoo.org>
#

version="0.0.5"
declare -i count=0

usage() {
	echo "Usage: elog-list [options]"
	echo "Display the list of elogs on the systen and view or delete each one separately."

	echo "Options:"
	echo "  -d, --use-directory		Indicate the location of the portage elog files."
	echo "  -h, --help        	   	Display the usage."
	echo "  -v, --version			Display the version."
	exit 1
}


scan() {

	if [[ -z $ELOG_DIR ]]; then
		PORTAGE_TMPDIR=$(portageq envvar PORTAGE_TMPDIR)

		if [[ -z $PORTAGE_TMPDIR ]]; then
			echo "Unable to retrieve portage variable PORTAGE_TMPDIR"
			echo "Try with --use-directory to indicate elogs directory"
			exit
		fi

		ELOG_DIR=$PORTAGE_TMPDIR/elogs
	fi

	curr_dir=$PWD
	cd $ELOG_DIR 2>/dev/null

	

	files=`ls -1tr *.log 2>/dev/null`
	count=$(ls *.log 2>/dev/null |wc -l)

	cd $curr_dir

	file_array=($files)

	if [[ ${#file_array[@]} -eq 0 ]]; then
		echo "No elogs found in $ELOG_DIR"
		exit 0
	fi
}

function do_file() {
	
    local numfiles=$(ls ${ELOG_DIR}/*.log|wc -l)

	if [[ $input -gt 0 && $input -le $numfiles ]]; then
		file=${file_array[(( $input -1 )) ]}
		
		cat "$ELOG_DIR/$file" | less

		sub_input=0

		until [ ${sub_input} == -1 ] || [ ${sub_input} == -3 ]; do
			clear
			echo
			echo "$file"
        	echo "	( 1 to redisplay the contents of this file )"
        	echo "	(-1 to exit this sub-menu) (-3 to delete the file)"
			read sub_input

			if (( ${sub_input} == 1 )); then
				do_file
			fi

			if (( ${sub_input} == -3 )); then
				rm "$ELOG_DIR/$file"
				scan
			fi

		done
				
	fi
}

function sel_file() {

	local numfiles=${#file_array[@]}

	clear

	input=0
    until [ $numfiles -le 0  ] || [ ${input} == -1 ]; do
		
		echo
        echo "portage elog files located in $ELOG_DIR"
		echo

   		index=0
	    for i in "${file_array[@]}"; do
       		index=$((index+1))
          	echo "$index) $i"
       	done
    
        echo    "Please select a file to view by entering the corresponding number."
        echo    "              (-1 to exit)"
        read input
        
		if (( ${input} == -1 )); then
			exit 0
        fi

		if [[ $input -gt 0 && $input -le $numfiles ]]; then
			break;
		fi
  	done
}


ROOT_UID=0 

if [ "$UID" -ne "$ROOT_UID" ]; then
	echo "You must be root to run this script."
	exit 0
fi

ELOG_DIR=""
PORTAGE_TMPDIR=""

if [ $# -ne 0 ]; then
	for x in "$@"; do 
		case $x in 
			-d|--use-directory)
	    		ELOG_DIR="$2" 
				shift
            	;;
        	-v|--version)
	    		echo $version 
            	exit 0
            	;;
        	-h|--help)
	    		usage 
            	exit 1
            	;;
			-*)
				usage 
				exit 1;;
		esac
	done
fi

scan

input=0

until (( ${input} == -1 )); do

    sel_file

    if (( ${input} != -1 )); then
        do_file $input
    fi
done
