#!/bin/bash
#
# SPDX-License-Identifier: GPL-2.0-only
# Copyright (c) 2020, AT&T Intellectual Property.
#
# Copy /etc/machine-id between installs
#

set -e

: ${vyatta_prefix:=/opt/vyatta}
: ${vyatta_exec_prefix:=$vyatta_prefix}
: ${vyatta_sbindir:=${vyatta_exec_prefix}/sbin}

source "${vyatta_sbindir}/vyatta-install-image.functions"

function copy_machine_id ()
{
    local root_dir=${1?Missing argument}
    local image_name=${2?Missing argument}

    # Don't copy machine-id generated by the booting live-cd,
    # so that the installed system can generate it's own.
    # WARNING: Any RW state that's keyed on machine-id had better not be copied.
    if is_live_cd_boot; then
        lecho "Not copying machine-id as live-cd"
        return 0
    fi

    if [ -e /etc/machine-id ]; then
        run_command cp -p -f /etc/machine-id "${root_dir}/etc"
        run_command sync
        becho "Copied machine-id: $(cat /etc/machine-id)"
    fi
}

case "$1" in
    configure)
	;;
    run)
	copy_machine_id $2 $3
	;;
    *)
	fail_exit "$0: unknown command: \"$1\""
	;;
esac

exit 0
