#!/bin/sh
#
#    Copyright (C) 2009-2017  Ruben Rodriguez <ruben@trisquel.info>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

VERSION=1
NETINST=true
. ./config

cat << EOF > debian/rules
#! /usr/bin/make -f
%:
	dh \$@
EOF

rm debian/apt-mirror-setup.templates-ubuntu

# Remove multiarch support
sed '/chroot/,$d' generators/01setup -i

# getline "seed" file
getline(){
grep -n "$1" "$2" |cut -d: -f1
}

START=$(getline apt-setup/non-free debian/apt-mirror-setup.templates)
END=$(getline "Please choose whether you want to have it available anyway." debian/apt-mirror-setup.templates)
sed ${START},${END}d debian/apt-mirror-setup.templates -i
START=$(getline apt-setup/contrib debian/apt-mirror-setup.templates)
END=$(getline "Please choose whether you want this software to be made available to you." debian/apt-mirror-setup.templates)
sed ${START},${END}d debian/apt-mirror-setup.templates -i

/bin/sed '/canonical/d;
/Canonical/d;
s/main restricted universe multiverse/main/g;
s/dists restricted/dists/g;
s/archive.ubuntu.com/archive.trisquel.info/g;
s/security.ubuntu.com/archive.trisquel.info/g;
s/ports.ubuntu.com/archive.trisquel.info/g;
s/Ubuntu/Trisquel/g;
s/ubuntu/trisquel/g;
s/universe/main/g;
s/main main/main/g;
s:help.trisquel.com/community/UpgradeNotes:trisquel.info/wiki/:g;' generators/* -i

replace "Default: /ubuntu" "Default: /trisquel" .
replace "security.ubuntu.com" "archive.trisquel.info" .
replace "archive.ubuntu.com" "archive.trisquel.info" .
replace "Ubuntu" "Trisquel" .

cd release-files
rm -rf *
for DIST in $CODENAME $CODENAME-security $CODENAME-updates $CODENAME-backports
do
    wget -r -l1 -np http://archive.trisquel.info/trisquel/dists/$DIST
done
find archive.trisquel.info/ |egrep '(index.html|robots.txt)'|xargs rm
cd ..

rm generators/9[1-9]* generators/50mirror generators/50mirror.ubuntu
sed -i '/generators\/9[1-9]*/d' debian/apt-setup-udeb.install

echo "generators/50mirror.trisquel usr/lib/apt-setup/generators
release-files/archive.trisquel.info usr/share/apt-setup/release-files" > debian/apt-mirror-setup.install

echo '#!/bin/sh
set -e

. /usr/share/debconf/confmodule

file="$1"

log() {
	logger -t apt-setup "$@"
}
warning() {
	log "warning: $@"
}

# Ask if a mirror should be used if the base system can be installed from CD
if [ -e /cdrom/.disk/base_installable ] || [ "$OVERRIDE_BASE_INSTALLABLE" ]; then
	if ! search-path choose-mirror; then
		warning "choose-mirror is not available; cannot offer network mirror"
		exit 1
	fi

	# Default to false if no network selected in netcfg
	if db_get netcfg/dhcp_options && \
	   [ "$RET" = "Do not configure the network at this time" ]; then
		use_mirror=false
	fi

	# Set default if no value (see Debian mirror generator)
	db_get apt-setup/use_mirror
	[ "$RET" ] || db_set apt-setup/use_mirror true 

	# Text is variable for Debian
	db_metaget apt-mirror/use/netinst_old description
	db_subst apt-setup/use_mirror EXPLANATION "$RET"

	db_input medium apt-setup/use_mirror || [ $? -eq 30 ]
	db_go # or exit 10

	db_get apt-setup/use_mirror
	if [ "$RET" = false ]; then
		exit 1
	fi

	if db_get cdrom/codename && [ "$RET" ]; then
		db_set mirror/codename $RET
	fi
	if db_get cdrom/suite && [ "$RET" ]; then
		db_set mirror/suite $RET
	fi
	choose-mirror -n # no progress bar
fi

db_input low apt-setup/backports || true

dists="main"

db_get mirror/protocol
protocol="$RET"
db_get mirror/codename
codename="$RET"
db_get mirror/$protocol/hostname
hostname="$RET"
db_get mirror/$protocol/directory
directory="/${RET#/}"

if [ "$protocol" = http ]; then
	db_get mirror/$protocol/proxy
	proxy="$RET"
	if [ -n "$proxy" ]; then
		if ! grep -iq "Acquire::$protocol::Proxy" $ROOT/etc/apt/apt.conf.new; then
			echo "Acquire::$protocol::Proxy \"$proxy\";" >> $ROOT/etc/apt/apt.conf.new
		fi
	fi
fi


cat > $file <<EOF
# See http://trisquel.info/wiki/ for how to upgrade to
# newer versions of the distribution.

deb $protocol://$hostname$directory $codename $dists
deb-src $protocol://$hostname$directory $codename $dists

deb $protocol://$hostname$directory $codename-updates $dists
deb-src $protocol://$hostname$directory $codename-updates $dists

deb $protocol://$hostname$directory $codename-security $dists
deb-src $protocol://$hostname$directory $codename-security $dists
EOF

# Even if the backports repository is not enabled, we write example lines for
# it.
echo >> $file
if db_get apt-setup/backports && [ "$RET" = true ]; then
	COMMENT=
else
	cat >> $file <<EOF
EOF
	COMMENT="# "
fi
cat >> $file <<EOF
# Uncomment this lines to enable the backports optional repository
${COMMENT}deb $protocol://$hostname$directory $codename-backports main
${COMMENT}deb-src $protocol://$hostname$directory $codename-backports main
EOF

apt-setup-signed-release archive.trisquel.info "$file"

exit 0
' > generators/50mirror.trisquel

sed '/The partner/,/^*/d; s/services-select-ubuntu/services-select-trisquel/' -i debian/apt-setup-udeb.templates

chmod 755 generators/50mirror.trisquel

changelog "Rebranded and adapted for Trisquel"
compile
