#!/bin/bash

set -eu

if [ "$(whoami)" != "root" ]; then
	echo "Must be root to run this script." >&2
	exit 77
fi

if [ "$#" -ne 1 ]; then
	echo "Usage: $0 <license_file>" >&2
	exit 1
fi

fn=$1

if [ ! -f "$fn" ]; then
	echo "Not a valid file: $fn" >&2
	exit 1
fi

copy_to() {
    cp "$fn" "$1"
    chmod 640 "$1"
    chown root:radarsw "$1"
}

# two copies of the license file; One installed license, and another just for backup
fileTimestamp=$(date -u '+%Y-%m-%dT%H-%M-%S')
copy_to "/etc/vaisala/radarsw/configuration/license.txt"
copy_to "/etc/vaisala/radarsw/configuration/license-$fileTimestamp.txt"

echo "License file installed!"
