Imported Upstream version 4.14.1
[platform/upstream/rpm.git] / scripts / brp-compress
1 #!/bin/sh
2
3 # If using normal root, avoid changing anything.
4 if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
5         exit 0
6 fi
7
8 cd "$RPM_BUILD_ROOT"
9
10 # Compress man pages
11 COMPRESS=${COMPRESS:-gzip -9 -n}
12 COMPRESS_EXT=${COMPRESS_EXT:-.gz}
13
14 for d in ./usr/man/man* ./usr/man/*/man* ./usr/info \
15         ./usr/share/man/man* ./usr/share/man/*/man* ./usr/share/info \
16         ./usr/kerberos/man ./usr/X11R6/man/man* ./usr/lib/perl5/man/man* \
17         ./usr/share/doc/*/man/man* ./usr/lib/*/man/man* ./usr/share/fish/man/man*
18 do
19     [ -d $d ] || continue
20     for f in `find $d -type f ! -name dir`
21     do
22         [ -f "$f" ] || continue
23
24         case "$f" in
25          *.gz|*.Z)    gunzip  -f $f; b=`echo $f | sed -e 's/\.\(gz\|Z\)$//'`;;
26          *.bz2)       bunzip2 -f $f; b=`echo $f | sed -e 's/\.bz2$//'`;;
27          *.xz|*.lzma) unxz    -f $f; b=`echo $f | sed -e 's/\.\(xz\|lzma\)$//'`;;
28          *) b=$f;;
29         esac
30
31         $COMPRESS $b </dev/null 2>/dev/null || {
32             inode=`ls -i $b | awk '{ print $1 }'`
33             others=`find $d -type f -inum $inode`
34             if [ -n "$others" ]; then
35                 for afile in $others ; do
36                     [ "$afile" != "$b" ] && rm -f $afile
37                 done
38                 $COMPRESS -f $b
39                 for afile in $others ; do
40                     [ "$afile" != "$b" ] && ln $b$COMPRESS_EXT $afile$COMPRESS_EXT
41                 done
42             else
43                 $COMPRESS -f $b
44             fi
45         }
46     done
47
48     for f in `find $d -type l`
49     do
50         l=`ls -l $f | sed -e 's/.* -> //' -e 's/\.\(gz\|Z\|bz2\|xz\|lzma\)$//'`
51         rm -f $f
52         b=`echo $f | sed -e 's/\.\(gz\|Z\|bz2\|xz\|lzma\)$//'`
53         ln -sf $l$COMPRESS_EXT $b$COMPRESS_EXT
54     done
55 done