#!/bin/bash

# Simple way to create simple repository using apt-ftparchive.

echo "This script assumes that a directory (or subdirectories) containing .deb files have been made already."
echo "The directory or subdirectories may also contain other files besides .deb"
echo "Run this script from terminal opened in the directory containing .deb files (can also include subdirectories with .deb files)"
echo "Type 'y' if you want to create a signed key (if .gnupg folder does not exist yet)"
echo "Type 'n' if you have already created a signed key and continue"

read -p "Create signed key (y/n)?" choice
case "$choice" in 
  y|Y ) gpg --gen-key --homedir $PWD/.gnupg
cp -a $PWD/.gnupg/pubring.gpg $PWD/debdog.gpg
[ "$?" -eq 0 ] && echo -e "\e[0;32mSigned key is created!\033[0m"
echo
echo "Creating the required files e.g. Packages.gz by apt-ftparchive..."
cd "$PWD"
# Insert needed info first in Release file
echo 'Origin: Debian
Label: DebianDog
Suite: stable
Version: 1.0
Codename: jessie
Architectures: i386
Components: main
Description: DebianDog repository' > Release

apt-ftparchive packages . > Packages
gzip -c Packages > Packages.gz
apt-ftparchive contents . > Contents
gzip -c Contents > Contents.gz 
apt-ftparchive release . >> Release
echo "When updating choose 'y' to overwrite Release.gpg"
gpg -abs --homedir .gnupg -o Release.gpg Release
;;
  n|N ) cd "$PWD"
echo "Creating the required files e.g. Packages.gz by apt-ftparchive..."
# Insert needed info first in Release file
echo 'Origin: Debian
Label: DebianDog
Suite: stable
Version: 1.0
Codename: jessie
Architectures: i386
Components: main
Description: DebianDog repository' > Release

apt-ftparchive packages . > Packages
gzip -c Packages > Packages.gz
gzip -c Packages > Translation-en.gz 
apt-ftparchive contents . > Contents
gzip -c Contents > Contents.gz 
apt-ftparchive release . >> Release
echo
echo
echo -e "\e[0;31mWarning\033[0m: In case of updating the repository:"
echo "Release.gpg and InRelease will be overwritten"
echo
echo "You need a passphrase to unlock the secret key for:" 
gpg --no-permission-warning --homedir .gnupg --list-keys 
read -sp "Enter passphrase: " pass
echo
gpg --no-permission-warning --no-use-agent --no-tty -abs --homedir .gnupg --passphrase $pass --yes -o Release.gpg Release
if [ $? -ne 0 ]; then
read -sp "Try again, enter passphrase: " pass
echo
gpg --no-permission-warning --no-use-agent --no-tty -abs --homedir .gnupg --passphrase $pass --yes -o Release.gpg Release
fi
if [ $? -ne 0 ]; then
echo "Wrong passphrase, exiting..."
sleep 3
exit
fi
[ $? -eq 0 ] && echo -e "\e[0;32mRelease.gpg created!\033[0m"
gpg --no-permission-warning --no-use-agent --no-tty --homedir .gnupg --clearsign --passphrase $pass --yes -o InRelease Release
[ $? -eq 0 ] && echo -e "\e[0;32mInRelease created!\033[0m"
;;
  * ) echo "invalid"
;;
esac

./cr-index

for dir in $(ls)
do
    if [ -d $dir -a "$(ls -A $dir )" ]
    then
        cd $dir
echo $dir
../cr-index
cd ..
    fi
done

loadmodule -a ../../../git.squashfs

. ../../../gitpushjessie

