aboutsummaryrefslogblamecommitdiff
path: root/plz
blob: c551ca938cf0ec90ca1cc81d946356a2cda62ed3 (plain) (tree)
1
2

                   



















                                                                              

        
                                                                         



                                         
                                                            
                                  
                                             



                                








                                                               




                                            
                                                     

                  
          

  
                                          





                                   
                                                      

                                  
                                 
                                                   
 
                                    
                      

                                             




                                    

                              
      
                                                      
          

                                          
      
                                     


          
#!/usr/bin/env bash

#
# plz -- Play a media file from a configured directory with a configured media
# player if it's a unique match for all the words given as arguments. If not
# unique, print all the matches to help the user pick unique terms.
#
#   $ plz led zep stairw
#   $ plz black sab iron m
#   $ plz beeth symph 9
#
# Create a file /etc/plzrc and/or ~/.plzrc to use:
#
#   dir=/path/to/media
#   player=mplayer
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
# Copyright: 2016
# License: MIT
#

# Name self
self=plz

# Specify array of configuration files; /etc/plzrc and ~/.plzrc, probably
declare -a confs
# shellcheck disable=SC2140
confs=(/etc/"$self"rc "$HOME"/."$self"rc)

# Iterate through configuration files, source any that exist
# shellcheck disable=SC2066,SC2068
for conf in "${PLZ_CONFIG:-${confs[@]}}" ; do
    [[ -e "$conf" ]] || continue
    source "$conf"
done

# Default the configuration to the environment variables if set
: "${dir:=$PLZ_DIR}"
: "${player:=$PLZ_PLAYER}"

# If we still don't have a media directory or player, bail
: "${dir:?}"
: "${player:?}"

# Make usage printing function
usage() {
    printf 'USAGE: %s TERM1 [TERM2 ...]\n' \
        "$self" >&2
}

# If no arguments, print usage and exit with an error
if ! (($#)) ; then
    usage >&2
    exit 2
fi

# Start building array of args for find(1)
declare -a fargs
for term ; do
    fargs[${#fargs[@]}]=-ipath
    fargs[${#fargs[@]}]=\*"$term"\*
done

# Collect an array of the full paths of matching files
declare -a matches
while IFS= read -d '' -r file ; do
    matches[${#matches[@]}]=$file
done < <(find "$dir" -type f "${fargs[@]}" -print0)

# Action depends on count of matches
case ${#matches[@]} in

    # No matches; report and exit with errors
    0)
        printf '%s: No matches.\n' \
            "$self" >&2
        exit 1
        ;;

    # One match; play the file
    1)
        exec "$player" "${opts[@]}" -- "${matches[0]}"
        ;;

    # More than one match; print the names
    *)
        printf '%s\n' "${matches[@]}"
        ;;
esac