#!/bin/sh

set -eu
source_list=/etc/apt/sources.list
suite=${VERSION_CODENAME:-}

if [ -z "$suite" ] && [ -r /etc/os-release ]; then
    . /etc/os-release
    suite=${VERSION_CODENAME:-}
fi

if [ -z "$suite" ] && [ -d /run/live/medium/dists ]; then
    set -- /run/live/medium/dists/*
    if [ -e "$1" ]; then
        suite=${1##*/}
    fi
fi

if [ -n "$suite" ] && [ -d "/run/live/medium/dists/$suite" ]; then
    entry="deb [trusted=yes] file:/run/live/medium $suite main restricted extras restricted-firmware"
    if ! grep -Fxq "$entry" "$source_list"; then
        sed -i "1i $entry" "$source_list"
    fi
fi
