#!/bin/bash

REPO="$(cat repo.txt | tr -d '[:blank:]' | tr -d '[:space:]')"

echo -n $( dirname -- "$( readlink -f -- "$0" )" ) > pwd.txt

CWD=$(cat pwd.txt)

dir_list=$(find . -mindepth 2 -maxdepth 4 -name "PKGBUILD" -type f | sort)

for i in ${dir_list[@]}
do
    path="$(dirname $i)"
    full_path="$CWD/$path"

    echo "$full_path"
    cd "$full_path"

    git_dir="$(find . -mindepth 1 -maxdepth 1 -name "$(basename $full_path)" -type d)"
    if [ -n "$git_dir" ] && [ -d "$git_dir" ]; then
        echo "rm -rf $git_dir"
        rm -rf "$git_dir"
    fi

    if [ -d "$full_path/src" ]; then
        rm -rf "$full_path/src"
    fi

    if [ -d "$full_path/pkg" ]; then
        rm -rf "$full_path/pkg"
    fi

    pkg_exist=$(find "$full_path" -type f -newermt "$(date '+%Y-%m-%d 00:00')" -name "*.pkg.tar.xz")
    if [ "$pkg_exist" == "" ]; then
        make_file=$(find . -mindepth 1 -maxdepth 1 -name "*make*\.sh" -type f)
        if [ -f "$make_file" ]; then
            echo "bash $make_file"
            bash "$make_file"
        else
            makepkg --syncdeps --asdeps --needed --noconfirm --clean --cleanbuild --force --config /etc/makepkg-gcc.conf
            if [ "$?" -ne 0 ]; then
                makepkg --syncdeps --asdeps --needed --noconfirm --clean --cleanbuild --force --config /etc/makepkg-gcc-without-lto.conf
                if [ "$?" -ne 0 ]; then
                    makepkg --syncdeps --asdeps --needed --noconfirm --clean --cleanbuild --force --config /etc/makepkg-clang.conf
                    if [ "$?" -ne 0 ]; then
                        makepkg --syncdeps --asdeps --needed --noconfirm --clean --cleanbuild --force --config /etc/makepkg-clang-without-lto.conf
                        if [ "$?" -ne 0 ]; then
                            echo "all fail" > fail
                        else
                            echo "makepkg-clang-without-lto.conf" > success
                        fi
                    else
                        echo "makepkg-clang.conf" > success
                    fi
                else
                    echo "makepkg-gcc-without-lto.conf" > success
                fi
            else
                echo "makepkg-gcc.conf" > success
            fi

            for i in *.pkg.tar.xz;
            do
                if [ -f "${i}" ]; then
                    if [ -f "$i.sig" ]; then
                        echo "Удаление подписи $i.sig"
                        rm -f $i.sig
                    fi
                    echo "Добавление подписи $i.sig"
                    gpg --detach-sign --local-user BC8B600E8DDA1F4CB77B10D2BA803A261A5EE6B8 --output "$i.sig" "$i"
                fi
            done
        fi
        unset make_file

        pkg_file=$(find . -mindepth 1 -maxdepth 1 -name "*.pkg.tar.xz" -type f)
        sig_file="${pkg_file}.sig"

        if [ ! -f "$sig_file" ]; then
            for i in *.pkg.tar.xz
            do
                if [ -f "${i}" ]; then
                    if [ -f "$i.sig" ]; then
                        echo "Удаление подписи $i.sig"
                        rm -f $i.sig
                    fi
                    echo "Добавление подписи $i.sig"
                    gpg --detach-sign --local-user BC8B600E8DDA1F4CB77B10D2BA803A261A5EE6B8 --output "$i.sig" "$i"
                fi
            done
        fi

        if [ -n "$git_dir" ] && [ -d "$git_dir" ]; then
            echo "rm -rf $git_dir"
            rm -rf "$git_dir"
        fi

        if [ -d "$full_path/src" ]; then
            rm -rf "$full_path/src"
        fi

        if [ -d "$full_path/pkg" ]; then
            rm -rf "$full_path/pkg"
        fi
    fi

    cd "$CWD"
done

notify-send -a "$(basename $(pwd)): Make and Move to" -t 10000 "$REPO" "$(date '+%Y.%m.%d %H:%M:%S')"

echo "Ready"
