#!/bin/bash

. ${TOOLKIT}-functions.sh


add_repo()
{
	local reponame=$1
	local desc=${2:-$reponame}
	local srcmatch="^[ ,	]*deb-src[ ,	]"
	local add_sources_here=false

	sourcefile="sources.list.$reponame"
	path=$(filepath $sourcefile) || exit_err 1 "$sourcefile missing"

	$add_sources && {
		add_sources_here=true
		grep -q "$srcmatch" $path || add_sources_here=false
	}

	if $add_sources_here
	then
		echo "  adding repos: $desc (with sources)"
		cat $path >>$outfile
	else
		echo "  adding repos: $desc"
		grep -v "$srcmatch" $path >>$outfile
	fi

	echo >>$outfile
	return 0
}


#----------------------------------------------------------------
# builds sources.list
#----------------------------------------------------------------

case $DISTRIB_ID in
  Devuan)	;;		# ok to continue
  *)		exit 0 ;; 	# DO NOTHING
esac

outfile=sources.tmp

cp sources.list.header $outfile || exit $?

getconfirm install.add_sources		&& add_sources=true || add_sources=false


# first, despite the backup made by kusa-reconf itself, we
# save original sources.list in the same apt dir
#
file="/etc/apt/sources.list"
[ -f $file.orig ] || {
	echo "  saving actual source list into $file.orig"
	cp -a $file $file.orig || exit $?
}

# standard repos
#
repos=$(jtconf install.mirror_opt_repos)

for repo in base $repos
do
	add_repo "$repo" "$DISTRIB_FULL_ID $DISTRIB_CODENAME ($repo) mirrors"
done


exit 0
