#!/bin/sh


ARCH=""
PARCH=""

if [ "$1" = "" ]; then
    echo "Automatically detecting architecture."
    PARCH=$(uname -m)
        if [ "$PARCH" = "i386" ] || [ "$PARCH" = "i686" ]; then
        ARCH="i386"
        fi
        if [ "$PARCH" = "amd64" ] || [ "$PARCH" = "x86_64" ]; then
        ARCH="amd64"
        fi
elif [ "$1" = "i386" ]; then
    echo "Regenerating i386 debian packages and debian repository."
    echo "Requires the reprepro tool."
    ARCH="$1"
elif [ "$1" = "amd64" ]; then
    echo "Regenerating amd64 debian packages and debian repository."
    echo "Requires the reprepro tool."
    ARCH="$1"
else
    echo "Usage: build-deb-repo i386 | amd64"
    exit 5
fi




set -e

for repo in 1 2; do
    rm -f deb_repo$repo/*$ARCH*.deb
    #rm -f file_repo/*1.0-${repo}_$ARCH*.deb
    rm -rf deb_repo$repo/db
    rm -rf deb_repo$repo/dists
    rm -rf deb_repo$repo/pool

    for i in alpha beta charlie; do
        #fakeroot dpkg-deb -b test-package-${i}_1.0-${repo}_"$ARCH"
        #cp -f test-package-${i}_1.0-${repo}_"$ARCH".deb file_repo/test-package-${i}_1.0-${repo}_"$ARCH".deb
        cp -f file_repo/test-package-${i}_1.0-${repo}_"$ARCH".deb test-package-${i}_1.0-${repo}_"$ARCH".deb
        mv -f test-package-${i}_1.0-${repo}_"$ARCH".deb deb_repo$repo/test-package-${i}_1.0-${repo}_"$ARCH".deb
    done

    cd deb_repo$repo
    for i in *.deb; do
        REPREPRO_BASE_DIR=`pwd` ../putinrepo.sh $i
    done

    for i in `find . -name Release`; do
        gpg --detach-sign --armor --no-default-keyring --keyring ../gpg/pubring.gpg --secret-keyring ../gpg/secring.gpg --output $i.gpg $i
    done
    cd ..
done
