#!/bin/bash

REPO="/data/github/active/Melawy/REPO/melawy-core/x86_64/"

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

CWD=$(cat pwd.txt)

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

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

    cd "$CWD"
done

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

echo "Ready"
