summaryrefslogtreecommitdiff
path: root/bin/eread
blob: 2289f2d46cc66532477b9d05674ea5fd2d87ce0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash

# This is a script to read portage log items from einfo, ewarn etc, new in the
# portage-2.1 series.
#
# Author: Donnie Berkholz <spyderous@gentoo.org>
# Updated by: Uwe Klosa <uwe.klosa@gmail.com>
# Updated by: Slawomir Nizio <slawomir.nizio@sabayon.org>

# Get prefix
EPREFIX=${EPREFIX:-$(portageq envvar EPREFIX)}

# set decent PATH for bug 172969

PATH=${EPREFIX}/usr/bin:${EPREFIX}/bin:${PATH}

# Set ELOGDIR
PORT_LOGDIR="$(portageq envvar PORT_LOGDIR)"
[ "$PORT_LOGDIR" = "" ] && PORT_LOGDIR="${EPREFIX}/var/log/portage"
ELOGDIR="$PORT_LOGDIR/elog"

# Verify that ELOGDIR exists
if [ ! -d "$ELOGDIR" ]; then
	echo "ELOG directory: $ELOGDIR does not exist!"
	exit 1
fi

# Use the pager from the users environment
[ -z "$PAGER" ] && PAGER="less"

# Set up select prompt
PS3="Choice? "

SORT=${EREAD_SORT_ORDER}

find_unsorted() {
	find . -name '*:*:*.log*' | sed -e "s:\./::g"
}
find_by_name() {
	find . -name '*:*:*.log*' | sort | sed -e "s:\./::g"
}
find_by_time() {
	find . -name '*:*:*.log*' | sort -k 3 -t : | sed -e "s:\./::g"
}
find_files() {
	case ${SORT} in
		alphabet)
			find_by_name
			;;
		time)
			find_by_time
			;;
		*)
			find_unsorted
		;;
	esac
}

select_loop() {
	until [[ -n ${QUIT} ]]; do
		ANY_FILES=$(find_files)

		if [[ -z ${ANY_FILES} ]]; then
			echo "No log items to read"
			break
		fi

		echo
		echo "This is a list of portage log items. Choose a number to view that file or type q to quit."
		echo

		# Pick which file to read
		select FILE in ${ANY_FILES}; do
			case ${REPLY} in
				q)
					echo "Quitting"
					QUIT="yes"
					break
					;;
				a)
					SORT="alphabet"
					;;
				t)
					SORT="time"
					;;
				*)
					if [ -f "$FILE" ]; then
						${PAGER} ${FILE}
						read -p "Delete file? [y/N] " DELETE
						case ${DELETE} in
							q)
								echo "Quitting"
								QUIT="yes"
								break
								;;
							y|Y)
								rm -f ${FILE}
								SUCCESS=$?
								if [[ ${SUCCESS} = 0 ]]; then
									echo "Deleted ${FILE}"
								else
									echo "Unable to delete ${FILE}"
								fi
								;;
							# Empty string defaults to N (save file)
							n|N|"")
								echo "Saving ${FILE}"
								;;
							*)
								echo "Invalid response. Saving ${FILE}"
								;;
						esac
					else
						echo
						echo "Invalid response."
					fi
					;;
			esac
			break
		done
	done
}

pushd ${ELOGDIR} > /dev/null

select_loop

popd > /dev/null