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