#!/usr/bin/env bash
# Desc: Wrapper for feh that accepts directory paths via posargs or stdin lines.
# Version: 0.3.0
# Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854
# Depends: GNU Parallel, GNU Bash v5.1.16, feh 3.6.3, GNU Coreutils 8.32 (b2sum)

#===Declare local functions===
yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
checkapp() {
    # Desc: If arg is a command, save result in assoc array 'appRollCall'
    # Usage: checkapp arg1 arg2 arg3 ...
    # Version: 0.1.1
    # Input: global assoc. array 'appRollCall'
    # Output: adds/updates key(value) to global assoc array 'appRollCall'
    # Depends: bash 5.0.3
    local returnState    

    #===Process Args===
    for arg in "$@"; do
	if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
	    appRollCall[$arg]="true";
	    if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
	else
	    appRollCall[$arg]="false"; returnState="false";
	fi;
    done;

    #===Determine function return code===
    if [ "$returnState" = "true" ]; then
	return 0;
    else
	return 1;
    fi;
} # Check that app exists
checkfile() {
    # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
    # Usage: checkfile arg1 arg2 arg3 ...
    # Version: 0.1.1
    # Input: global assoc. array 'fileRollCall'
    # Output: adds/updates key(value) to global assoc array 'fileRollCall';
    # Output: returns 0 if app found, 1 otherwise
    # Depends: bash 5.0.3
    local returnState

    #===Process Args===
    for arg in "$@"; do
	if [ -f "$arg" ]; then
	    fileRollCall["$arg"]="true";
	    if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
	else
	    fileRollCall["$arg"]="false"; returnState="false";
	fi;
    done;
    
    #===Determine function return code===
    if [ "$returnState" = "true" ]; then
	return 0;
    else
	return 1;
    fi;
} # Check that file exists
checkdir() {
    # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
    # Usage: checkdir arg1 arg2 arg3 ...
    # Version 0.1.1
    # Input: global assoc. array 'dirRollCall'
    # Output: adds/updates key(value) to global assoc array 'dirRollCall';
    # Output: returns 0 if app found, 1 otherwise
    # Depends: Bash 5.0.3
    local returnState

    #===Process Args===
    for arg in "$@"; do
	if [ -d "$arg" ]; then
	    dirRollCall["$arg"]="true";
	    if ! [ "$returnState" = "false" ]; then returnState="true"; fi
	else
	    dirRollCall["$arg"]="false"; returnState="false";
	fi
    done
    
    #===Determine function return code===
    if [ "$returnState" = "true" ]; then
	return 0;
    else
	return 1;
    fi
} # Check that dir exists
displayMissing() {
    # Desc: Displays missing apps, files, and dirs
    # Usage: displayMissing
    # Version 0.1.1
    # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
    # Output: stderr: messages indicating missing apps, file, or dirs
    # Depends: bash 5, checkAppFileDir()
    local missingApps value appMissing missingFiles fileMissing
    local missingDirs dirMissing
    
    #==BEGIN Display errors==
    #===BEGIN Display Missing Apps===
    missingApps="Missing apps  :";
    #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
    for key in "${!appRollCall[@]}"; do
	value="${appRollCall[$key]}";
	if [ "$value" = "false" ]; then
	    #echo "DEBUG:Missing apps: $key => $value";
	    missingApps="$missingApps""$key ";
	    appMissing="true";
	fi;
    done;
    if [ "$appMissing" = "true" ]; then  # Only indicate if an app is missing.
	echo "$missingApps" 1>&2;
    fi;
    unset value;
    #===END Display Missing Apps===

    #===BEGIN Display Missing Files===
    missingFiles="Missing files:";
    #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
    for key in "${!fileRollCall[@]}"; do
	value="${fileRollCall[$key]}";
	if [ "$value" = "false" ]; then
	    #echo "DEBUG:Missing files: $key => $value";
	    missingFiles="$missingFiles""$key ";
	    fileMissing="true";
	fi;
    done;
    if [ "$fileMissing" = "true" ]; then  # Only indicate if an app is missing.
	echo "$missingFiles" 1>&2;
    fi;
    unset value;
    #===END Display Missing Files===

    #===BEGIN Display Missing Directories===
    missingDirs="Missing dirs:";
    #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
    for key in "${!dirRollCall[@]}"; do
	value="${dirRollCall[$key]}";
	if [ "$value" = "false" ]; then
	    #echo "DEBUG:Missing dirs: $key => $value";
	    missingDirs="$missingDirs""$key ";
	    dirMissing="true";
	fi;
    done;
    if [ "$dirMissing" = "true" ]; then  # Only indicate if an dir is missing.
	echo "$missingDirs" 1>&2;
    fi;
    unset value;
    #===END Display Missing Directories===

    #==END Display errors==
} # Display missing apps, files, dirs
check_depends() {
    if ! checkapp feh parallel bkshuf b2sum; then
        displayMissing;
        die "FATAL:Missing apps.";
    fi;
    return 1;
}; # check dependencies
checkInt() {
    # Desc: Checks if arg is integer
    # Usage: checkInt arg
    # Input: arg: integer
    # Output: - return code 0 (if arg is integer)
    #         - return code 1 (if arg is not integer)
    # Example: if ! checkInt $arg; then echo "not int"; fi;
    # Version: 0.0.1
    local returnState

    #===Process Arg===
    if [[ $# -ne 1 ]]; then
	die "ERROR:Invalid number of arguments:$#";
    fi;
    
    RETEST1='^[0-9]+$'; # Regular Expression to test
    if [[ ! $1 =~ $RETEST1 ]] ; then
	returnState="false";
    else
	returnState="true";
    fi;

    #===Determine function return code===
    if [ "$returnState" = "true" ]; then
	return 0;
    else
	return 1;
    fi;
} # Checks if arg is integer
read_stdin() {
    # Desc: Consumes stdin; outputs as stdout lines
    # Input: stdin (consumes)
    # Output: stdout (newline delimited)
    # Example: printf "foo\nbar\n" | read_stdin
    # Depends: GNU bash (version 5.1.16)
    # Version: 0.0.1
    local input_stdin output;

    # Store stdin
    if [[ -p /dev/stdin ]]; then
        input_stdin="$(cat -)";
    fi; 
    
    # Store as output array elements
    ## Read in stdin
    if [[ -n $input_stdin ]]; then
        while read -r line; do
            output+=("$line");
        done < <(printf "%s\n" "$input_stdin");
    fi;

    # Print to stdout
    printf "%s\n" "${output[@]}";
}; # read stdin to stdout lines
read_psarg() {
    # Desc: Reads arguments; outputs as stdout lines
    # Input: args
    # Output: stdout (newline delimited)
    # Example: read_psarg "$@"
    # Depends: GNU bash (version 5.1.16)
    # Version: 0.0.1
    local input_psarg output;
    
    # Store arguments
    if [[ $# -gt 0 ]]; then
        input_psarg="$*";
    fi;
    
    # Store as output array elements
    ## Read in positional arguments
    if [[ -n $input_psarg ]]; then
        for arg in "$@"; do
            output+=("$arg");
        done;
    fi;

    # Print to stdout
    printf "%s\n" "${output[@]}";
}; # read positional argument to stdout lines
find_flist() {
    # Desc: print file list to stdout via `find` using script parameters
    # Input: arg1: path to dir
    #        var: find_depth
    #        var: pattern_find_iregex
    #        var: find_size
    if [[ ! -d "$1" ]]; then return 1; fi;
    must find "$1" -maxdepth "$fdepth" -type f -iregex "$firegex" -size +"$fsize";
}; # print file list to stdout from dir with script parameters
save_sample() {
    # Usage: save_sample arg1
    # Input: arg1   list_paths        (list of files to take samples from)
    #        envvar BKFEH_SAMPLE_DIR  (environment variable set outside of this script)
    #        envvar BKFEH_SAMPLE_SIZE (space limit for sample dir files)
    # Depends: GNU Parallel, GNU find, GNU Coreutils 8.32 (cut, find, du)
    #   BK-2020-03: bkshuf (0.0.1), yell()
    local list_paths
    sample_count="100"; # max number of images to put in sample dir
    sample_max_space="10000000"; # max bytes to put in sample dir

    # Load environment variables if set
    if [[ ! -v BKFEH_SAMPLE_DIR ]]; then return 0; fi; # return early if environment var not set.
    if [[ -v BKFEH_SAMPLE_SIZE ]] && checkInt "$BKFEH_SAMPLE_SIZE"; then
        sample_max_space="$BKFEH_SAMPLE_SIZE";
    fi;
    if [[ -v BKFEH_SAMPLE_COUNT ]] && checkInt "$BKFEH_SAMPLE_COUNT"; then
        sample_count="$BKFEH_SAMPLE_COUNT";
    fi;
    
    if [[ -n "$1" ]]; then
        list_paths="$1"; # newline-delimited list of file paths to sample from
    else
        yell "ERROR:NO paths available to sample.";
    fi;
    
    if [[ -d "$BKFEH_SAMPLE_DIR" ]]; then
        #sample_dir="$BKFEH_SAMPLE_DIR";
        yell "STATUS:Environment variable BKFEH_SAMPLE_DIR set. Clearing and saving samples...";
        
        ## clear previous sample
        count_prev_samples="$(find "$BKFEH_SAMPLE_DIR" -maxdepth 1 -type f | wc -l)";
        yell "STATUS:Deleting $count_prev_samples previous samples...";
        find "$BKFEH_SAMPLE_DIR" -maxdepth 1 -type f -exec rm '{}' \; ;
        
        ## save random sample
        yell "STATUS:Saving random sample of size $sample_count to $BKFEH_SAMPLE_DIR...";
        list_paths_sample="$(echo "$list_paths" | bkshuf "$sample_count" | head -n"$sample_count")";
        n_samp=0; # init sample file counter
        sample_log="$BKFEH_SAMPLE_DIR"/paths.txt;
        printf "%s,%s,%s\n" "n_samp" "file_hash" "file_path" >> "$sample_log";
        while read -r line; do
            if [[ -z "$line" ]]; then continue; fi;
            ### check size limit
            sample_act_space="$(du -bd1 "$BKFEH_SAMPLE_DIR" | cut -f1 )"; # actual used space
            cand_space="$(du -bd1 "$line" | cut -f1 )"; # size of candidate file to add
            sample_req_space="$((sample_act_space + cand_space))";

            ### Customize file names
            n_samp_w="$(printf "%s" "$sample_count" | wc -c)";
            n_samp_fmt="%0""$n_samp_w""d";
            n_samp_dd="$(printf "$n_samp_fmt" "$n_samp")"; # sample number fixed-width
            file_path="$line";
            #file_dir="$(dirname "$line")";
            file_name="$(basename "$line")";
            file_hash="$(b2sum -l32 "$line" | awk '{print $1}')"; # use file hash to avoid clobbering
            file_ext="${file_name##*.}";
            file_name="${file_name%.*}";
            file_shortname="${file_name:0:32}";
            file_name_new="$n_samp_dd"_"$file_hash".."$file_shortname"."$file_ext";
            file_path_new="$BKFEH_SAMPLE_DIR"/"$file_name_new"            
            if [[ "$sample_req_space" -lt "$sample_max_space" ]]; then
                #### add file to sample dir
                must cp -n "$file_path" "$file_path_new";
                #### note path in sample dir log
                printf "%s,%s,%s\n" "$n_samp_dd" "$file_hash" "$file_path" \
                       >> "$sample_log";
            fi;
            ((n_samp++));
        done < <( echo "$list_paths_sample" );
    else
        yell "ERROR:Does not exist: $BKFEH_SAMPLE_DIR";
    fi;    
}; # save sample of files

main() {
    # Depends: read_stdin_psarg() v0.0.1, check_depends()
    local re_dotfile;
    declare -a dirs_stdin dirs_psarg;
    declare -a paths_images;
    declare list_paths_images;
    check_depends;

    #Populate dirs_stdin and dirs_psarg arrays
    ## Read stdin as lines
    re_dotfile="^\."; # first char is a dot
    while read -r line; do
        line="$(readlink -e "$line")";
        # Check if dir
        if [[ ! -d "$line" ]]; then
            echo "ERROR:Not a dir:$line" 1>&2;
            continue;
        fi;
        dir_name="$(basename "$line")";
        # Exclude dotdirs
        if [[ "$dir_name" =~ $re_dotfile ]]; then
            echo "ERROR:Is a dotdir:$line" 1>&2;
            continue
        fi;
        dirs_stdin+=("$line");
    done < <(read_stdin);
    ## Read positional arguments as lines
    re_dotfile="^\."; # first char is a dot
    while read -r line; do
        line="$(readlink -e "$line")";
        # Check if dir
        if [[ ! -d "$line" ]]; then
            echo "ERROR:Not a dir:$line" 1>&2;
            continue;
        fi;
        dir_name="$(basename "$line")";
        # Exclude dotdirs
        if [[ "$dir_name" =~ $re_dotfile ]]; then
            echo "ERROR:Is a dotdir:$line" 1>&2;
            continue
        fi;
        dirs_psarg+=("$line");
    done < <(read_psarg "$@");
    
    # Catch all arrays empty
    if [[ "${#dirs_stdin[@]}" -le 0 ]] && [[ "${#dirs_psarg[@]}" -le 0 ]]; then
        die "FATAL:No valid directories provided.";
    fi;
    
    # Generate file list
    # Find settings
    firegex=".+\(jpg\|jpeg\|gif\|png\|webm\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u`
    fsize="10k"; # default: minimum "10k"
    export firegex fsize ; # export for parallel
    ## Call find_filelist() in parallel for positional argument input
    if [[ "${#dirs_psarg[@]}" -gt 0 ]]; then
        fdepth=10; export fdepth; # 10 for dirs from positional arguments
        paths_images+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_psarg[@]}" )"); # See [1]
    fi;
    ## Call find_filelist() in parallel for stdin input
    if [[ "${#dirs_stdin[@]}" -gt 0 ]]; then
        fdepth=1; export fdepth; # 1 ofr dirs from stdin
        paths_images+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_stdin[@]}" )"); # See [1]
    fi;
    
    # Convert paths_images array into file list
    for i in "${!paths_images[@]}"; do
        list_paths_images="$(printf "%s\n%s" "${paths_images[$i]}" "$list_paths_images")";

        # Get stats
        file_count="$(wc -l < <(echo -n "$list_paths_images"))";
        echo "$DEBUG:file_count:$file_count"
    done;
    
    # Sort, remove duplicate paths
    list_paths_images="$(echo "$list_paths_images" | sort -u | tr -s '\n')";

    # Remove paths with dotfiles
    list_paths_images="$(echo "$list_paths_images" | grep -viE "/\." )";

    # Write
    list_paths_images_tmp="/dev/shm/$(date +%Y%m%dT%H%M%S.%N%z)"..feh_paths.txt;
    must echo -n "$list_paths_images" > "$list_paths_images_tmp";

    # Print stats
    yell "STATUS:Built file list in $SECONDS seconds.";

    # Run feh with filelist
    feh --full-screen --auto-zoom --draw-filename --filelist "$list_paths_images_tmp" && \
        must rm "$list_paths_images_tmp" &

    # Save sample to path in env. var. BKFEH_SAMPLE_DIR if set
    save_sample "$list_paths_images";
    
};
export -f yell die must read_stdin read_psarg find_flist;
#==END Define local functions==

main "$@";

# Author: Steven Baltakatei Sandoval
# License: GPLv3+