summaryrefslogtreecommitdiff
blob: 67b7ff0d3c71a92bdb985d7a6e150c046dbb42a5 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash

# Deleting all locale files on system
# *not* listed in /etc/locale.nopurge

set -e

LOCALEDIRS="/usr/share/locale /usr/local/share/locale /usr/kde/?.?/share/locale /opt/sun-jdk-*/jre/lib/locale /opt/sun-jre-bin-*/lib/locale /usr/lib/locale /usr/share/binutils-data/*/*/locale /usr/share/gcc-data/*/*/locale"
MANPAGEDIRS="/usr/share/man /usr/man /usr/local/share/man /usr/kde/?.?/share/man /usr/kde/?.?/man /opt/sun-jdk-*/man /opt/sun-jre-bin-*/man"
LOCALELIST="/var/cache/localepurge/localelist"
CONFIGFILE="/etc/locale.nopurge"
VERSION="0.5.3.3"

LOCALETOTAL=0
MANTOTAL=0
SPACETMP=0

GOOD=$'\e[32;01m'
WARN=$'\e[33;01m'
BAD=$'\e[31;01m'
NORMAL=$'\e[0m'
HILITE=$'\e[36;01m'
BRACKET=$'\e[34;01m'
BOLD=$'\e[1m'

ACTION="/bin/rm -vf"

eerror () {
    echo -e " ${BAD}*${NORMAL} $*" 2> /dev/stderr
}

ewarn () {
    echo -e " ${WARN}*${NORMAL} $*"
}

einfo () {
    echo -e " ${GOOD}*${NORMAL} $*"
}
etab () {
    echo -e " ${GOOD}*${NORMAL} $*"
}

print() {
    for x in $@; do
        if [ "${x}" = "" ]; then return 0
	else /bin/echo $x
	fi
    done
}

ayuda () {
    echo "${HILITE}localepurge${NORMAL} for ${BOLD}Gentoo Linux${NORMAL} ${BRACKET}[${NORMAL} ${GOOD}${VERSION}${NORMAL} ${BRACKET}]${NORMAL}"
    echo -e " " "by bass@gentoo.org"
    echo
    einfo "Remember that you must edit ${CONFIGFILE}"
    echo
    einfo "Usage: `basename $0` [options]"
    echo
    etab "${BOLD}-debug (-d)    ${NORMAL}  : debug mode."
    etab "${BOLD}-help (-h)	${NORMAL}  : show this help."    
    etab "${BOLD}-list (-l)	${NORMAL}  : list directories to be cleared"
    etab "${BOLD}-nocolor (-nc) ${NORMAL}  : no color mode."
    etab "${BOLD}-pretend (-p)  ${NORMAL}  : pretend mode."
    etab "${BOLD}-verbose (-v)	${NORMAL}  : verbose mode."
    etab "${BOLD}-version	${NORMAL}  : show version."
    echo
}

version () {
    einfo "${BRACKET}[${NORMAL} ${GOOD}${VERSION}${NORMAL} ${BRACKET}]${NORMAL}"
}

# Check options in CONFIGFILE

grep -xq SHOWFREEDSPACE ${CONFIGFILE} && SHOWFREEDSPACE=enabled
grep -xq VERBOSE ${CONFIGFILE} && VERBOSE=enabled
grep -xq NOCOLOR ${CONFIGFILE} &&
        unset GOOD WARN BAD NORMAL HILITE BRACKET BOLD

# We need to loop through for no color first, otherwise options that exit 
# will make us ignore nocolor, so we might as well loop for debug first as well

for x in $@
do
    if [ "$x" = "-debug" ] || [ "$x" = "-d" ]; then
        set -x
    fi

    if [ "$x" = "-nocolor" ] || [ "$x" = "-nc" ]; then
        unset GOOD WARN BAD NORMAL HILITE BRACKET BOLD
    fi

done

# Loop through the rest of the options

for x in $@; do

    if [ "$x" = "-help" ] || [ "$x" = "-h" ]; then
        ayuda
	exit 0

    elif [ "$x" = "-list" ] || [ "$x" = "-l" ]; then
        LIST=enabled

    elif [ "$x" = "-pretend" ] || [ "$x" = "-p" ]; then
        PRETEND=enabled
	ACTION=print
	SHOWFREEDSPACE=disabled

    elif [ "$x" = "-verbose" ] || [ "$x" = "-v" ]; then
        VERBOSE=enabled

    elif [ "$x" = "-version" ] ; then
        version
	exit 0

    else
        eerror "Unrecognised option: ${x}"
	eerror "Please see `basename $0` -help"
	exit 1
    fi

    if [ "${LIST}" = "enabled" ] && [ "${PRETEND}" = "enabled" ]; then
        eerror "Cannot have list and pretend mode enabled at the same time"
	eerror "Please enable one at a time"
	echo >> /dev/stderr
	eerror "For more information, see `basename $0` -help"
    fi

done

# Do nothing and report why if no valid configuration file exists:

if [ ! -f ${CONFIGFILE} ]; then
    eerror " No ${CONFIGFILE} file present, exiting ..."
    exit 0
else
    if [ "`grep -x ^NEEDSCONFIGFIRST ${CONFIGFILE}`" ]; then
        echo ""
        einfo "    You have to configure \"${CONFIGFILE}\" "
        echo ""
        einfo "    to make $0 actually start to function."
        echo ""
        einfo "    Nothing to be done, exiting ..."
        echo ""
        exit 0
    fi
fi

# Prune the locale list using CONFIGFILE as a list of regex patterns:

PURGELIST=`grep -xvf ${CONFIGFILE} "${LOCALELIST}"`


# Deleting a filepath and optionally counting freed space

remove() {
    local REMOVEPATH="$1"

    if [ "$SHOWFREEDSPACE" = "enabled" ]; then
        SPACEBEFORE=$(df -P ${REMOVEPATH}| awk '{if ( NR==2 ) { print $3 }}')
    fi

    ${ACTION} `find ${REMOVEPATH} -type f -o -type l`

    if [ "$SHOWFREEDSPACE" = "enabled" ]; then
        SPACEAFTER=$(df -P ${REMOVEPATH} | awk '{if ( NR==2 ) { print $3 }}')
        SPACESUM=$(($SPACEBEFORE - $SPACEAFTER))

        if test $SPACESUM -gt 0 ; then
            SPACETMP=$(($SPACETMP + $SPACESUM))
        fi
    fi
}

# Getting rid of superfluous locale files in $LOCALEDIR:

purgelocale () {
    local LOCALEDIR="$1"
    if [ -d $LOCALEDIR ]; then

        if [ "$VERBOSE" = "enabled" ]; then
            einfo "localepurge: processing locale files in ${LOCALEDIR} ..."
        fi

        for LOCALE in `/bin/ls ${LOCALEDIR}`; do
            if echo "${PURGELIST}" | grep -xq ${LOCALE}; then
                if [ -d ${LOCALEDIR}/${LOCALE}/LC_MESSAGES ]; then
                    if [ "${LIST}" = "enabled" ]; then 
                        echo "${LOCALEDIR}/${LOCALE}"
                    else
                        remove "${LOCALEDIR}/${LOCALE}"
                    fi
                fi
            fi
        done

        if [ "$SHOWFREEDSPACE" = "enabled" ]; then
           if test $SPACETMP -gt 0 ; then
               LOCALETOTAL=$SPACETMP
               einfo "localepurge: Disk space freed in $LOCALEDIR: ${BOLD}"$SPACETMP"K${NORMAL}"
           fi
           SPACETMP=0
        fi
    fi
}

# Getting rid of localized man pages in $MANPAGEDIR

purgeman () {
    local MANPAGEDIR="$1"
    if [ -d $MANPAGEDIR ] && [ "`grep -x ^MANDELETE ${CONFIGFILE}`" ]; then

        if [ "$VERBOSE" = "enabled" ]; then
            einfo "localepurge: processing man pages in ${MANPAGEDIR} ..."
        fi

        for LOCALE in `/bin/ls ${MANPAGEDIR} | grep -v ^man[1-9]`; do
            if echo "${PURGELIST}" | grep -xq ${LOCALE}; then
                if [ -d ${MANPAGEDIR}/${LOCALE} ]; then
                    if [ "${LIST}" = "enabled" ]; then
                        echo "${MANPAGEDIR}/${LOCALE}"
                    else
                        remove "${MANPAGEDIR}/${LOCALE}"
                    fi
                fi
            fi
        done

        if [ "$SHOWFREEDSPACE" = "enabled" ]; then
           if test $SPACETMP -gt 0 ; then
               MANTOTAL=$SPACETMP
               einfo "localepurge: Disk space freed in $MANPAGEDIR: ${BOLD}"$SPACETMP"K${NORMAL}"
           fi
           SPACETMP=0
        fi
    fi
}

[ "${LIST}" = "enabled" ] && [ "${VERBOSE}" = "enabled" ] &&
    ewarn "If not in list mode, localepurge would clear the following directories:\n"

[ "${PRETEND}" = "enabled" ] && [ "${VERBOSE}" = "enabled" ] &&
    ewarn "If not pretending, localepurge would delete the following files:\n"

for folder in ${LOCALEDIRS}; do
    purgelocale "$folder"
done

for folder in ${MANPAGEDIRS}; do
    purgeman "$folder"
done

# Calculating and reporting total disk space freed:
if [ "$SHOWFREEDSPACE" = "enabled" ]; then
    let TOTAL=$LOCALETOTAL+$MANTOTAL
    if test $TOTAL -lt 0; then
        TOTAL=0
    fi

    echo ""
    einfo "Total disk space freed by localepurge: ${BOLD}"$TOTAL"K${NORMAL}"
    echo ""
fi