]> mj.ucw.cz Git - pciutils.git/blob - update-pciids.sh
update-pciids: Re-compress pci.ids if needed
[pciutils.git] / update-pciids.sh
1 #!/bin/sh
2
3 set -e
4
5 SRC="https://pci-ids.ucw.cz/v2.2/pci.ids"
6 DEST=pci.ids
7 PCI_COMPRESSED_IDS=
8 GREP=grep
9
10 [ "$1" = "-q" ] && quiet=true || quiet=false
11
12 # if pci.ids is read-only (because the filesystem is read-only),
13 # then just skip this whole process.
14 if ! touch ${DEST} >/dev/null 2>&1 ; then
15         ${quiet} || echo "${DEST} is read-only, exiting." 1>&2
16         exit 1
17 fi
18
19 if command -v xz >/dev/null 2>&1 ; then
20         DECOMP="xz -d"
21         SRC="$SRC.xz"
22 elif command -v bzip2 >/dev/null 2>&1 ; then
23         DECOMP="bzip2 -d"
24         SRC="$SRC.bz2"
25 elif command -v gzip >/dev/null 2>&1 ; then
26         DECOMP="gzip -d"
27         SRC="$SRC.gz"
28 else
29         DECOMP="cat"
30 fi
31
32 if command -v curl >/dev/null 2>&1 ; then
33         DL="curl -o $DEST.new $SRC"
34         ${quiet} && DL="$DL -s -S"
35 elif command -v wget >/dev/null 2>&1 ; then
36         DL="wget --no-timestamping -O $DEST.new $SRC"
37         ${quiet} && DL="$DL -q"
38 elif command -v lynx >/dev/null 2>&1 ; then
39         DL="eval lynx -source $SRC >$DEST.new"
40 else
41         echo >&2 "update-pciids: cannot find curl, wget or lynx"
42         exit 1
43 fi
44
45 if ! $DL ; then
46         echo >&2 "update-pciids: download failed"
47         rm -f $DEST.new
48         exit 1
49 fi
50
51 if ! $DECOMP <$DEST.new >$DEST.new.plain ; then
52         echo >&2 "update-pciids: decompression failed, probably truncated file"
53         exit 1
54 fi
55
56 if ! $GREP >/dev/null "^C " $DEST.new.plain ; then
57         echo >&2 "update-pciids: missing class info, probably truncated file"
58         exit 1
59 fi
60
61 if [ -f $DEST ] ; then
62         ln -f $DEST $DEST.old
63         # --reference is supported only by chmod from GNU file, so let's ignore any errors
64         chmod -f --reference=$DEST.old $DEST.new $DEST.new.plain 2>/dev/null || true
65 fi
66
67 if [ "$PCI_COMPRESSED_IDS" = 1 ] ; then
68         if [ "${SRC%.gz}" != .gz ] ; then
69                 # Recompress to gzip
70                 gzip <$DEST.new.plain >$DEST.new
71         fi
72         mv $DEST.new $DEST
73         rm -f $DEST.new.plain
74 else
75         mv $DEST.new.plain $DEST
76         rm -f $DEST.new
77 fi
78
79 # Older versions did not compress the ids file, so let's make sure we
80 # clean that up.
81 if [ ${DEST%.gz} != ${DEST} ] ; then
82         rm -f ${DEST%.gz} ${DEST%.gz}.old
83 fi
84
85 ${quiet} || echo "Done."