summarylogtreecommitdiffstats
path: root/pacman-diffs
blob: 7ad4ef51073c8c4d041443541e346a2ba3d89bf2 (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
#!/usr/bin/env bash
# finding .pacnew files and compare
# v1.1.0


AGE=360 # ne rechercher que ceux qui ont moins de 360 jours
EXT='pacnew'
ARCCHIVEDIR='/var/cache/pacman/pacnew'

c_red='\033[0m\033[31m'
c_green='\033[0m\033[32m'
c_yellow='\033[0m\033[33m'
c_end='\033[0m'
bold='\033[1m'

## default string constants {{{
    declare -A txt=(
        ['label']='differences of'
        ['error']='Error'
        ['notfound']='file not found'
        ['root']='This script must be run as root'
        ['files']='files'
        ['noarchive']='not in archives'
        ['erase']='You can erase this file'
        ['archiver']='archive in'
    )
lg=$(locale 2>/dev/null | awk -F'=' '/^LANG/ {print $2}')
if [ 'fr' == "${lg:0:2}" ]; then
    declare -A txt=(
        ['label']='les différences de'
        ['error']='Erreur'
        ['notfound']="fichier n'existe pas"
        ['root']='Vous devez avoir des droits administrateur'
        ['files']='fichiers'
        ['noarchive']='non archivés'
        ['erase']='Vous pouvez supprimer ce fichier'
        ['archiver']='archivé dans'
    )
fi
#}}}

function error()
{
    echo -e "\033[41m${txt[error]}:\033[0m $1" >&2
}
function test_sudo() {
    if [[ $EUID -ne 0 ]]; then
        error "${txt[root]}"
        exit 1
    fi
}

function get_su(){
    local mysu=''
    if [ -f '/usr/bin/kdesu' ]; then
        mysu='kdesu'
    else
        [ -f '/usr/bin/gksu' ] && mysu='gksu'
    fi
    echo -n "$mysu"
}

function get_editor(){
    local ed=''
    if [ -f '/usr/bin/diffuse' ]; then
        ed='/usr/bin/diffuse'
    else
        [ -f '/usr/bin/kompare' ] && ed='/usr/bin/kompare'
    fi
    echo "$ed"
}

function archive()
{
    test_sudo
    local file="$1"
    local name="$2"
    file=$ARCCHIVEDIR${file/$name.$EXT/$(stat -c "%Y" "$1")-$name.$EXT}
    install -D "$1" "$file"
    echo "-- ${txt[archiver]} $file"
    file=$(dirname $file)
    # want only 2 last files
    nb=$(ls $file/*$name.$EXT | wc -l)
    if [ "$nb" -gt 2 ]; then
        ls -v $file/*$name.$EXT | head -$(($nb-2)) | xargs rm
    fi
}
function in_archive(){
    local file="$1"
    local name="$2"
    declare -a out
    file=$ARCCHIVEDIR${file/$name.$EXT/*-$name.$EXT}
    nb=$(ls $file | wc -l)
    if [ "$nb" -gt "1" ]; then
        echo $(ls -v $file)
    else
        return 1
    fi
}

function make_diff()
{
    local file1="$1"
    local file2="$2"
    local original="$3"
    local nothing=1
    IFS=$'\n'
    # < : removed       > : added
    diff $file1 $file2| grep -E "^>|^<" | while read line ; do
        if [ "${line::1}" = '<' ]; then c="$c_red-"; else c="$c_green+"; fi
        echo -e "$c ${line:2}$c_end"
        nothing=0
    done
    if [[ $nothing -eq 1 ]]; then
        # can remove etc/pacnew
        echo -e "-- ${c_yellow}${txt[erase]}${c_end} : sudo rm $file"
    fi
}

function usage()
{
    echo "usage: pacman-diffs [options]"
    echo -e "\t -x extension (pacnew, pacsave...)"
    echo -e "\t -d since x days"
    echo -e "\t -l : list only - no diff"
    echo -e "\t -e filename : edit files"
    echo -e "\t -a archive pacnew in /var/cache/pacman/pacnew/"
    echo -e "\t -c compare archives"
    exit
}



while getopts "hd:x:le:ac" flags; do
	case $flags in
		h) usage;exit 0 ;;
		d) AGE="${OPTARG}" ;;
		x) EXT="${OPTARG}" ;;
		l) listing=1 ;;
		e) edit="${OPTARG}"; listing=1 ;;	
		a) archive=1; listing=1 ;;
		c) archive=1; archivediff=1 ;;
		:) error "Vous avez oublié un argument"; exit 1 ;;
		?) error "invalid Option"; exit 1 ;;
		*) error "*"; exit 1 ;;
	esac
done

find '/etc' -name "*.$EXT" -mtime -$AGE -type f 2> /dev/null | while read file ; do

    fname=$(basename "$file")
    fname="${fname%.$EXT}"
    ofile="$(dirname ${file})/${fname}"
    dfile=$(stat -c "%y" ${file} | awk '{print $1}')
    echo -e "\n$bold${fname}$c_end\n-- ${txt[label]} $file\t(${dfile})"
   
    if [[ -f "$ofile" ]]; then
    
        if [[ $archive -eq 1 ]]; then
            # cp in  /var/cache/pacman/pacnew/
            archive ${file} ${fname}
        fi
        
 
        if [[ $listing -ne 1 ]]; then
        
            if [[ $archivediff -eq 1 ]]; then
                files=$(in_archive ${file} ${fname})
                if [ "$?" -ne "1" ]; then
                        make_diff ${files} ${file}
                        continue
                else
                    echo "2 ${txt[files]} \"${fname}\" ${txt[noarchive]}"
                    continue
                fi                
            fi
            
            make_diff ${ofile} ${file} ${file}
        fi
    
        if [ "$edit" == "$fname" ]; then
            cmd="$(get_su) $(get_editor) $ofile $file &"
            $cmd
        fi
    
    else
        echo -e "\t$bold ${txt[error]}:$c_end ${txt[notfound]} ? $ofile"
    fi
    
done

exit 0