xref: /pciutils/update-pciids.sh (revision 7ec58f1c)
11b95f396SMartin Mares#!/bin/sh
21b95f396SMartin Mares
31b95f396SMartin Maresset -e
4bae18059SGuillem Jover
5e2d84e91SMartin MaresSRC="https://pci-ids.ucw.cz/v2.2/pci.ids"
61b95f396SMartin MaresDEST=pci.ids
7cc062b4aSMartin MaresPCI_COMPRESSED_IDS=
8cc062b4aSMartin MaresGREP=grep
9*7ec58f1cSMartin MaresVERSION=unknown
10*7ec58f1cSMartin MaresUSER_AGENT=update-pciids/$VERSION
11*7ec58f1cSMartin MaresQUIET=
121b95f396SMartin Mares
1320e206ecSGuillem Jover[ "$1" = "-q" ] && quiet=true || quiet=false
1420e206ecSGuillem Jover
15d26b5378SMartin Mares# if pci.ids is read-only (because the filesystem is read-only),
16d26b5378SMartin Mares# then just skip this whole process.
17d26b5378SMartin Maresif ! touch ${DEST} >/dev/null 2>&1 ; then
18d26b5378SMartin Mares	${quiet} || echo "${DEST} is read-only, exiting." 1>&2
19d26b5378SMartin Mares	exit 1
20d26b5378SMartin Maresfi
21d26b5378SMartin Mares
22b6e558fbSMartin Maresif command -v xz >/dev/null 2>&1 ; then
23327b6e8aSMartin Mares	DECOMP="xz -d"
24327b6e8aSMartin Mares	SRC="$SRC.xz"
2585f05a7cSMartin Mareselif command -v bzip2 >/dev/null 2>&1 ; then
261b95f396SMartin Mares	DECOMP="bzip2 -d"
271b95f396SMartin Mares	SRC="$SRC.bz2"
2885f05a7cSMartin Mareselif command -v gzip >/dev/null 2>&1 ; then
291b95f396SMartin Mares	DECOMP="gzip -d"
301b95f396SMartin Mares	SRC="$SRC.gz"
311b95f396SMartin Mareselse
321b95f396SMartin Mares	DECOMP="cat"
331b95f396SMartin Maresfi
341b95f396SMartin Mares
3585f05a7cSMartin Maresif command -v curl >/dev/null 2>&1 ; then
36*7ec58f1cSMartin Mares	${quiet} && QUIET="-s -S"
37*7ec58f1cSMartin Mares	dl ()
38*7ec58f1cSMartin Mares	{
39*7ec58f1cSMartin Mares		curl -o $DEST.new --user-agent "$USER_AGENT curl" $QUIET $SRC
40*7ec58f1cSMartin Mares	}
4185f05a7cSMartin Mareselif command -v wget >/dev/null 2>&1 ; then
42*7ec58f1cSMartin Mares	${quiet} && QUIET="-q"
43*7ec58f1cSMartin Mares	dl ()
44*7ec58f1cSMartin Mares	{
45*7ec58f1cSMartin Mares		wget --no-timestamping -O $DEST.new --user-agent "$USER_AGENT wget" $QUIET $SRC
46*7ec58f1cSMartin Mares	}
4785f05a7cSMartin Mareselif command -v lynx >/dev/null 2>&1 ; then
48*7ec58f1cSMartin Mares	dl ()
49*7ec58f1cSMartin Mares	{
50*7ec58f1cSMartin Mares		lynx -source -useragent="$USER_AGENT lynx" $SRC >$DEST.new
51*7ec58f1cSMartin Mares	}
521b95f396SMartin Mareselse
531bbdea17SMartin Mares	echo >&2 "update-pciids: cannot find curl, wget or lynx"
541b95f396SMartin Mares	exit 1
551b95f396SMartin Maresfi
561b95f396SMartin Mares
57*7ec58f1cSMartin Maresif ! dl ; then
581b95f396SMartin Mares	echo >&2 "update-pciids: download failed"
591b95f396SMartin Mares	rm -f $DEST.new
601b95f396SMartin Mares	exit 1
611b95f396SMartin Maresfi
621b95f396SMartin Mares
63b6e558fbSMartin Maresif ! $DECOMP <$DEST.new >$DEST.new.plain ; then
641b95f396SMartin Mares	echo >&2 "update-pciids: decompression failed, probably truncated file"
651b95f396SMartin Mares	exit 1
661b95f396SMartin Maresfi
671b95f396SMartin Mares
68b6e558fbSMartin Maresif ! $GREP >/dev/null "^C " $DEST.new.plain ; then
691b95f396SMartin Mares	echo >&2 "update-pciids: missing class info, probably truncated file"
701b95f396SMartin Mares	exit 1
711b95f396SMartin Maresfi
721b95f396SMartin Mares
731b95f396SMartin Maresif [ -f $DEST ] ; then
74b6a0091eSGuillem Jover	ln -f $DEST $DEST.old
7580b3121aSMartin Mares	# --reference is supported only by chmod from GNU file, so let's ignore any errors
76b6e558fbSMartin Mares	chmod -f --reference=$DEST.old $DEST.new $DEST.new.plain 2>/dev/null || true
771b95f396SMartin Maresfi
78b6e558fbSMartin Mares
79b6e558fbSMartin Maresif [ "$PCI_COMPRESSED_IDS" = 1 ] ; then
80b6e558fbSMartin Mares	if [ "${SRC%.gz}" != .gz ] ; then
81b6e558fbSMartin Mares		# Recompress to gzip
82b6e558fbSMartin Mares		gzip <$DEST.new.plain >$DEST.new
83b6e558fbSMartin Mares	fi
84b6e558fbSMartin Mares	mv $DEST.new $DEST
85b6e558fbSMartin Mares	rm -f $DEST.new.plain
86b6e558fbSMartin Mareselse
87b6e558fbSMartin Mares	mv $DEST.new.plain $DEST
88b6e558fbSMartin Mares	rm -f $DEST.new
89b6e558fbSMartin Maresfi
901b95f396SMartin Mares
91d26b5378SMartin Mares# Older versions did not compress the ids file, so let's make sure we
92d26b5378SMartin Mares# clean that up.
93d26b5378SMartin Maresif [ ${DEST%.gz} != ${DEST} ] ; then
94d26b5378SMartin Mares	rm -f ${DEST%.gz} ${DEST%.gz}.old
95d26b5378SMartin Maresfi
96d26b5378SMartin Mares
97d26b5378SMartin Mares${quiet} || echo "Done."
98