From 871841a7fab4560c4a44fd48f7f9c4a4f9107b2f Mon Sep 17 00:00:00 2001 From: Salt Date: Thu, 26 Sep 2019 03:34:42 -0500 Subject: [PATCH] Add minimal ptgdp script --- ptgdp | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 ptgdp diff --git a/ptgdp b/ptgdp new file mode 100755 index 0000000..5f06550 --- /dev/null +++ b/ptgdp @@ -0,0 +1,97 @@ +#! /bin/bash +# +# ptgdp - Play the Goddamned Playlist +# Copyright (C) 2019 Vintage Salt +# +# Distributed under terms of the MIT license. +# +set -e + +# Read-only set-once variables +declare -r _name="$(basename -- "$0")" +# Options +declare -i _opthelp +declare -i _optverbose=0 +# Working variables +declare _return + +# Helper Functions +log() { + # Print a line to the terminal if _optverbose is greater than $2 + # $2 defaults to 0 + [ -z "$1" ] && return 1 + if [ "$_optverbose" -ge "${2:-0}" ]; then + printf "%s\\n" "$1" + fi +} +error() { + # Print a red line to the terminal, exit if $2 is specified + [ -z "$1" ] && return 1 + if [ -t 2 ]; then + printf "\\e[31m%s\\e[0m\\n" "$1" 1>&2 + else + printf "ERROR: %s\\n" "$1" 1>&2 + fi + [ -n "$2" ] && exit "${2:-1}" +} +has() { + # Parse out all arguments and try to find them in path + # If an argument cannot be found, set _return and fail + [ -z "$@" ] && return 0 + for prog in $@; do + if ! command -v "$prog" > /dev/null 2>&1; then + _return="$prog" + return 1 + fi + done + return 0 +} + +# Core program functions +printhelp() { + cat << EOF +Usage: $_name [OPTION]... [FILE]... +Use youtube-dl and a music player to queue up or download a number of songs +given plaintext files with only search queries + + -h Print this help text + -v Print more status messages. Stacks + +Copyright (c) 2019 rehashedsalt@cock.li +Licensed under the MIT license +EOF +} + +# Main +main() { + # Getopts before anything else + while getopts ":hv" opt; do + case $opt in + h) + _opthelp=1 + ;; + v) + _optverbose+=1 + ;; + :) + error "Option requires argument: -$OPTARG" 2 + ;; + *) + error "Invalid option: -$OPTARG" 2 + ;; + esac + done + + # Pre-really-do-stuff hooks like help text + [ -n "$_opthelp" ] && printhelp && exit 0 + + # Do the do + log "Validating dependencies" 1 + if ! has youtube-dl; then + error "Failed to validate dependency on program: $_return" 1 + fi + exit 0 +} + +main "$@" +