#!/usr/bin/sh #---------------------------------------------------------------------------------- # /var/install/bin/lprng_power-status - show status # # Copyright (c) 2010-2024 The Eisfair Team, team(at)eisfair(dot)org # # Creation: 2010-07-10 jed # Last Update: $Id$ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------------- # read eislib etc. . /var/install/include/eislib . /var/install/include/jedlib # debug mode: true/false #debug=true if ${debug:-false} then exec 2>/tmp/$(basename ${0})-trace$$.log set -x ask_debug=true export ask_debug fi #---------------------------------------------------------------------------------------- # print list of devices #---------------------------------------------------------------------------------------- print_list_of_devices () { if [ ${LPRNG_POWER_N} -gt 0 ] then # devices found, go on ... techo --begin r4 1 15 10 20 30 techo --row --info "Nbr" "" --info "Status" --info "Printer" --info "IP Address" --info "Comment" sel_values='' idx=1 while [ ${idx} -le ${LPRNG_POWER_N} ] do atjob_id_file=/var/run/${module_name}-${idx}.id eval active="\$LPRNG_POWER_${idx}_ACTIVE" if [ "${active}" = "yes" ] then eval comment="\$LPRNG_POWER_${idx}_COMMENT" eval server="\$LPRNG_POWER_${idx}_IPADDR" eval capname="\$LPRNG_POWER_${idx}_PRINTER_CAPNAME" sel_values="${sel_values} ${idx}" if `/usr/bin/lprng_power-access -instance ${idx} --quiet --status` then status='off' else if [ -f ${atjob_id_file} ] then status='-warn on_(auto-off)' else status='-warn on' fi fi techo ${idx} "" ${status} ${capname} ${server} "${comment}" fi idx=`expr ${idx} + 1` done techo --end else mecho --warn " no devices found!" fi mecho } #---------------------------------------------------------------------------------------- # create menu selection - This function reformats the given selection lists # to a more readable format. Example: # # Input : "1 2 6 10 11 12 13 14 15 16 17 19" # Output: "1-2,6,10-17,19" # # Input : $1 - string of available menu values # Output: reformated string #---------------------------------------------------------------------------------------- create_menu_selection () { svalues=" $* " new_svalues='' prev_val='' range_flag=0 for curr_val in ${svalues} do # process input string if [ -z "${new_svalues}" ] then # add primiary value new_svalues="${curr_val}" else # additional values given... if [ `expr ${prev_val} + 1` -eq ${curr_val} ] then # previous value + 1 is equal current value => range condition if [ ${range_flag} -eq 0 ] then # range new_svalues="${new_svalues}-" range_flag=1 fi else # previous value +1 is less than current value => list condition if [ ${range_flag} -eq 1 ] then new_svalues="${new_svalues}${prev_val},${curr_val}" else new_svalues="${new_svalues},${curr_val}" fi range_flag=0 fi fi # save current value for next loop prev_val=${curr_val} done if [ ${range_flag} -eq 1 ] then # add final value if range condition has not been finalized new_svalues="${new_svalues}${prev_val}" fi echo "${new_svalues}" } #---------------------------------------------------------------------------------------- # check if valid menu selection # # Input : $1 - value to check # $2 - string of available menu values # Output: 0 - valid value, entry found # 1 - invalid value, entry not found #---------------------------------------------------------------------------------------- is_valid_menu_selection () { sval=$1 # value to check if [ -z "${sval}" ] then # empty string - invalid value ret=1 else shift sel_values=" $* " # string of available menu values echo "${sel_values}" | grep -q " ${sval} " ret=$? fi return ${ret} } #---------------------------------------------------------------------------------------- # print 'not in range' error message #---------------------------------------------------------------------------------------- print_not_in_range () { # not in range mecho mecho --error "Error: input not in range, try again!" mecho } #================================================================================== # main #================================================================================== module_name='lprng_power' pgmname=`basename $0` ### set file names ### lprng_powerfile=/etc/config.d/${module_name} . ${lprng_powerfile} if [ "${START_LPRNG_POWER}" = "yes" ] then u_exit=0 until [ ${u_exit} -eq 1 ] do clrhome mecho --info "Show/Toggle device status" mecho print_list_of_devices auto_on=1 auto_off=1 nbr='q' /var/install/bin/ask "Please select [append 'a' -> auto-off, 'd' -> no auto-off] (`create_menu_selection "${sel_values}"`), (q)uit" "${nbr}" '+' > /tmp/ask.$$ rc=$? nbr=`cat /tmp/ask.$$ | sed 's/[^0-9qQ]*//g'` auto_on=`grep -E -q "[aA]" /tmp/ask.$$; echo $?` auto_off=`grep -E -q "[dD]" /tmp/ask.$$; echo $?` rm -f /tmp/ask.$$ if [ ${rc} = 255 ] then exit 1 fi case "${nbr}" in [qQ] ) # quit u_exit=1 ;; [1-9]|1[0-9] ) # check input if is_valid_menu_selection ${nbr} ${sel_values} then if `/usr/bin/lprng_power-access -instance ${nbr} --quiet --status` then # status: off if [ ${auto_on} -eq 0 ] then # action: switch on with auto-off # echo "CMD: SWITCH ON WITH AUTO-OFF"; sleep 10 /usr/bin/lprng_power-access -instance ${nbr} --on-autooff else # action: switch on # echo "CMD: SWITCH ON"; sleep 10 /usr/bin/lprng_power-access -instance ${nbr} --on fi else # status: on if [ ${auto_on} -eq 0 ] then # action: enable auto-off # echo "CMD: ENABLE AUTO_OFF"; sleep 10 /usr/bin/lprng_power-access -instance ${nbr} --on-autooff elif [ ${auto_off} -eq 0 ] then # action: disable auto-off # echo "CMD: DISABLE AUTO_OFF"; sleep 10 /usr/bin/lprng_power-access -instance ${nbr} --on else # action: switch off # echo "CMD: SWITCH OFF"; sleep 10 /usr/bin/lprng_power-access -instance ${nbr} --off fi fi else nbr='q' print_not_in_range anykey fi ;; * ) print_not_in_range anykey ;; esac done else mecho --warn "Packages has been disabled!" fi anykey #================================================================================== # end #==================================================================================