#!/bin/bash
shopt -s extglob;

# Usage:
# aur4 <commit-message>
# Example:
# aur4 "Initial import"

# Note: MUST run this script under the cloned (or soon to be cloned) directory of the package's aur4 repository.

# If user requested help, print it.
_chkHelp() {
    case "${1}" in        
        +(-)[hH]*([eE][lL][pP]) )
            _help 0;
            ;;
        * )
            return
            ;;
    esac;
}

# Print help and exit.
_help() {
    local path="$0";
    local script=${path##*/};
    cat << EOH >&2
Usage:
  ${script} <commit-message>
Note:
  This ${script} script needs to run under the cloned (or soon 
  to be cloned) directory of the package's aur4 repository.
EOH

#'
    [ "${1}" ] && exit "${1}";
    exit 1;
}

# Set font color in Terminal.
_c() {
    tput sgr0;
    (($# == 0)) && return;
    
    local -Ar Codes=(
        ["black"]="0" ["red"]="1" ["green"]="2" ["yellow"]="3" 
        ["blue"]="4" ["magenta"]="5" ["cyan"]="6" ["white"]="7" 
        ["bk"]="0" ["r"]="1" ["g"]="2" ["y"]="3" 
        ["bu"]="4" ["m"]="5" ["c"]="6" ["w"]="7" 
    );

    local k c;
    for k in "${!Codes[@]}"; do
        [[ "${k}" == "${1}" ]] && 
            c="${Codes["$k"]}";
    done;

    [ "${c}" ] && tput setaf "${c}";
}

# Set bold font color in Terminal.
# Optionally set a new font color as well.
_C() {
    (($#<1)) && _c "${1}";
    tput bold;
}

# Reset font color in Terminal to normal.
_u() {
    tput sgr0;
}

# Print messages in bold green.
_gecho() {
    _C green
    for line; do
        echo "${line}";
    done;
    _u;
}

# Print a message in bold green, WITHOUT appending a newline.
_gprint() {
    _C green
    printf "%s" "$*";
    _u;
}

# Echo a command in bold green and then run it.
_gechval() {
    _C green;
    echo "$@";
    eval "$@";
    _u;
}

# Run a command silently.
_q() {
    eval "$@" >/dev/null;
}

# Exit with error 1 if the previous command returned an error.
_err() {
    [[ $? == 0 ]] || exit 1;
}

# Obtain user input (command variant of `read -p`).
_inp() {
    _u;

    local ans;
    read -p "" ans;
    
    printf "\r";
    echo "${ans}";
}

# Rerun script if no arguments were passed.
_VrfyArgs() {
    ((${#ARGS} > 0)) && return;

    local msg;
    for msg; do
        _gprint "${msg}";
        ARGS+=( "$(_inp)" );
    done;

    ((${#ARGS} == 0)) && ARGS+=( "$(_inp)" );

    $0 "${ARGS[@]}";
    exit;
}

# Install any missing package dependencies.
_VrfyDeps() {
    if ! _q command -v "yaourt"; then
        echo "Install yaourt first.";
        exit 1;
    fi;

    local -Ar Deps=( 
        ['git']='git'
        ['mksrcinfo']='pkgbuild-introspection'
        ['wget']='wget' 
    );

    local cmd;
    for cmd in "${!Deps[@]}"; do
        _q command -v "${cmd}" ||
            yaourt -Sa "${Deps[$cmd]}";
    done;
}

# Clone an aur4 repo if it has not been cloned already.
_VrfyCloned() {
    _isCloned && return 0;

    local pkg="${1}";
    if ! [ "${pkg}" ]; then
        _gprint "Enter package name: ";
        read -p "" pkg;
    fi;
    
    _clone "${pkg}";
    _err;
}

# Test whether the current directory is a cloned git/aur4 repository.
_isCloned() {
    [ -d ".git" ] && return 0;
    return 1;
}

# Git clone aur4 repository.
_clone() {
    local -r repo="${1}";
    _gechval git init .;
    _gechval git remote add -t \* -f origin "ssh://aur@aur.archlinux.org/${repo}.git";
    _gechval git checkout master;
}

# Upload local repository to its aur4 repository.
_Upload() {
    mksrcinfo;
    _err;

    git add .;
    git add .SRCINFO -f;
    _err;

    git commit -m "$@";
    _err;

    git push origin master;
}

_chkHelp "${1}";

declare -a ARGS=( $@ );

declare ROOT;
ROOT="$(basename "${PWD}")";

_VrfyDeps;

_VrfyArgs "Enter commit: ";

declare COMMIT="${ARGS[*]}";
[ "${COMMIT[*]}" ] || _help;

_VrfyCloned;

_Upload "${COMMIT}";

