Last active
September 30, 2022 15:18
-
-
Save Stewie410/68d511f7aeecd48b54e458179e4c655f to your computer and use it in GitHub Desktop.
A rewrite of u/dewdude's AJW script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # Generate the dialplan that interfaces with the player logic | |
| cleanup() { | |
| if [[ -s "${stage}" ]]; then | |
| if [[ "${outfile}" == "-" ]]; then | |
| cat "${stage}" | |
| else | |
| mv --force "${stage}" "${outfile}" | |
| cat <<- EOF | |
| Done. | |
| Your jukebox is located at '${outfile}' | |
| Make sure to include it and the player config in your dial plan somewhere | |
| EOF | |
| fi | |
| fi | |
| rm --force "${stage}" | |
| unset stage outfile parent | |
| } | |
| show_help() { | |
| cat << EOF | |
| Generate the dialplan that interfaces with the player logic | |
| USAGE: ${0##*/} [OPTIONS] (-i|--interactive) | |
| ${0##*/} [OPTIONS] NAME ROOTFOLDER INTROFILE GOODBYE LOOP | |
| OPTIONS: | |
| -h, --help Show this help message | |
| -V, --version Display script version info | |
| -i, --interactive Prompt user for required parameters | |
| -d, --delay INT Delay (default: none) | |
| -v, --volume INT Volume (default: -3) | |
| -o, --outfile PATH Write configuration to PATH (default: "./YYYY-mm-ddTHH:MM:SS:zzzz-juke.conf") | |
| If PATH is "-", write configuration to stdout | |
| -p, --parent PATH Asterisk Sounds Directory parent path (default: "/var/lib/asterisk/sounds/en" | |
| EOF | |
| } | |
| show_version() { | |
| cat <<- EOF | |
| |a|j|w| - asterisk jukebox wizard | |
| version 1.51 - Jay Moore/NQ4T | |
| git.pickmy.org/dewdude/Asterisk_Jukebox | |
| EOF | |
| } | |
| init() { | |
| ( | |
| cat <<- EOF | |
| [${1}-main] | |
| exten = s,1,Answer() | |
| same = n,Set(VOLUME(TX)=${6} | |
| same = n,Set(c=${1}-main) | |
| same = n(return),Set(m=1) | |
| same = n(loop),Background(${2}/${3}) | |
| EOF | |
| (($# == 7)) && printf 'same = n,WaitExten(%s)\n' "${7}" | |
| cat <<- EOF | |
| same = n,Set(m=\$[\${m} + 1]) | |
| same = n,GoToIf(\$["\${m}" > "${5}"]?loop) | |
| same = n(goodbye),Background(${2}/${4}) | |
| same = n,Hangup() | |
| exten = i,1,Playback(option-is-invalid) | |
| same = n,GoTo(s,return) | |
| EOF | |
| ) >> "${stage}" | |
| } | |
| # $path | |
| loop() { | |
| local folder idx | |
| idx="0" | |
| find "${*}" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort --numeric-sort | while read -r folder; do | |
| find "${*}/${folder}" -iname "*.ulaw" -printf '%f\n' | \ | |
| awk --assign "folder=${*##*/}/${folder}" --assign "idx=${idx}" ' | |
| function prefix() { | |
| if (ld == 0) | |
| return "#" | |
| return "*" | |
| } | |
| BEGIN { | |
| limit = 0 | |
| ld = 0 | |
| } | |
| { | |
| limit++ | |
| } | |
| /^0/ { | |
| ld++ | |
| } | |
| END { | |
| printf "\nexten = _%02dXX,1,Set(aa=%s)\n", idx, folder | |
| printf "same = n,GoToIf($[\"${EXTEN:-2}\" = \"00\"]?8track-player,%s%02d,1)\n", tc_prefix(), limit + 1 | |
| print "same = n,GoToIf($[\"${EXTEN:-2}\" > \"" limit "\"]?i,1)" | |
| if (ld == 0) | |
| print "same = n,GoToIf($[\"${EXTEN:-2}\" < \"10\"]?cart-player,#${EXTEN:-1},1)" | |
| print "same = n,GoTo(cart-player,*${EXTEN:-2},1)" | |
| } | |
| ' >> "${stage}" | |
| ((idx++)) | |
| done | |
| } | |
| showRequiredArgs() { | |
| cat << EOF | |
| Current required parameters: | |
| - Name: ${1} | |
| - Root Folder: ${2} | |
| - Intro File: ${3} | |
| - Goodbye: ${4} | |
| - Loop: ${5} | |
| - Volume: ${6} | |
| - Delay: ${7} | |
| - Outfile: ${8} | |
| EOF | |
| } | |
| interactive() { | |
| local -a iargs prompts defaults | |
| local answer i | |
| prompts[0]="Jukebox name/identifier"; defaults[0]="" | |
| prompts[1]="Jukebox albums root folder name"; defaults[1]="" | |
| prompts[2]="Menu/Intro file-name"; defaults[2]="" | |
| prompts[3]="Exit file-name"; defaults[3]="" | |
| prompts[4]="Play loop count"; defaults[4]="1" | |
| prompts[5]="Channel volume (default: ${volume})"; defaults[5]="${volume}" | |
| prompts[6]="Delay in seconds (default: ${delay:-0})"; defaults[6]="${delay:-0}" | |
| printf '%s\n' "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" | |
| show_version | |
| printf '%s\n' "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" | |
| read -ri "${parent}" -p "Jukebox root path (default: '${parent}'): " parent | |
| for ((i = 0; i < ${#prompts[@]}; i++)); do | |
| read -ri "${defaults[i]:--}" -p "${prompts[i]}: " answer | |
| if [[ "${answer}" == "-" ]]; then | |
| printf '%s\n' "'${prompts[i]% (*}' is required" >&2 | |
| return 1 | |
| fi | |
| iargs+=("${answer}") | |
| done | |
| showRequiredArgs "${iargs[@]}" "${outfile}" | |
| read -ri "n" -n 1 -p "Continue? (y/N): " answer | |
| [[ "${answer,,}" == "y" ]] || return 1 | |
| init "${iargs[@]}" | |
| loop "${parent}/${iargs[1]}" | |
| } | |
| main() { | |
| local opts interact delay volume ans | |
| volume="-3" | |
| opts="$(getopt \ | |
| --options hVid:v:o: \ | |
| --longoptions help,version,interactive,delay:,volume:,outfile: \ | |
| --name "${0##*/}" \ | |
| -- "${@}" \ | |
| )" | |
| eval set -- "${opts}" | |
| while true; do | |
| case "${1}" in | |
| -h | --help ) show_help; return 0;; | |
| -V | --version ) show_version; return 0;; | |
| -i | --interactive ) interact="1";; | |
| -d | --delay ) delay="${2}"; shift;; | |
| -v | --volume ) volume="${2}"; shift;; | |
| -o | --outfile ) outfile="${2}"; shift;; | |
| -p | --parent ) parent="${2}"; shift;; | |
| -- ) shift; break;; | |
| * ) break;; | |
| esac | |
| shift | |
| done | |
| if [[ -n "${interact}" ]]; then | |
| interactive || return 1 | |
| elif (($# != 5)); then | |
| printf '%s\n' "Missing required parameters for non-interactive mode!" >&2 | |
| show_help | |
| return 1 | |
| else | |
| showRequiredArgs "${@}" "${volume}" "${delay}" "${outfile}" | |
| read -ri "n" -n 1 -p "Continue? (y/N): " ans | |
| [[ "${ans,,}" == "y" ]] || return 1 | |
| init "${@}" "${volume}" "${delay}" > "${stage}" | |
| loop "${parent}/${2}" | |
| fi | |
| return 0 | |
| } | |
| parent="/var/lib/asterisk/sounds/en" | |
| stage="$(mktemp)" | |
| outfile="$(realpath .)/$(date --iso-8061=sec)-juke.conf" | |
| trap cleanup EXIT | |
| main "${@}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment