#!/usr/bin/env bash

cargo install -f svd2rust &&
cargo install -f rustfmt &&

wget \
	-O LPC82x.svd \
	http://ds.arm.com/media/resources/db/chip/nxp/lpc824m201jdh20/LPC82x.svd &&

# The SVD file has many cases of duplicate names. This leads to compiler errors
# in the generated code and therefore needs to be fixed.
patch \
	--silent \
	--force \
	--reject-file=- \
	LPC82x.svd LPC82x.svd.duplicate-names.patch || true &&

# Some names in the SVD file have multiple underscores, which leads to names in
# the generated code that are not valid snake case names. This causes build
# errors, as code generated by svd2rust treats warnings as errors.
patch \
	--silent \
	--force \
	--reject-file=- \
	LPC82x.svd LPC82x.svd.snake-case-warnings.patch || true &&

# One of the registers has a `<size>` field, which is specified as the register
# width in bits, but the value is the width in bytes. This causes svd2rust to
# insert incorrect padding.
patch \
	--silent \
	--force \
	--reject-file=- \
	LPC82x.svd LPC82x.svd.size-in-bytes.patch || true &&

mkdir -p src &&
svd2rust -i LPC82x.svd | rustfmt > src/lib.rs
