#!/bin/sh
PATH=/usr/bin:/usr/sbin:/bin:/sbin export PATH
crt_stat="`lpm-key -L -v 2>/dev/null|grep ^Status |cut -c15-`"
if [ "$crt_stat" = "VALID" ]
then
	echo "ok, status is: $crt_stat, no update needed" >&2
	exit
else
	echo "ok, status is: $crt_stat, will need new crt" >&2
fi
. /etc/lsb-release
if [ "x$DISTRIB_RELEASE" = "x" ]
then
	echo "error: this is not an LGL system"
	exit
fi

crt_path="https://lpk.laconian-linux.org/lgl-$DISTRIB_RELEASE/lpk-gen.crt"
crt_temp="/tmp/lpk-gen.crt"

if [ -f "/tmp/lpk-gen.crt" ]
then
	\rm -f /tmp/lpk-gen.crt
fi
curl -s -L -o /tmp/lpk-gen.crt $crt_path
rc=$?
if [ "$rc" != "0" ]
then
	echo "error: failure getting crt, please try again later" >&2
	exit
fi
echo "ok, replacing crt" >&2
sudo lpm-key -f -D 446bf5f4
sudo lpm-key -f -A /tmp/lpk-gen.crt
crt_stat="`lpm-key -L -v 2>/dev/null|grep ^Status |cut -c15-`"
if [ "$crt_stat" = "VALID" ]
then
	echo "ok, status is now: $crt_stat, finished" >&2
else
	echo "error: status is now: $crt_stat, please try again later" >&2
fi
rm -r /tmp/lpk-gen.crt
exit
