]> mj.ucw.cz Git - pciutils.git/blob - update-pciids.sh
32633394a28bebdde61aa7338638db6a444b42e9
[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 [ "$PCI_COMPRESSED_IDS" = 1 ] ; then
20         DECOMP="cat"
21         SRC="$SRC.gz"
22         GREP=zgrep
23 elif command -v xz >/dev/null 2>&1 ; then
24         DECOMP="xz -d"
25         SRC="$SRC.xz"
26 elif command -v bzip2 >/dev/null 2>&1 ; then
27         DECOMP="bzip2 -d"
28         SRC="$SRC.bz2"
29 elif command -v gzip >/dev/null 2>&1 ; then
30         DECOMP="gzip -d"
31         SRC="$SRC.gz"
32 else
33         DECOMP="cat"
34 fi
35
36 if command -v curl >/dev/null 2>&1 ; then
37         DL="curl -o $DEST.new $SRC"
38         ${quiet} && DL="$DL -s -S"
39 elif command -v wget >/dev/null 2>&1 ; then
40         DL="wget --no-timestamping -O $DEST.new $SRC"
41         ${quiet} && DL="$DL -q"
42 elif command -v lynx >/dev/null 2>&1 ; then
43         DL="eval lynx -source $SRC >$DEST.new"
44 else
45         echo >&2 "update-pciids: cannot find curl, wget or lynx"
46         exit 1
47 fi
48
49 if ! $DL ; then
50         echo >&2 "update-pciids: download failed"
51         rm -f $DEST.new
52         exit 1
53 fi
54
55 if ! $DECOMP <$DEST.new >$DEST.neww ; then
56         echo >&2 "update-pciids: decompression failed, probably truncated file"
57         exit 1
58 fi
59
60 if ! $GREP >/dev/null "^C " $DEST.neww ; then
61         echo >&2 "update-pciids: missing class info, probably truncated file"
62         exit 1
63 fi
64
65 if [ -f $DEST ] ; then
66         ln -f $DEST $DEST.old
67         # --reference is supported only by chmod from GNU file, so let's ignore any errors
68         chmod -f --reference=$DEST.old $DEST.neww 2>/dev/null || true
69 fi
70 mv $DEST.neww $DEST
71 rm $DEST.new
72
73 # Older versions did not compress the ids file, so let's make sure we
74 # clean that up.
75 if [ ${DEST%.gz} != ${DEST} ] ; then
76         rm -f ${DEST%.gz} ${DEST%.gz}.old
77 fi
78
79 ${quiet} || echo "Done."