--- /dev/null
+# Setup a reprepro repository in /root/apt and add it to
+# /etc/apt/sources.list.
+
+# Copyright (C) 2013 Simon Ruderich
+#
+# 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 3 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, see <http://www.gnu.org/licenses/>.
+
+set -e
+
+
+sources=/etc/apt/sources.list
+repo=/root/apt
+
+# Don't look for exact "file://$repo" match to allow modifications on the
+# system.
+if grep -E '^deb file:///' "$sources" >/dev/null 2>&1; then
+ echo "file:/// already present in '$sources'!" >&2
+ exit 1
+fi
+
+# Create signing key for the repository if necessary.
+email="apt@`hostname`"
+if ! gpg --list-keys "$email" >/dev/null 2>&1; then
+ # See doc/DETAILS in the gpg source tree for documentation.
+ gpg --gen-key --batch <<EOF
+%echo generating signing key $email for APT, this may take a while ...
+Key-Type: RSA
+Key-Length: 4096
+# "cert" is automatically used for primary key.
+Key-Usage: sign
+Subkey-Type: RSA
+Subkey-Length: 4096
+Subkey-Usage: encrypt
+Name-Real: Apt signing key
+Name-Email: $email
+Preferences: SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 BZIP2 ZLIB ZIP Uncompressed
+%commit
+EOF
+fi
+
+keyid=`gpg --list-keys --with-colons --fixed-list-mode "$email" \
+ | grep -E '^pub:' \
+ | tail -n1 \
+ | awk -F: '{ print $5 }'`
+# Copied from simon-config and modified (uses "$sources"), keep in sync.
+codename=`lsb_release -s -c 2>/dev/null \
+ || grep -E '^deb' "$sources" | head -n1 | awk '{ print $3 }'`
+
+# Create reprepro repository.
+if test ! -d "$repo"; then
+ echo "creating reprepro repository in '$repo'" >&2
+ mkdir -p "$repo/conf"
+ mkdir -p "$repo/morgue"
+
+ arch=`dpkg --print-architecture`
+ cat >"$repo/conf/distributions" <<EOF
+Codename: $codename
+Components: main
+Architectures: $arch source
+SignWith: $keyid
+EOF
+ cat >"$repo/conf/options" <<EOF
+morguedir ./morgue
+EOF
+
+ # Initialize repository and check configuration files. Ignore errors if
+ # reprepro is not installed, it's not necessary for a correct
+ # installation.
+ echo 'Initializing repository, errors are ignored.' >&2
+ ( cd "$repo" && reprepro check ) || true
+
+ # Does nothing if the same key is imported multiple times.
+ echo 'Adding key with apt-key.' >&2
+ gpg --export "$keyid" | apt-key add -
+fi
+
+echo "deb file://$repo $codename main" >>"$sources"
+
+echo 'Finished successfully'. >&2