c6d4de13f5d11afbee0b7de8f71ae8aff246e5f4
[gentoolkit.git] / bin / eread
1 #!/bin/bash
2
3 # This is a script to read portage log items from einfo, ewarn etc, new in the
4 # portage-2.1 series.
5 #
6 # Author: Donnie Berkholz <spyderous@gentoo.org>
7 # Updated by: Uwe Klosa <uwe.klosa@gmail.com>
8
9 # set decent PATH for bug 172969
10
11 PATH=/usr/bin:/bin:${PATH}
12
13 # Set ELOGDIR
14 PORT_LOGDIR="$(portageq envvar PORT_LOGDIR)"
15 [ "$PORT_LOGDIR" = "" ] && PORT_LOGDIR="/var/log/portage"
16 ELOGDIR="$PORT_LOGDIR/elog"
17
18 # Verify that ELOGDIR exists
19 if [ ! -d "$ELOGDIR" ]; then
20         echo "ELOG directory: $ELOGDIR does not exist!"
21         exit 1
22 fi
23
24 # Use the pager from the users environment
25 [ -z "$PAGER" ] && PAGER="less"
26
27 # Set up select prompt
28 PS3="Choice? "
29
30 select_loop() {
31         ANY_FILES=$(find . -type f)
32         ANY_FILES=$(echo ${ANY_FILES} | sed -e "s:\./::g")
33
34         if [[ -z ${ANY_FILES} ]]; then
35                 echo "No log items to read"
36                 break
37         fi
38
39         echo
40         echo "This is a list of portage log items. Choose a number to view that file or type q to quit."
41         echo
42
43         # Pick which file to read
44         select FILE in ${ANY_FILES}; do
45                 case ${REPLY} in
46                         q)
47                                 echo "Quitting"
48                                 QUIT="yes"
49                                 break
50                                 ;;
51                         *)
52                                 if [ -f "$FILE" ]; then
53                                         ${PAGER} ${FILE}
54                                         read -p "Delete file? [y/N] " DELETE
55                                         case ${DELETE} in
56                                                 q)
57                                                         echo "Quitting"
58                                                         QUIT="yes"
59                                                         break
60                                                         ;;
61                                                 y|Y)
62                                                         rm -f ${FILE}
63                                                         SUCCESS=$?
64                                                         if [[ ${SUCCESS} = 0 ]]; then
65                                                                 echo "Deleted ${FILE}"
66                                                         else
67                                                                 echo "Unable to delete ${FILE}"
68                                                         fi
69                                                         ;;
70                                                 # Empty string defaults to N (save file)
71                                                 n|N|"")
72                                                         echo "Saving ${FILE}"
73                                                         ;;
74                                                 *)
75                                                         echo "Invalid response. Saving ${FILE}"
76                                                         ;;
77                                         esac
78                                 else
79                                         echo
80                                         echo "Invalid response."
81                                 fi
82                                 ;;
83                 esac
84                 break
85         done
86 }
87
88 pushd ${ELOGDIR} > /dev/null
89
90 until [[ -n ${QUIT} ]]; do
91         select_loop
92 done
93
94 popd > /dev/null