4d88f670f29a0e7af813772117997066e71ff004
[quassel.git] / data / scripts / inxi
1 #!/bin/bash
2 ########################################################################
3 ####  Script Name: inxi
4 ####  version: 1.0.17
5 ####  Date: 7 July 2009
6 ########################################################################
7 ####  SPECIAL THANKS
8 ########################################################################
9 ####  Special thanks to all those in #lsc and #smxi for their tireless 
10 ####  dedication helping test inxi modules.
11 ########################################################################
12 ####  ABOUT INXI
13 ########################################################################
14 ####  inxi is a fork of infobash 3.02, the original bash sys info script by locsmif
15 ####  As time permits functionality improvements and recoding will occur.
16 ####
17 ####  inxi, the universal, portable, system info script for irc.
18 ####  Tested with Irssi, Xchat, Konversation, BitchX, KSirc, ircII,
19 ####  Gaim/Pidgin, Weechat, KVIrc and Kopete.
20 ####  Original infobash author and copyright holder:
21 ####  Copyright (C) 2005-2007  Michiel de Boer a.k.a. locsmif
22 ####  inxi version: Copyright (C) 2008-9 Scott Rogers & Harald Hope
23 ####  Further fixes (listed as known): Horst Tritremmel <hjt at sidux.com>
24 ####  Steven Barrett (aka: damentz) - usb audio patch; swap percent used patch
25 ####
26 ####  Current script home page: http://techpatterns.com/forums/about1131.html
27 ####  Script svn: http://code.google.com/p/inxi
28 ####
29 ####  This program is free software; you can redistribute it and/or modify
30 ####  it under the terms of the GNU General Public License as published by
31 ####  the Free Software Foundation; either version 3 of the License, or
32 ####  (at your option) any later version.
33 ####
34 ####  This program is distributed in the hope that it will be useful,
35 ####  but WITHOUT ANY WARRANTY; without even the implied warranty of
36 ####  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37 ####  GNU General Public License for more details.
38 ####
39 ####  You should have received a copy of the GNU General Public License
40 ####  along with this program.  If not, see <http://www.gnu.org/licenses/>.
41 ####
42 ####  If you don't understand what Free Software is, please read (or reread)
43 ####  this page: http://www.gnu.org/philosophy/free-sw.html
44 ########################################################################
45 ####  DEPENDENCIES
46 ####  bash >=3.0(bash), df;readlink;stty;tr;uname;wc(coreutils),
47 ####  gawk(gawk), grep(grep), hostname(hostname), lspci(pciutils),
48 ####  ps;uptime(procps), glxinfo;xdpyinfo;xrandr(xbase-clients)
49 ####  Also the proc filesystem should be present and mounted
50 ####
51 ####    Apparently unpatched bash 3.0 has arrays broken; bug reports:
52 ####    http://ftp.gnu.org/gnu/bash/bash-3.0-patches/bash30-008
53 ####    http://lists.gnu.org/archive/html/bug-bash/2004-08/msg00144.html
54 ####
55 ####    Arrays work in bash 2.05b, but "egrep -m" does not
56 ####
57 ####  RECOMMENDS (Needed to run certain features)
58 ####  For local interfaces/IP test: ifconfig (in net-tools for Debian systems)
59 ####  runlevel(sysvinit): to view current runlevel while not in X window system
60 ####  Bash 3.1 for proper array use
61 ########################################################################
62 ####  CONVENTIONS:
63 ####  Indentation: TABS
64 ####  Do not use `....` (back quotes), those are totally non-reabable, use $(....).
65 ####  Do not use one liner flow controls. 
66 ####  The ONLY time you should use ';' (semi-colon) is in this single case: if [[ condition ]];then.
67 ####  Never use compound 'if': ie, if [[ condition ]] && statement.
68 ####  Note: [[ -n $something ]] - double brackets does not require quotes for variables: ie, "$something".
69 ####  Always use quotes, double or single, for all string values.
70 ####
71 ####  All new code/methods must be in a function.
72 ####  For all boolean tests, use 'true' / 'false'.
73 ####  !! Do NOT use 0 or 1 unless it's a function return. 
74 ####  Avoid complicated tests in the if condition itself.
75 ####  To 'return' a value in a function, use 'echo <var>'.
76 ####
77 ####  For gawk: use always if ( num_of_cores > 1 ) { hanging { starter for all blocks
78 ####  This lets us use one method for all gawk structures, including BEGIN/END, if, for, etc
79 ####
80 ####  VARIABLE/FUNCTION NAMING:
81 ####  All functions should follow standard naming--verb adjective noun. 
82 ####    ie, get_cpu_data
83 ####  All variables MUST be initialized / declared explicitly.
84 ####  All variables should clearly explain what they are, except counters like i, j.
85 ####  Each word of variable must be separated by '_' (underscore) (camel form).
86 ####  Global variables are 'UPPER CASE', at top of script.
87 ####    ie, SOME_VARIABLE=''
88 ####  Local variables are 'lower case' and declared at the top of the function.
89 ####    ie, some_variable=''
90 ####  Locals that will be inherited by child functions have first char capitalized (so you know they are inherited).
91 ####    ie, Some_Variable 
92 ####
93 ####  Booleans should start with b_ (local) or B_ (global) and state clearly what is being tested.
94 ####  Arrays should start with a_ (local) or A_ (global).
95 ####
96 ####  SPECIAL NOTES:
97 ####  The color variable ${C2} must always be followed by a space unless you know what
98 ####  character is going to be next for certain. Otherwise irc color codes can be accidentally
99 ####  activated or altered.
100 ####
101 ####  For native script konversation support (check distro for correct konvi scripts path):
102 ####  ln -s <path to inxi> /usr/share/apps/konversation/scripts/inxi
103 ####  DCOP doesn't like \n, so avoid using it for most output unless required, as in error messages.
104 ####
105 ####  As with all 'rules' there are acceptions, these are noted where used.
106 ####
107 ###################################################################################
108 ####    KDE Konversation information.  Moving from dcop(qt3/KDE3) to dbus(qt4/KDE4)
109 ###################################################################################
110 ####    dcop and dbus   -- these talk back to Konversation from this script
111 ####    
112 ####    Scripting info  -- http://konversation.berlios.de/docs/scripting.html
113 ####                    -- http://www.kde.org.uk/apps/konversation/
114 ####
115 ####    dbus info       -- http://dbus.freedesktop.org/doc/dbus-tutorial.html
116 ####    view dbus info  -- https://fedorahosted.org/d-feet/
117 ####                    -- or run qdbus
118 ####    Konvi dbus/usage-- qdbus org.kde.konversation /irc say <server> <target-channel> <output>
119 ####
120 ####    Python usage    -- http://wiki.python.org/moin/DbusExamples  (just in case)
121 ####
122 ####    Because webpages come and go, the above information needs to be moved to inxi's wiki
123 ####    
124 ########################################################################
125 ####  Valuable Resources
126 ####  gawk arrays: http://www.math.utah.edu/docs/info/gawk_12.html
127 ########################################################################
128 ####  TESTING FLAGS
129 ####  inxi supports advanced testing triggers to do various things, using -! <arg>
130 ####  -! 1  - triggers default B_TESTING_1='true' to trigger some test or other
131 ####  -! 2  - triggers default B_TESTING_2='true' to trigger some test or other
132 ####  -! 3  - triggers B_TESTING_1='true' and B_TESTING_2='true'
133 ####  -! 10 - triggers an update from the primary dev download server instead of svn
134 ####  -! 11 - triggers an update from svn branch one - if present, of course
135 ####  -! 12 - triggers an update from svn branch two - if present, of course
136 ####  -! 13 - triggers an update from svn branch three - if present, of course
137 ####  -! 14 - triggers an update from svn branch four - if present, of course
138 ####  -! <http://......> - Triggers an update from whatever server you list.
139 #### LOG FLAGS (logs to $HOME/.inxi/inxi.log with rotate 3 total)
140 ####  -@ 8  - Basic data logging of generated data / array values
141 ####  -@ 9  - Full logging of all data used, including cat of files and system data
142 ####  -@ 10 - Basic data logging plus color code logging
143 ########################################################################
144 #### VARIABLES
145 ########################################################################
146
147 ## NOTE: we can use hwinfo if it's available in all systems, or most, to get
148 ## a lot more data and verbosity levels going
149
150 ### Variable initializations: null values
151 CMDL_MAX=''
152 COLOR_SCHEME=''
153 COLOR_SCHEME_SET=''
154 IRC_CLIENT=''
155 IRC_CLIENT_VERSION=''
156
157 ### primary data array holders  ## usage: 'A_<var>'
158 A_AUDIO_DATA=''
159 A_CMDL=''
160 A_CPU_CORE_DATA=''
161 A_CPU_DATA=''
162 A_CPU_TYPE_PCNT_CCNT=''
163 A_DEBUG_BUFFER=''
164 A_GFX_CARD_DATA=''
165 A_GLX_DATA=''
166 A_HDD_DATA=''
167 A_INTERFACES_DATA=''
168 A_NETWORK_DATA=''
169 A_PARTITION_DATA=''
170 A_X_DATA=''
171
172 ### Boolean true/false globals  ## usage: 'B_<var>'
173 # flag to allow distro maintainers to turn off update features. If false, turns off
174 # -U and -! testing/advanced update options, as well as removing the -U help menu item
175 B_ALLOW_UPDATE='true'
176 # triggers full display of cpu flags
177 B_CPU_FLAGS_FULL='false'
178 # Debug flood override: make 'true' to allow long debug output
179 B_DEBUG_FLOOD='false'
180 # show extra output data
181 B_EXTRA_DATA='false'
182 # override certain errors due to currupted data
183 B_HANDLE_CORRUPT_DATA='false'
184 B_LOG_COLORS='false'
185 B_LOG_FULL_DATA='false'
186 B_ROOT='false'
187 # Running in a shell? Defaults to false, and is determined later.
188 B_RUNNING_IN_SHELL='false'
189 # this sets the debug buffer
190 B_SCRIPT_UP='false'
191 # Show sound card data
192 B_SHOW_AUDIO='false'
193 B_SHOW_CPU='false'
194 B_SHOW_DISK='false'
195 # Show full hard disk output
196 B_SHOW_FULL_HDD='false'
197 B_SHOW_GRAPHICS='false'
198 # Set this to 'false' to avoid printing the hostname
199 B_SHOW_HOST='true'
200 B_SHOW_INFO='false'
201 B_SHOW_IP='false'
202 B_SHOW_LABELS='false'
203 B_SHOW_NETWORK='false'
204 # either -v > 3 or -P will show partitions
205 B_SHOW_PARTITIONS='false'
206 B_SHOW_PARTITIONS_FULL='false'
207 # triggers only short inxi output
208 B_SHOW_SHORT_OUTPUT='false'
209 B_SHOW_SYSTEM='false'
210 B_SHOW_UUIDS='false'
211 # triggers various debugging and new option testing
212 B_TESTING_1='false'
213 B_TESTING_2='false'
214 # set to true here for debug logging from script start
215 B_USE_LOGGING='false'
216 # Test for X running
217 B_X_RUNNING='false'
218 # test for dbus irc client
219 B_DBUS_CLIENT='false'
220
221 ### Directory/file exist flags; test as [[ $(boolean) ]] not [[ $boolean ]]
222 B_PROC='false'
223 B_CPUINFO='false'
224 B_MEMINFO='false'
225 B_ASOUND_CARDS='false'
226 B_ASOUND_VERSION='false'
227 B_BASH_ARRAY='false'
228 B_IFCONFIG='false'
229 B_LSB_DIR='false'
230 B_SCSI_DIR='false'
231 B_MODULES_DIR='false' #
232 B_MOUNTS_DIR='false'
233 B_PARTITIONS_DIR='false' #
234
235 ### Directory's used when present
236 DIR_CPUINFO='/proc/cpuinfo'
237 DIR_MEMINFO='/proc/meminfo'
238 DIR_ASOUND_DEVICE='/proc/asound/cards'
239 DIR_ASOUND_VERSION='/proc/asound/version'
240 DIR_LSB_RELEASE='/etc/lsb-release'
241 DIR_SCSI='/proc/scsi/scsi'
242 DIR_MODULES='/proc/modules' #
243 DIR_MOUNTS='/proc/mounts'
244 DIR_PARTITIONS='/proc/partitions' #
245 DIR_IFCONFIG='/sbin/ifconfig'
246
247 ### Variable initializations: constants
248 DCOPOBJ="default"
249 DEBUG=0 # Set debug levels from 1-10 (8-10 trigger logging levels)
250 # Debug Buffer Index, index into a debug buffer storing debug messages until inxi is 'all up'
251 DEBUG_BUFFER_INDEX=0
252 ## note: the debugger rerouting to /dev/null has been moved to the end of the get_parameters function
253 ## so -@[number] debug levels can be set if there is a failure, otherwise you can't even see the errors
254
255 # Defaults to 2, make this 1 for normal, 0 for no colorcodes at all. Set to any other valid scheme you like.
256 # Same as runtime parameter.
257 DEFAULT_SCHEME=2
258 # Default indentation level
259 INDENT=10
260
261 # logging eval variables, start and end function: Insert to LOGFS LOGFE when debug level >= 8
262 LOGFS_STRING='log_function_data fs $FUNCNAME "$( echo $@ )"'
263 LOGFE_STRING='log_function_data fe $FUNCNAME'
264 LOGFS=''
265 LOGFE=''
266 # uncomment for debugging from script start
267 # LOGFS=$LOGFS_STRING
268 # LOGFE=$LOGFE_STRING
269
270 # default to false, no konversation found, 1 is native konvi (qt3/KDE3) script mode, 2 is /cmd inxi start,
271 ##      3 is Konversation > 1.2 (qt4/KDE4) 
272 KONVI=0
273 # NO_CPU_COUNT=0        # Wether or not the string "dual" or similar is found in cpuinfo output. If so, avoid dups.
274 # This is a variable that controls how many parameters inxi will parse in a /proc/<pid>/cmdline file before stopping.
275 PARAMETER_LIMIT=30
276 SCHEME=0 # set default scheme
277 # SHOW_IRC=1 to avoid showing the irc client version number, or SHOW_IRC=0 to disable client information completely.
278 SHOW_IRC=2
279 # Verbosity level defaults to 0, this can also be set with -v0, -v2, -v3, etc as a parameter.
280 VERBOSITY_LEVEL=0
281 # Supported number of verbosity levels, including 0
282 VERBOSITY_LEVELS=5
283
284 # Clear nullglob, because it creates unpredictable situations with IFS=$'\n' ARR=($VAR) IFS="$ORIGINAL_IFS"
285 # type constructs. Stuff like [rev a1] is now seen as a glob expansion pattern, and fails, and
286 # therefore results in nothing.
287 shopt -u nullglob
288 ## info on bash built in: $IFS - http://tldp.org/LDP/abs/html/internalvariables.html
289 # Backup the current Internal Field Separator
290 ORIGINAL_IFS="$IFS"
291
292 # These two determine separators in single line output, to force irc clients not to break off sections
293 SEP1='-'
294 SEP2='~'
295
296 ### Script names/paths - must be non root writable
297 SCRIPT_DATA_DIR="$HOME/.inxi"
298 LOG_FILE="$SCRIPT_DATA_DIR/inxi.log"
299 LOG_FILE_1="$SCRIPT_DATA_DIR/inxi.1.log"
300 LOG_FILE_2="$SCRIPT_DATA_DIR/inxi.2.log"
301 SCRIPT_NAME="inxi"
302 SCRIPT_PATH=""                  #filled-in in Main
303 SCRIPT_VERSION_NUMBER=""        #filled-in in Main
304 SCRIPT_DOWNLOAD='http://inxi.googlecode.com/svn/trunk/'
305 SCRIPT_DOWNLOAD_BRANCH_1='http://inxi.googlecode.com/svn/branches/one/'
306 SCRIPT_DOWNLOAD_BRANCH_2='http://inxi.googlecode.com/svn/branches/two/'
307 SCRIPT_DOWNLOAD_BRANCH_3='http://inxi.googlecode.com/svn/branches/three/'
308 SCRIPT_DOWNLOAD_BRANCH_4='http://inxi.googlecode.com/svn/branches/four/'
309 SCRIPT_DOWNLOAD_DEV='http://smxi.org/test/'
310 KONVI_CFG="konversation/scripts/$SCRIPT_NAME.conf" # relative path to $(kde-config --path data)
311
312 ### Script Localization
313 # Make sure every program speaks English.
314 LC_ALL="C"
315 export LC_ALL
316
317 ### Output Colors
318 # A more elegant way to have a scheme that doesn't print color codes (neither ANSI nor mIRC) at all. See below.
319 unset EMPTY
320 #              DGREY    BLACK    RED      DRED     GREEN    DGREEN   YELLOW   DYELLOW
321 ANSI_COLORS="\e[1;30m \e[0;30m \e[1;31m \e[0;31m \e[1;32m \e[0;32m \e[1;33m \e[0;33m"
322 IRC_COLORS="   \x0314   \x0301   \x0304   \x0305   \x0309   \x0303   \x0308   \x0307"
323 #                            BLUE     DBLUE    MAGENTA  DMAGENTA CYAN     DCYAN    WHITE    GREY     NORMAL
324 ANSI_COLORS="$ANSI_COLORS \e[1;34m \e[0;34m \e[1;35m \e[0;35m \e[1;36m \e[0;36m \e[1;37m \e[0;37m \e[0;37m"
325 IRC_COLORS=" $IRC_COLORS    \x0312   \x0302   \x0313   \x0306   \x0311   \x0310   \x0300   \x0315   \x03"
326
327 #ANSI_COLORS=($ANSI_COLORS); IRC_COLORS=($IRC_COLORS)
328 A_COLORS_AVAILABLE=( DGREY BLACK RED DRED GREEN DGREEN YELLOW DYELLOW BLUE DBLUE MAGENTA DMAGENTA CYAN DCYAN WHITE GREY NORMAL )
329 # See above for notes on EMPTY
330 A_COLOR_SCHEMES=( EMPTY,EMPTY,EMPTY NORMAL,NORMAL,NORMAL BLUE,NORMAL,NORMAL GREEN,YELLOW,NORMAL DYELLOW,NORMAL,NORMAL CYAN,BLUE,NORMAL RED,NORMAL,NORMAL GREEN,NORMAL,NORMAL YELLOW,NORMAL,NORMAL GREEN,DGREEN,NORMAL BLUE,RED,NORMAL BLUE,NORMAL,RED YELLOW,WHITE,GREEN BLUE,NORMAL,GREEN DCYAN,NORMAL,DMAGENTA )
331 ## Actual color variables
332 C1=''
333 C2=''
334 CN=''
335
336 ### Distro Data
337 # In cases of derived distros where the version file of the base distro can also be found under /etc,
338 # the derived distro's version file should go first. (Such as with Sabayon / Gentoo)
339 DISTROS_DERIVED="antix-version kanotix-version knoppix-version mandrake-release pardus-release sabayon-release sidux-version turbolinux-release zenwalk-version"
340 # debian_version excluded from DISTROS_PRIMARY so Debian can fall through to /etc/issue detection. Same goes for Ubuntu.
341 DISTROS_EXCLUDE_LIST="debian_version ubuntu_version"
342 DISTROS_PRIMARY="gentoo-release redhat-release slackware-version SuSE-release"
343 DISTROS_LSB_GOOD="mandrake-release mandriva-release mandrakelinux-release"
344 ## Distros with known problems
345 # DSL (Bash 2.05b: grep -m doesn't work; arrays won't work) --> unusable output
346 # Puppy Linux 4.1.2 (Bash 3.0: arrays won't work) --> works partially
347
348 ### Bans Data
349 # Precede a banword with $'\2' to prevent it from being subject to automated escaping by the make_ban_lists routine
350 # $'\1' gets weird results :
351 # user@host $ ARR=($'\x01'"one two" three four); echo ${ARR[0]} | hd -v
352 # 00000000  01 01 6f 6e 65 20 74 77  6f 0a                    |..one two.|
353 A_NORMAL_BANS=( corporation communications gmbh technologies technology group $'\2'"\<ltd\>" ltd. $'\2'"\<inc\>" inc. $'\2'\<co\> co. "(tm)" "(r)" "®" $'\2'"\(rev ..\)" )
354 A_CPU_BANS=( @ cpu deca 'dual core' dual-core 'tri core' tri-core 'quad core' quad-core ennea genuine hepta hexa multi octa penta 'processor' processor single triple $'\2'"[0-9.]+ *[MmGg][Hh][Zz]" )
355 # after processing, the ban arrays will be put into these:
356 BAN_LIST_NORMAL=''
357 BAN_LIST_CPU=''
358
359 # WARNING: In the main part below (search for 'KONVI')
360 # there's a check for Konversation-specific config files.
361 # Any one of these can override the above if inxi is run
362 # from Konversation!
363
364 ########################################################################
365 #### MAIN: Where it all begins
366 ########################################################################
367 main()
368 {
369         eval $LOGFS
370         
371         # This function just initializes variables
372         initialize_script_data
373
374         # Check for dependencies BEFORE running ANYTHING else except above functions
375         # Not all distro's have these depends installed by default
376         check_script_depends
377         check_script_suggested_apps
378
379         # first init function must be set first for colors etc. Remember, no debugger
380         # stuff works on this function unless you set the debugging flag manually.
381         # Debugging flag -@ [number] will not work until get_parameters runs.
382         
383         ### Only continue if depends ok
384         SCRIPT_PATH=$( dirname $0 )
385         SCRIPT_VERSION_NUMBER=$( grep -im 1 'version:' $SCRIPT_PATH/$SCRIPT_NAME | gawk '{print $3}' )
386         
387         ### Source global config overrides
388         if [[ -s /etc/$SCRIPT_NAME.conf ]];then
389                 source /etc/$SCRIPT_NAME.conf
390         fi
391         # Source user config overrides, ~/.inxi/inxi.conf
392         if [[ -s $HOME/.$SCRIPT_NAME/$SCRIPT_NAME.conf ]];then
393                 source $HOME/.$SCRIPT_NAME/$SCRIPT_NAME.conf
394         fi
395
396         ## this needs to run before the KONVI stuff is set below
397         ## Konversation 1.2 apparently does not like the $PPID test in get_start_client
398         ## So far there is no known way to detect if qt4_konvi is the parent process
399         ## this method will infer qt4_konvi as parent
400         
401         get_start_client
402
403         # note: this only works if it's run from inside konversation as a script builtin or something
404         # only do this if inxi has been started as a konversation script, otherwise bypass this 
405 #       KONVI=3 ## for testing puroses
406         ## 
407         if [[ $KONVI -eq 1 || $KONVI -eq 3 ]];then
408                 
409                 if [[ $KONVI -eq 1 ]]; then ## dcop Konversation (ie 1.x < 1.2(qt3))    
410                         DCPORT="$1"
411                         DCSERVER="$2"
412                         DCTARGET="$3"
413                         shift 3
414                 elif [[ $KONVI -eq 3 ]]; then ## dbus Konversation (> 1.2 (qt4))
415                         DCSERVER="$1" ##dbus testing
416                         DCTARGET="$2" ##dbus testing
417                         shift 2
418                 fi
419
420                 # The section below is on request of Argonel from the Konversation developer team:
421                 # it sources config files like $HOME/.kde/share/apps/konversation/scripts/inxi.conf
422                 IFS=":"
423                 for kde_config in $( kde-config --path data )
424                 do
425                         if [[ -r ${kde_config}${KONVI_CFG} ]];then
426                                 source "${kde_config}${KONVI_CFG}"
427                                 break
428                         fi
429                 done
430                 IFS="$ORIGINAL_IFS"
431         fi
432
433         ## leave this for debugging dcop stuff if we get that working
434         #       print_screen_output "DCPORT: $DCPORT"
435         #       print_screen_output "DCSERVER: $DCSERVER"
436         #       print_screen_output "DCTARGET: $DCTARGET"
437
438         # "$@" passes every parameter separately quoted, "$*" passes all parameters as one quoted parameter.
439         # must be here to allow debugger and other flags to be set.
440         get_parameters "$@"
441
442         # If no colorscheme was set in the parameter handling routine, then set the default scheme
443         if [[ $COLOR_SCHEME_SET != 'true' ]];then
444                 set_color_scheme "$DEFAULT_SCHEME"
445         fi
446
447         # all the pre-start stuff is in place now
448         B_SCRIPT_UP='true'
449         script_debugger "Debugger: $SCRIPT_NAME is up and running..."
450
451         # then create the output
452         print_it_out
453
454         ## last steps
455         if [[ $B_RUNNING_IN_SHELL == 'true' && $SCHEME -gt 0 ]];then
456                 echo -n "\e[0m"
457         fi
458         eval $LOGFE
459         # weechat's executor plugin forced me to do this, and rightfully so, because else the exit code
460         # from the last command is taken..
461         exit 0
462 }
463
464 #### -------------------------------------------------------------------
465 #### basic tests: set script data, booleans, PATH
466 #### -------------------------------------------------------------------
467
468 # Set PATH data so we can access all programs as user. Set BAN lists.
469 # initialize some boleans, these directories are used throughout the script
470 # some apps are used for extended functions any directory used, should be
471 # checked here first.
472 # No args taken.
473 initialize_script_data()
474 {
475         eval $LOGFS
476         local path='' sys_path='' added_path='' b_path_found=''
477         # Extra path variable to make execute failures less likely, merged below
478         local extra_paths="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
479
480         # Fallback paths put into $extra_paths; This might, among others, help on gentoo.
481         # Now, create a difference of $PATH and $extra_paths and add that to $PATH:
482         IFS=":"
483         for path in $extra_paths
484         do
485                 b_path_found='false'
486                 for sys_path in $PATH
487                 do
488                         if [[ $path == $sys_path ]];then
489                                 b_path_found='true'
490                         fi
491                 done
492                 if [[ $b_path_found == 'false' ]];then
493                         added_path="$added_path:$path"
494                 fi
495         done
496
497         IFS="$ORIGINAL_IFS"
498         PATH="${PATH}${added_path}"
499         ##echo "PATH='$PATH'"
500         ##/bin/sh -c 'echo "PATH in subshell=\"$PATH\""'
501
502         # Do this after sourcing of config overrides so user can customize banwords
503         BAN_LIST_NORMAL=$( make_ban_lists "${A_NORMAL_BANS[@]}" ) # Contrary to my previous belief, "${ARR[@]}" passes a quoted list, not one string
504         BAN_LIST_CPU=$( make_ban_lists "${A_CPU_BANS[@]}" )
505         ##echo "BAN_LIST_NORMAL='$BAN_LIST_NORMAL'"
506
507         # now set the script BOOLEANS for files required to run features
508         if [[ -d "/proc/" ]];then
509                 B_PROC='true'
510         else
511                 error_handler 6
512         fi
513
514         if [[ -e $DIR_CPUINFO ]]; then
515                 B_CPUINFO='true'
516         fi
517
518         if [[ -e $DIR_MEMINFO ]];then
519                 B_MEMINFO='true'
520         fi
521
522         if [[ -e $DIR_ASOUND_DEVICE ]];then
523                 B_ASOUND_CARDS='true'
524         fi
525
526         if [[ -e $DIR_ASOUND_VERSION ]];then
527                 B_ASOUND_VERSION='true'
528         fi
529
530         if [[ -f $DIR_LSB_RELEASE ]];then
531                 B_LSB_DIR='true'
532         fi
533
534         if [[ -e $DIR_SCSI ]];then
535                 B_SCSI_DIR='true'
536         fi
537
538         # lack of ifconfig will throw an error only upon it's usage
539         if [[ -x ifconfig ]]; then
540                 B_IFCONFIG='true'
541                 DIR_IFCONFIG='ifconfig' # change from full path to use $PATH
542         elif [[ -x $DIR_IFCONFIG ]];then
543                 B_IFCONFIG='true'
544         else
545                 A_INTERFACES_DATA=( "Interfaces tool requires missing app: $DIR_IFCONFIG" )
546         fi
547
548         if [[ -n $DISPLAY ]];then
549                 B_X_RUNNING='true'
550         fi
551
552         if [[ -e $DIR_MODULES ]];then
553                 B_MODULES_DIR='true'
554         fi
555
556         if [[ -e $DIR_MOUNTS ]];then
557                 B_MOUNTS_DIR='true'
558         fi
559
560         if [[ -e $DIR_PARTITIONS ]];then
561                 B_PARTITIONS_DIR='true'
562         fi
563         # gfx output will require this flag
564         if [[ $( whoami ) == 'root' ]];then
565                 B_ROOT='true'
566         fi
567         eval $LOGFE
568 }
569
570 # No args taken.
571 check_script_suggested_apps()
572 {
573         eval $LOGFS
574         local bash_array_test=( "one" "two" )
575
576         # check for array ability of bash, this is only good for the warning at this time
577         # the boolean could be used later
578         # bash version 2.05b is used in DSL
579         # bash version 3.0 is used in Puppy Linux; it has a known array bug <reference to be placed here>
580         # versions older than 3.1 don't handle arrays
581         # distro's using below 2.05b are unknown, released in 2002
582         if [[ ${bash_array_test[1]} -eq "two" ]];then
583                 B_BASH_ARRAY='true'
584         else
585                 script_debugger "Suggestion: update to Bash v3.1 for optimal inxi output"
586         fi
587         eval $LOGFE
588 }
589
590 # Determine if any of the absolutely necessary tools are absent
591 # No args taken.
592 check_script_depends()
593 {
594         eval $LOGFS
595         local app_name='' app_data=''
596         # bc removed from deps for now
597         local depends="df free gawk grep hostname lspci ps readlink tr uname uptime wc"
598         local x_apps="xrandr xdpyinfo glxinfo"
599
600         if [[ $B_X_RUNNING == 'true' ]];then
601                 for app_name in $x_apps
602                 do
603                         app_data=$( type -p $app_name )
604                         if [[ -z $app_data ]];then
605                                 script_debugger "Resuming in non X mode: $app_name not found in path"
606                                 B_X_RUNNING='false'
607                                 break
608                         fi
609                 done
610         fi
611
612         app_name=''
613
614         for app_name in $depends
615         do
616                 app_data=$( type -p $app_name )
617                 if [[ -z $app_data ]];then
618                         error_handler 5 "$app_name"
619                 fi
620         done
621         eval $LOGFE
622 }
623
624 ## note: this is now running inside each gawk sequence directly to avoid exiting gawk
625 ## looping in bash through arrays, then re-entering gawk to clean up, then writing back to array
626 ## in bash. For now I'll leave this here because there's still some interesting stuff to get re methods
627 # Enforce boilerplate and buzzword filters
628 # args: $1 - BAN_LIST_NORMAL/BAN_LIST_CPU; $2 - string to sanitize
629 sanitize_characters()
630 {
631         eval $LOGFS
632         # Cannot use strong quotes to unquote a string with pipes in it!
633         # bash will interpret the |'s as usual and try to run a subshell!
634         # Using weak quotes instead, or use '"..."'
635         echo "$2" | gawk "
636         BEGIN {
637                 IGNORECASE=1
638         }
639         {
640                 gsub(/${!1}/,\"\")
641                 gsub(/ [ ]+/,\" \")    ## ([ ]+) with (space)
642                 gsub(/^ +| +$/,\"\")   ## (pipe char) with (nothing)
643                 print                  ## prints (returns) cleaned input
644         }"
645         eval $LOGFE
646 }
647
648 # Filter boilerplate & buzzwords.
649 # args: $1 - quoted: "$@" array of ban terms
650 make_ban_lists()
651 {
652         eval $LOGFS
653         local ban_list=''
654         # Iterate over $@
655         ## note: this is a weird, non-intuitive method, needs some documentation or rewriting
656         ## if you declare ban_string it stops working, have to read up on this
657         for ban_string
658         do
659                 # echo "term=\"$ban_string\"" # >&2
660                 if [[ ${ban_string:0:1} = $'\2' ]];then
661                         ban_list="${ban_list}${ban_list+|}${ban_string:1:${#ban_string}-1}"
662                 else
663                         # Automatically escapes [ ] ( ) . and +
664                         ban_list="${ban_list}${ban_list+|}$( echo "$ban_string" | gawk '{
665                                 gsub(/([\[\]+().])/,"\\\\&")
666                                 print
667                         }' )"
668                 fi
669         done
670
671         echo "$ban_list"
672         eval $LOGFS
673 }
674 # make_ban_lists "${A_CPU_BANS[@]}";exit
675
676 # Set the colorscheme
677 # args: $1 = <scheme number>|<"none">
678 set_color_scheme()
679 {
680         eval $LOGFS
681         local i='' script_colors='' color_codes=''
682
683         if [[ $1 -ge ${#A_COLOR_SCHEMES[@]} ]];then
684                 set -- 1
685         fi
686         # Set a global variable to allow checking for chosen scheme later
687         SCHEME="$1"
688         if [[ $B_RUNNING_IN_SHELL == 'true' ]];then
689                 color_codes=( $ANSI_COLORS )
690         else
691                 color_codes=( $IRC_COLORS )
692         fi
693         for (( i=0; i < ${#A_COLORS_AVAILABLE[@]}; i++ ))
694         do
695                 eval "${A_COLORS_AVAILABLE[i]}=\"${color_codes[i]}\""
696         done
697         IFS=","
698         script_colors=( ${A_COLOR_SCHEMES[$1]} )
699         IFS="$ORIGINAL_IFS"
700         # then assign the colors globally
701         C1="${!script_colors[0]}"
702         C2="${!script_colors[1]}"
703         CN="${!script_colors[2]}"
704         # ((COLOR_SCHEME++)) ## note: why is this? ##
705         eval $LOGFE
706 }
707
708 ########################################################################
709 #### UTILITY FUNCTIONS
710 ########################################################################
711
712 #### -------------------------------------------------------------------
713 #### error handler, debugger, script updater
714 #### -------------------------------------------------------------------
715
716 # Error handling
717 # args: $1 - error number; $2 - optional, extra information
718 error_handler()
719 {
720         eval $LOGFS
721         local error_message=''
722
723         # assemble the error message
724         case $1 in
725                 2)      error_message="large flood danger, debug buffer full!"
726                         ;;
727                 3)      error_message="unsupported color scheme number: $2"
728                         ;;
729                 4)      error_message="unsupported verbosity level: $2"
730                         ;;
731                 5)      error_message="dependency not met: $2 not found in path"
732                         ;;
733                 6)      error_message="/proc not found! Quitting..."
734                         ;;
735                 7)      error_message="One of the options you entered in your script parameters: $2\nIs not supported. For supported options, check the help menu: $SCRIPT_NAME -h"
736                         ;;
737                 8)      error_message="the self-updater failed, wget exited with error: $2.\nYou probably need to be root.\nHint, to make for easy updates without being root, do: chown <user name> $SCRIPT_PATH/$SCRIPT_NAME"
738                         ;;
739                 9)      error_message="unsupported debugging level: $2"
740                         ;;
741                 10)
742                         error_message="the alt download url you provided: $2\nappears to be wrong, download aborted. Please note, the url\nneeds to end in /, without $SCRIPT_NAME, like: http://yoursite.com/downloads/"
743                         ;;
744                 11)
745                         error_message="unsupported testing option argument: -! $2"
746                         ;;
747                 12)
748                         error_message="the svn branch download url: $2\nappears to be empty currently. Make sure there is an actual svn branch version\nactive before you try this again. Check http://code.google.com/p/inxi\nto verify the branch status."
749                         ;;
750                 *)      error_message="error unknown: $@"
751                         set -- 99
752                         ;;
753         esac
754         # then print it and exit
755         print_screen_output "Error $1: $error_message"
756         eval $LOGFE
757         exit $1
758 }
759
760 # prior to script up set, pack the data into an array
761 # then we'll print it out later.
762 # args: $1 - $@ debugging string text
763 script_debugger()
764 {
765         eval $LOGFS
766         if [[ $B_SCRIPT_UP == 'true' ]];then
767                 # only return if debugger is off and no pre start up errors have occured
768                 if [[ $DEBUG -eq 0 && $DEBUG_BUFFER_INDEX -eq 0 ]];then
769                         return 0
770                 # print out the stored debugging information if errors occured
771                 elif [[ $DEBUG_BUFFER_INDEX -gt 0 ]];then
772                         for (( DEBUG_BUFFER_INDEX=0; DEBUG_BUFFER_INDEX < ${#A_DEBUG_BUFFER[@]}; DEBUG_BUFFER_INDEX++ ))
773                         do
774                                 print_screen_output "${A_DEBUG_BUFFER[$DEBUG_BUFFER_INDEX]}"
775                         done
776                         DEBUG_BUFFER_INDEX=0
777                 fi
778                 # or print out normal debugger messages if debugger is on
779                 if [[ $DEBUG -gt 0 ]];then
780                         print_screen_output "$1"
781                 fi
782         else
783                 if [[ $B_DEBUG_FLOOD == 'true' && $DEBUG_BUFFER_INDEX -gt 10 ]];then
784                         error_handler 2
785                 # this case stores the data for later printout, will print out only
786                 # at B_SCRIPT_UP == 'true' if array index > 0
787                 else
788                         A_DEBUG_BUFFER[$DEBUG_BUFFER_INDEX]="$1"
789                         # increment count for next pre script up debugging error
790                         (( DEBUG_BUFFER_INDEX++ ))
791                 fi
792         fi
793         eval $LOGFE
794 }
795
796 # NOTE: no logging available until get_parameters is run, since that's what sets logging
797 # in order to trigger earlier logging manually set B_USE_LOGGING to true in top variables.
798 # $1 alone: logs data; $2 with or without $3 logs func start/end.
799 # $1 type (fs/fe/cat/raw) or logged data; [$2 is $FUNCNAME; [$3 - function args]]
800 log_function_data()
801 {
802         if [ "$B_USE_LOGGING" == 'true' ];then
803                 local logged_data='' spacer='   ' line='----------------------------------------'
804                 case $1 in
805                         fs)
806                                 logged_data="Function: $2 - Primary: Start"
807                                 if [ -n "$3" ];then
808                                         logged_data="$logged_data\n${spacer}Args: $3"
809                                 fi
810                                 spacer=''
811                                 ;;
812                         fe)
813                                 logged_data="Function: $2 - Primary: End"
814                                 spacer=''
815                                 ;;
816                         cat)
817                                 if [[ $B_LOG_FULL_DATA == 'true' ]];then
818                                         logged_data="\n$line\nFull file data: cat $2\n\n$( cat $2 )\n$line\n"
819                                         spacer=''
820                                 fi
821                                 ;;
822                         raw)
823                                 if [[ $B_LOG_FULL_DATA == 'true' ]];then
824                                         logged_data="\n$line\nRaw system data:\n\n$2\n$line\n"
825                                         spacer=''
826                                 fi
827                                 ;;
828                         *)
829                                 logged_data="$1"
830                                 ;;
831                 esac
832                 # Create any required line breaks and strip out escape color code, either ansi (case 1)or irc (case 2).
833                 # This pattern doesn't work for irc colors, if we need that someone can figure it out
834                 if [[ -n $logged_data ]];then
835                         if [[ $B_LOG_COLORS != 'true' ]];then
836                                 echo -e "${spacer}$logged_data" | sed -r 's/\x1b\[[0-9]{1,2}(;[0-9]{1,2}){0,2}m//g' >> $LOG_FILE
837                         else
838                                 echo -e "${spacer}$logged_data" >> $LOG_FILE
839                         fi
840                 fi
841         fi
842 }
843
844 # called in the initial -@ 10 script args setting so we can get logging as soon as possible
845 # will have max 3 files, inxi.log, inxi.1.log, inxi.2.log
846 create_rotate_logfiles()
847 {
848         if [[ ! -d $SCRIPT_DATA_DIR ]];then
849                 mkdir $SCRIPT_DATA_DIR
850         fi
851         # do the rotation if logfile exists
852         if [[ -f $LOG_FILE ]];then
853                 # copy if present second to third
854                 if [[ -f $LOG_FILE_1 ]];then
855                         mv -f $LOG_FILE_1 $LOG_FILE_2
856                 fi
857                 # then copy initial to second
858                 mv -f $LOG_FILE $LOG_FILE_1
859         fi
860         # now create the logfile
861         touch $LOG_FILE
862         # and echo the start data
863         echo "=========================================================" >> $LOG_FILE
864         echo "START $SCRIPT_NAME LOGGING:"                               >> $LOG_FILE
865         echo "Script started: $( date +%Y-%m-%d-%H:%M:%S )"              >> $LOG_FILE
866         echo "=========================================================" >> $LOG_FILE
867 }
868
869 # args: $1 - download url, not including file name; $2 - string to print out
870 # note that $1 must end in / to properly construct the url path
871 script_self_updater()
872 {
873         eval $LOGFS
874         local wget_error=0
875         print_screen_output "Starting $SCRIPT_NAME self updater."
876         print_screen_output "Currently running $SCRIPT_NAME version number: $SCRIPT_VERSION_NUMBER"
877         print_screen_output "Updating $SCRIPT_NAME in $SCRIPT_PATH using $2 as download source..."
878         # first test if path is good, need to make sure it's good because we're -O overwriting file
879         wget -q --spider $1$SCRIPT_NAME || wget_error=$?
880         # then do the actual download
881         if [[ $wget_error -eq 0 ]];then
882                 wget -q -O $SCRIPT_PATH/$SCRIPT_NAME $1$SCRIPT_NAME || wget_error=$?
883                 if [[ $wget_error -eq 0 ]];then
884                         SCRIPT_VERSION_NUMBER=$( grep -im 1 'version:' $SCRIPT_PATH/$SCRIPT_NAME | gawk '{print $3}' )
885                         print_screen_output "Successfully updated to $2 version: $SCRIPT_VERSION_NUMBER"
886                         print_screen_output "To run the new version, just start $SCRIPT_NAME again."
887                         exit 0
888                 fi
889         fi
890         # now run the error handlers on any wget failure
891         if [[ $wget_error -gt 0 ]];then
892                 if [[ $2 == 'svn server' ]];then
893                         error_handler 8 "$wget_error"
894                 elif [[ $2 == 'alt server' ]];then
895                         error_handler 10 "$1"
896                 else
897                         error_handler 12 "$1"
898                 fi
899         fi
900         eval $LOGFS
901 }
902
903 #### -------------------------------------------------------------------
904 #### print / output cleaners
905 #### -------------------------------------------------------------------
906
907 # inxi speaks through here. When run by Konversation script alias mode, uses DCOP
908 # for dcop to work, must use 'say' operator, AND colors must be evaluated by echo -e
909 # note: dcop does not seem able to handle \n so that's being stripped out and replaced with space.
910 print_screen_output()
911 {
912         eval $LOGFS
913         # the double quotes are needed to avoid losing whitespace in data when certain output types are used
914         local print_data="$( echo -e "$1" )"
915
916         # just using basic debugger stuff so you can tell which thing is printing out the data. This
917         # should help debug kde 4 konvi issues when that is released into sid, we'll see. Turning off
918         # the redundant debugger output which as far as I can tell does exactly nothing to help debugging.
919         if [[ $DEBUG -gt 5 ]];then
920                 if [[ $KONVI -eq 1 ]];then
921                         # konvi doesn't seem to like \n characters, it just prints them literally
922                         # print_data="$( tr '\n' ' ' <<< "$print_data" )"
923                         # dcop "$DCPORT" "$DCOPOBJ" say "$DCSERVER" "$DCTARGET" "konvi='$KONVI' saying : '$print_data'"
924                         print_data="KP-$KONVI: $print_data"
925                 elif [[ $KONVI -eq 2 ]];then
926                         # echo "konvi='$KONVI' saying : '$print_data'"
927                         print_data="KP-$KONVI: $print_data"
928                 else
929                         # echo "printing out: '$print_data'"
930                         print_data="P: $print_data"
931                 fi
932         fi
933
934         if [[ $KONVI -eq 1 ]]; then ## dcop Konversation (<= 1.1 (qt3))
935                 # konvi doesn't seem to like \n characters, it just prints them literally
936                 $print_data="$( tr '\n' ' ' <<< "$print_data" )"
937                 dcop "$DCPORT" "$DCOPOBJ" say "$DCSERVER" "$DCTARGET" "$print_data"
938
939         elif [[ $KONVI -eq 3 ]]; then ## dbus Konversation (> 1.2 (qt4))
940                 qdbus org.kde.konversation /irc say "$DCSERVER" "$DCTARGET" "$print_data"
941
942 #       elif [[ $IRC_CLIENT == 'X-Chat' ]]; then
943 #               qdbus org.xchat.service print "$print_data\n"
944
945         else
946                 # the -n is needed to avoid double spacing of output in terminal
947                 echo -ne "$print_data\n"
948         fi
949         eval $LOGFE
950 }
951
952 ## this handles all verbose line construction with indentation/line starter
953 ## args: $1 - null (, actually: " ") or line starter; $2 - line content
954 create_print_line()
955 {
956         eval $LOGFS
957         printf "${C1}%-${INDENT}s${C2} %s" "$1" "$2"
958         eval $LOGFE
959 }
960
961 # this removes newline and pipes.
962 # args: $1 - string to clean
963 remove_erroneous_chars()
964 {
965         eval $LOGFS
966         ## RS is input record separator
967         ## gsub is substitute;
968         gawk '
969         BEGIN {
970                 RS=""
971         }
972         {
973                 gsub(/\n$/,"")         ## (newline; end of string) with (nothing)
974                 gsub(/\n/," ");        ## (newline) with (space)
975                 gsub(/^ *| *$/, "")    ## (pipe char) with (nothing)
976                 gsub(/  +/, " ")       ## ( +) with (space)
977                 gsub(/ [ ]+/, " ")     ## ([ ]+) with (space)
978                 gsub(/^ +| +$/, "")    ## (pipe char) with (nothing)
979                 printf $0
980         }' "$1"      ## prints (returns) cleaned input
981         eval $LOGFE
982 }
983
984 #### -------------------------------------------------------------------
985 #### parameter handling, print usage functions.
986 #### -------------------------------------------------------------------
987
988 # Get the parameters. Note: standard options should be lower case, advanced or testing, upper
989 # args: $1 - full script startup args: $@
990 get_parameters()
991 {
992         eval $LOGFS
993         local opt='' wget_test='' update_flags='U!:'
994         local use_short='true' # this is needed to trigger short output, every v/d/F/line trigger sets this false
995
996         # <comment here.  what is this for?>
997         if [[ $B_ALLOW_UPDATE == 'false' ]];then
998                 update_flags=''
999         fi
1000
1001         # the short form only runs if no args output args are used
1002         # no need to run through these if there are no args
1003         if [[ -n $1 ]];then
1004                 while getopts Ac:CdDfFGhHiIlNpPSuv:Vx%@:${update_flags} opt
1005                 do
1006                         case $opt in
1007                         A)      B_SHOW_AUDIO='true'
1008                                 use_short='false'
1009                                 ;;
1010                         c)      if [[ -n $( egrep '^[0-9][0-9]?$' <<< $OPTARG ) ]];then
1011                                         COLOR_SCHEME_SET='true'
1012                                         ## note: not sure about this, you'd think user values should be overridden, but
1013                                         ## we'll leave this for now
1014                                         if [[ -z $COLOR_SCHEME ]];then
1015                                                 set_color_scheme "$OPTARG"
1016                                         fi
1017                                 else
1018                                         error_handler 3 "$OPTARG"
1019                                 fi
1020                                 ;;
1021                         C)      B_SHOW_CPU='true'
1022                                 use_short='false'
1023                                 ;;
1024                         d)      VERBOSITY_LEVEL=1
1025                                 use_short='false'
1026                                 ;;
1027                         D)      B_SHOW_DISK='true'
1028                                 use_short='false'
1029                                 ;;
1030                         f)      B_SHOW_CPU='true'
1031                                 B_CPU_FLAGS_FULL='true'
1032                                 use_short='false'
1033                                 ;;
1034                         F)      VERBOSITY_LEVEL=$VERBOSITY_LEVELS
1035                                 B_EXTRA_DATA='true'
1036                                 B_SHOW_DISK='true'
1037                                 B_SHOW_PARTITIONS='true'
1038                                 B_SHOW_AUDIO='true'
1039                                 use_short='false'
1040                                 ;;
1041                         G)      B_SHOW_GRAPHICS='true'
1042                                 use_short='false'
1043                                 ;;
1044                         i)      B_SHOW_IP='true'
1045                                 B_SHOW_NETWORK='true'
1046                                 use_short='false'
1047                                 ;;
1048                         I)      B_SHOW_INFO='true'
1049                                 use_short='false'
1050                                 ;;
1051                         l)      B_SHOW_LABELS='true'
1052                                 B_SHOW_PARTITIONS='true'
1053                                 use_short='false'
1054                                 ;;
1055                         N)      B_SHOW_NETWORK='true'
1056                                 use_short='false'
1057                                 ;;
1058                         p)      B_SHOW_PARTITIONS_FULL='true'
1059                                 B_SHOW_PARTITIONS='true'
1060                                 use_short='false'
1061                                 ;;
1062                         P)      B_SHOW_PARTITIONS='true'
1063                                 use_short='false'
1064                                 ;;
1065                         S)      B_SHOW_SYSTEM='true'
1066                                 use_short='false'
1067                                 ;;
1068                         u)      B_SHOW_UUIDS='true'
1069                                 B_SHOW_PARTITIONS='true'
1070                                 use_short='false'
1071                                 ;;
1072                         v)      if [[ -n $( egrep "^[0-9][0-9]?$" <<< $OPTARG ) && $OPTARG -le $VERBOSITY_LEVELS ]];then
1073                                         VERBOSITY_LEVEL="$OPTARG"
1074                                         if [[ $OPTARG -gt 0 ]];then
1075                                                 use_short='false'
1076                                         fi
1077                                 else
1078                                         error_handler 4 "$OPTARG"
1079                                 fi
1080                                 ;;
1081                         U)      script_self_updater "$SCRIPT_DOWNLOAD" 'svn server'
1082                                 ;;
1083                         V)      print_version_info
1084                                 exit 0
1085                                 ;;
1086                         x)      B_EXTRA_DATA='true'
1087                                 ;;
1088                         h)      show_options
1089                                 exit 0
1090                                 ;;
1091                         H)      show_options 'full'
1092                                 exit 0
1093                                 ;;
1094                         ## debuggers and testing tools
1095                         %)      B_HANDLE_CORRUPT_DATA='true'
1096                                 ;;
1097                         @)      if [[ -n $( egrep "^([1-9]|10)$" <<< $OPTARG ) ]];then
1098                                         DEBUG=$OPTARG
1099                                         exec 2>&1
1100                                         # switch on logging only for -@ 8-10
1101                                         if [[ $OPTARG -ge 8 ]];then
1102                                                 if [[ $OPTARG -eq 10 ]];then
1103                                                         B_LOG_COLORS='true'
1104                                                 elif [[ $OPTARG -eq 9 ]];then           
1105                                                         B_LOG_FULL_DATA='true'
1106                                                 fi
1107                                                 B_USE_LOGGING='true'
1108                                                 # pack the logging data for evals function start/end
1109                                                 LOGFS=$LOGFS_STRING
1110                                                 LOGFE=$LOGFE_STRING
1111                                                 create_rotate_logfiles # create/rotate logfiles before we do anything else
1112                                         fi
1113                                 else
1114                                         error_handler 9 "$OPTARG"
1115                                 fi
1116                                 ;;
1117                         !)      # test for various supported methods
1118                                 case $OPTARG in
1119                                         1)      B_TESTING_1='true'
1120                                                 ;;
1121                                         2)      B_TESTING_2='true'
1122                                                 ;;
1123                                         3)      B_TESTING_1='true'
1124                                                 B_TESTING_2='true'
1125                                                 ;;
1126                                         10)
1127                                                 script_self_updater "$SCRIPT_DOWNLOAD_DEV" 'dev server'
1128                                                 ;;
1129                                         11)
1130                                                 script_self_updater "$SCRIPT_DOWNLOAD_BRANCH_1" 'svn: branch one server'
1131                                                 ;;
1132                                         12)
1133                                                 script_self_updater "$SCRIPT_DOWNLOAD_BRANCH_2" 'svn: branch two server'
1134                                                 ;;
1135                                         13)
1136                                                 script_self_updater "$SCRIPT_DOWNLOAD_BRANCH_3" 'svn: branch three server'
1137                                                 ;;
1138                                         14)
1139                                                 script_self_updater "$SCRIPT_DOWNLOAD_BRANCH_4" 'svn: branch four server'
1140                                                 ;;
1141                                         http*)
1142                                                 script_self_updater "$OPTARG" 'alt server'
1143                                                 ;;
1144                                         *)      error_handler 11 "$OPTARG"
1145                                                 ;;
1146                                 esac
1147                                 ;;
1148                         *)      error_handler 7 "$1"
1149                                 ;;
1150                         esac
1151                 done
1152         fi
1153         ## this must occur here so you can use the debugging flag to show errors
1154         ## Reroute all error messages to the bitbucket (if not debugging)
1155         if [[ $DEBUG -eq 0 ]];then
1156                 exec 2>/dev/null
1157         fi
1158         #((DEBUG)) && exec 2>&1 # This is for debugging konversation
1159
1160         # after all the args have been processed, if no long output args used, run short output
1161         if [[ $use_short == 'true' ]];then
1162                 B_SHOW_SHORT_OUTPUT='true'
1163         fi
1164         eval $LOGFE
1165 }
1166
1167 ## print out help menu, not including Testing or Debugger stuff because it's not needed
1168 show_options()
1169 {
1170         local color_scheme_count=${#A_COLOR_SCHEMES[@]}
1171
1172         print_screen_output "$SCRIPT_NAME supports the following options. You can combine them, or list them"
1173         print_screen_output "one by one: Examples: $SCRIPT_NAME -v4 -c6 OR $SCRIPT_NAME -dDc 6"
1174         print_screen_output ""
1175         print_screen_output "If you start $SCRIPT_NAME with no arguments, it will show the short form."
1176         print_screen_output "The following options if used without -d or -v will show just that complete line:"
1177         print_screen_output "A,C,D,G,I,N,P,S - you can use these together to show just the lines you want to see."
1178         print_screen_output "If you use them with a -v level (or -d), it will show the full output for that line "
1179         print_screen_output "along with the output for the chosen verbosity level."
1180         print_screen_output "- - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
1181         print_screen_output "Output Control Options:"
1182         print_screen_output "-A  Show Audio/sound card information."
1183         print_screen_output "-c  Available color schemes. Scheme number is required."
1184         print_screen_output "    Supported schemes: 0-$color_scheme_count Example: $SCRIPT_NAME -c 11"
1185         print_screen_output "-C  Show full CPU output, including per CPU clockspeed."
1186         print_screen_output "-d  Default output verbosity level, same as: $SCRIPT_NAME -v 1"
1187         print_screen_output "-D  Show full hard Disk info, not only model, ie: /dev/sda ST380817AS 80.0GB."
1188         print_screen_output "-f  Show all cpu flags used, not just the short list. Not shown with -F to avoid spamming."
1189         print_screen_output "-F  Show Full output for $SCRIPT_NAME. Does not show extra verbose options like -f -u -l or -p"
1190         print_screen_output "-G  Show Graphic card information (card, x type, resolution, glx renderer, version)."
1191         print_screen_output "-i  Show Wan IP address, and shows local interfaces (requires ifconfig network tool)."
1192         print_screen_output "    Not shown with -F for user security reasons, you shouldn't paste your local/wan IP."
1193         print_screen_output "-I  Show Information: processes, uptime, memory, irc client, inxi version."
1194         print_screen_output "-l  Show partition labels. Default: short partition -P. For full -p output, use: -pl (or -plu)."
1195         print_screen_output "-N  Show Network card information."
1196         print_screen_output "-p  Show full partition information (-P plus all other detected partitions)."
1197         print_screen_output "-P  Show Partition information (shows what -v 4 would show, but without extra data)."
1198         print_screen_output "    Shows, if detected: / /boot /home /tmp /usr /var. Use -p to see all mounted partitions."
1199         print_screen_output "-S  Show System information: host name, kernel, distro"
1200         print_screen_output "-u  Show partition UUIDs. Default: short partition -P. For full -p output, use: -pu (or -plu)."
1201         print_screen_output "-v  Script verbosity levels. Verbosity level number is required."
1202         print_screen_output "    Supported levels: 0-${VERBOSITY_LEVELS} Example: $SCRIPT_NAME -v 4"
1203         print_screen_output "    0 - short output, same as: $SCRIPT_NAME"
1204         print_screen_output "    1 - basic verbose, same as: $SCRIPT_NAME -d"
1205         print_screen_output "    2 - Also show networking card data"
1206         print_screen_output "    3 - Also show hard disk names as detected."
1207         print_screen_output "    4 - Also show partition size/filled data for (if present):/, /home, /var/, /boot"
1208         print_screen_output "    5 - For multicore systems, also show per core clock speeds; shows audio card."
1209         print_screen_output "-x  Show extra data: bogomips on Cpu; driver version (if available) for Network/Audio;"
1210         print_screen_output "    direct rendering status for Graphics (in X). Only works with verbose or line output."
1211         print_screen_output ""
1212         print_screen_output "Additional Options:"
1213         print_screen_output "-h - this help menu."
1214         if [[ $B_ALLOW_UPDATE == 'true' ]];then
1215                 print_screen_output "-U  Auto-update script. Note: if you installed as root, you"
1216                 print_screen_output "    must be root to update, otherwise user is fine."
1217         fi
1218         print_screen_output "-V  $SCRIPT_NAME version information. Prints information then exits."
1219         print_screen_output "-%  Overrides defective or corrupted data."
1220         print_screen_output "-@  Triggers debugger output. Requires debugging level 1-10 (8-10 - logging)."
1221         print_screen_output "    8 - basic logging; 9 - full file/sys info logging; 10 - color logging."
1222         if [[ $1 == 'full' ]];then
1223                 print_screen_output ""
1224                 print_screen_output "Developer and Testing Options (Advanced):"
1225                 print_screen_output "-! 1 - Sets testing flag B_TESTING_1='true' to trigger testing condition 1."
1226                 print_screen_output "-! 2 - Sets testing flag B_TESTING_2='true' to trigger testing condition 2."
1227                 print_screen_output "-! 3 - Sets flags B_TESTING_1='true' and B_TESTING_2='true'."
1228                 print_screen_output "-! 10 - Triggers an update from the primary dev download server instead of svn."
1229                 print_screen_output "-! 11 - Triggers an update from svn branch one - if present, of course."
1230                 print_screen_output "-! 12 - Triggers an update from svn branch two - if present, of course."
1231                 print_screen_output "-! 13 - Triggers an update from svn branch three - if present, of course."
1232                 print_screen_output "-! 14 - Triggers an update from svn branch four - if present, of course."
1233                 print_screen_output "-! <http://......> - Triggers an update from whatever server you list."
1234                 print_screen_output ""
1235         fi
1236         print_screen_output ""
1237 }
1238
1239 ## print out version information for -V/--version
1240 print_version_info()
1241 {
1242         local last_modified=$( grep -im 1 'date:' $SCRIPT_PATH/$SCRIPT_NAME | gawk '{print $3,$4,$5}' )
1243
1244         print_screen_output "$SCRIPT_NAME - the universal, portable, system info script for irc."
1245         print_screen_output "Version: $SCRIPT_VERSION_NUMBER"
1246         print_screen_output "Script Last Modified: $last_modified"
1247         print_screen_output "Script Location: $SCRIPT_PATH"
1248         print_screen_output ""
1249         print_screen_output "Tested with Irssi, Xchat, Konversation, BitchX, KSirc, ircII,"
1250         print_screen_output "Gaim/Pidgin, Weechat, KVIrc and Kopete."
1251         print_screen_output ""
1252         print_screen_output "This script is a fork of Infobash 3.02, which is:"
1253         print_screen_output "Copyright (C) 2005-2007  Michiel de Boer a.k.a. locsmif"
1254         print_screen_output "Subsequent changes and modifications (after Infobash 3.02) are:"
1255         print_screen_output "Copyright (C) 2008-9 Scott Rogers, Harald Hope, aka trash80 & h2"
1256         print_screen_output ""
1257         print_screen_output "This program is free software; you can redistribute it and/or modify"
1258         print_screen_output "it under the terms of the GNU General Public License as published by"
1259         print_screen_output "the Free Software Foundation; either version 3 of the License, or"
1260         print_screen_output "(at your option) any later version."
1261 }
1262
1263 ########################################################################
1264 #### MAIN FUNCTIONS
1265 ########################################################################
1266
1267 #### -------------------------------------------------------------------
1268 #### initial startup stuff
1269 #### -------------------------------------------------------------------
1270
1271 # Determine where inxi was run from, set IRC_CLIENT and IRC_CLIENT_VERSION
1272 get_start_client()
1273 {
1274         eval $LOGFS
1275         local irc_client_path='' irc_client_path_lower='' non_native_konvi='' i=''
1276         local b_non_native_app='false' pppid='' app_working_name=''
1277         local b_qt4_konvi='false'
1278
1279         if tty >/dev/null;then
1280                 IRC_CLIENT='Shell'
1281                 unset IRC_CLIENT_VERSION
1282                 B_RUNNING_IN_SHELL='true'
1283         elif [[ -n $PPID && -f /proc/$PPID/exe ]];then
1284                 irc_client_path=$( readlink /proc/$PPID/exe )
1285                 irc_client_path_lower=$( tr '[:upper:]' '[:lower:]' <<< $irc_client_path )
1286                 app_working_name=$( basename $irc_client_path_lower )
1287                 # handles the xchat/sh/bash/dash cases, and the konversation/perl cases, where clients
1288                 # report themselves as perl or unknown shell. IE:  when konversation starts inxi
1289                 # from inside itself, as a script, the parent is konversation/xchat, not perl/bash etc
1290                 # note: perl can report as: perl5.10.0, so it needs wildcard handling
1291                 case $app_working_name in
1292                         bash|dash|sh|perl*)     # We want to know who wrapped it into the shell or perl.
1293                                 pppid="$( ps -p $PPID -o ppid --no-headers | sed 's/ //g' )"
1294                                 if [[ -n $pppid && -f /proc/$pppid/exe ]];then
1295                                         irc_client_path="$( readlink /proc/$pppid/exe )"
1296                                         irc_client_path_lower="$( tr '[:upper:]' '[:lower:]' <<< $irc_client_path )"
1297                                         app_working_name=$( basename $irc_client_path_lower )
1298                                         b_non_native_app='true'
1299                                 fi
1300                                 ;;
1301                 esac
1302                 # replacing loose detection with tight detection, bugs will be handled with app names
1303                 # as they appear.
1304                 case $app_working_name in
1305                         # check for shell first
1306                         bash|dash|sh)
1307                                 unset IRC_CLIENT_VERSION
1308                                 IRC_CLIENT="Shell wrapper"
1309                                 ;;
1310                         # now start on irc clients, alphabetically
1311                         bitchx)
1312                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v | gawk '
1313                                 /Version/ {
1314                                         a=tolower($2)
1315                                         gsub(/[()]|bitchx-/,"",a)
1316                                         print a
1317                                         exit
1318                                 }
1319                                 $2 == "version" {
1320                                         a=tolower($3)
1321                                         sub(/bitchx-/,"",a)
1322                                         print a
1323                                         exit
1324                                 }' )"
1325                                 IRC_CLIENT="BitchX"
1326                                 ;;
1327                         finch)
1328                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v | gawk 'NR == 1 {
1329                                         print $2
1330                                 }' )"
1331                                 IRC_CLIENT="Finch"
1332                                 ;;
1333                         gaim)
1334                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v | gawk 'NR == 1 {
1335                                         print $2
1336                                 }' )"
1337                                 IRC_CLIENT="Gaim"
1338                                 ;;
1339                         ircii)
1340                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v | gawk 'NR == 1 {
1341                                         print $3
1342                                 }' )"
1343                                 IRC_CLIENT="ircII"
1344                                 ;;
1345                         irssi-text|irssi)
1346                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v | gawk 'NR == 1 {
1347                                         print $2
1348                                 }' )"
1349                                 IRC_CLIENT="Irssi"
1350                                 ;;
1351                         konversation) ## konvi < 1.2 (qt4)
1352                                 # this is necessary to avoid the dcop errors from starting inxi as a /cmd started script
1353                                 if [[ $b_non_native_app == 'true' ]];then  ## true negative is confusing
1354                                         KONVI=2
1355                                 else # if native app
1356                                         KONVI=1
1357                                 fi
1358                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v | gawk '
1359                                 /Konversation:/ {
1360                                         for ( i=2; i<=NF; i++ ) {
1361                                                 if (i == NF) {
1362                                                         print $i
1363                                                 }
1364                                                 else {
1365                                                         printf $i" "
1366                                                 }
1367                                         }
1368                                         exit
1369                                 }' )"
1370
1371                                 T=($IRC_CLIENT_VERSION)
1372                                 if [[ ${T[0]} == *+* ]];then
1373                                         # < Sho_> locsmif: The version numbers of SVN versions look like this:
1374                                         #         "<version number of last release>+ #<build number", i.e. "1.0+ #3177" ...
1375                                         #         for releases we remove the + and build number, i.e. "1.0" or soon "1.0.1"
1376                                         IRC_CLIENT_VERSION=" CVS $IRC_CLIENT_VERSION"
1377                                         T2="${T[0]/+/}"
1378                                 else
1379                                         IRC_CLIENT_VERSION=" ${T[0]}"
1380                                         T2="${T[0]}"
1381                                 fi
1382                                 # Remove any dots except the first, and make sure there are no trailing zeroes,
1383                                 T2=$( echo "$T2" | gawk '{
1384                                         sub(/\./, " ")
1385                                         gsub(/\./, "")
1386                                         sub(/ /, ".")
1387                                         printf("%g\n", $0)
1388                                 }' )
1389                                 # Since Konversation 1.0, the DCOP interface has changed a bit: dcop "$DCPORT" Konversation ..etc
1390                                 # becomes : dcop "$DCPORT" default ... or dcop "$DCPORT" irc ..etc. So we check for versions smaller
1391                                 # than 1 and change the DCOP parameter/object accordingly.
1392                                 if [[ ${T2} -lt 1 ]];then
1393                                         DCOPOBJ="Konversation"
1394                                 fi
1395                                 IRC_CLIENT="Konversation"
1396                                 ;;
1397                         kopete)
1398                                 IRC_CLIENT_VERSION=" $( kopete -v | gawk '
1399                                 /Kopete:/ {
1400                                         print $2
1401                                         exit
1402                                 }' )"
1403                                 IRC_CLIENT="Kopete"
1404                                 ;;
1405                         kvirc)
1406                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v 2>&1 | gawk '{
1407                                         for ( i=2; i<=NF; i++) {
1408                                                 if ( i == NF ) {
1409                                                         print $i
1410                                                 }
1411                                                 else {
1412                                                         printf $i" "
1413                                                 }
1414                                         }
1415                                         exit
1416                                  }' )"
1417                                 IRC_CLIENT="KVIrc"
1418                                 ;;
1419                         pidgin)
1420                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v | gawk 'NR == 1 {
1421                                         print $2
1422                                 }' )"
1423                                 IRC_CLIENT="Pidgin"
1424                                 ;;
1425                         quassel*)
1426                                 # sample: quassel -v
1427                                 # Qt: 4.5.0
1428                                 # KDE: 4.2.65 (KDE 4.2.65 (KDE 4.3 >= 20090226))
1429                                 # Quassel IRC: v0.4.0 [+60] (git-22effe5)
1430                                 # note: early < 0.4.1 quassels do not have -v
1431                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v 2>/dev/null | gawk -F ': ' '
1432                                 BEGIN {
1433                                         IGNORECASE=1
1434                                         clientVersion=""
1435                                 }
1436                                 /Quassel IRC/ {
1437                                         clientVersion = $2
1438                                 }
1439                                 END {
1440                                         # this handles pre 0.4.1 cases with no -v
1441                                         if ( clientVersion == "" ) {
1442                                                 clientVersion = "(pre v0.4.1)"
1443                                         }
1444                                         print clientVersion
1445                                 }' )"
1446                                 # now handle primary, client, and core. quasselcore doesn't actually
1447                                 # handle scripts with exec, but it's here just to be complete
1448                                 case $app_working_name in
1449                                         quassel)
1450                                                 IRC_CLIENT="Quassel [M]"
1451                                                 ;;
1452                                         quasselclient)
1453                                                 IRC_CLIENT="Quassel"
1454                                                 ;;
1455                                         quasselcore)
1456                                                 IRC_CLIENT="Quassel (core)"
1457                                                 ;;
1458                                 esac
1459                                 ;;
1460                         weechat-curses)
1461                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v ) "
1462                                 IRC_CLIENT="Weechat"
1463                                 ;;
1464                         xchat-gnome)
1465                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v | gawk 'NR == 1 {
1466                                         print $2
1467                                 }' )"
1468                                 IRC_CLIENT="X-Chat-Gnome"
1469                                 ;;
1470                         xchat)
1471                                 IRC_CLIENT_VERSION=" $( $irc_client_path -v | gawk 'NR == 1 {
1472                                         print $2
1473                                 }' )"
1474                                 IRC_CLIENT="X-Chat"
1475                                 ;;
1476                         # then do some perl type searches, do this last since it's a wildcard search
1477                         perl*|ksirc|dsirc)
1478                                 unset IRC_CLIENT_VERSION
1479                                 # KSirc is one of the possibilities now. KSirc is a wrapper around dsirc, a perl client
1480                                 get_cmdline $PPID
1481                                 for (( i=0; i <= $CMDL_MAX; i++ ))
1482                                 do
1483                                         case ${A_CMDL[i]} in
1484                                                 *dsirc*)
1485                                                 IRC_CLIENT="KSirc"
1486                                                 # Dynamic runpath detection is too complex with KSirc, because KSirc is started from
1487                                                 # kdeinit. /proc/<pid of the grandparent of this process>/exe is a link to /usr/bin/kdeinit
1488                                                 # with one parameter which contains parameters separated by spaces(??), first param being KSirc.
1489                                                 # Then, KSirc runs dsirc as the perl irc script and wraps around it. When /exec is executed,
1490                                                 # dsirc is the program that runs inxi, therefore that is the parent process that we see.
1491                                                 # You can imagine how hosed I am if I try to make inxi find out dynamically with which path
1492                                                 # KSirc was run by browsing up the process tree in /proc. That alone is straightjacket material.
1493                                                 # (KSirc sucks anyway ;)
1494                                                 IRC_CLIENT_VERSION=" $( ksirc -v | gawk '
1495                                                 /KSirc:/ {
1496                                                         print $2
1497                                                         exit
1498                                                 }' )"
1499                                                 break
1500                                                 ;;
1501                                         esac
1502                                 done
1503                                 if [[ -z $IRC_CLIENT_VERSION ]];then
1504                                         IRC_CLIENT="Unknown Perl client"
1505                                 fi
1506                                 ;;
1507                         # then unset, set unknown data
1508                         *)      
1509                                 IRC_CLIENT="Unknown : ${irc_client_path##*/}"
1510                                 unset IRC_CLIENT_VERSION
1511                                 ;;
1512                 esac
1513                 if [[ $SHOW_IRC -lt 2 ]];then
1514                         unset IRC_CLIENT_VERSION
1515                 fi
1516         else
1517                 ## lets look to see if qt4_konvi is the parent.  There is no direct way to tell, so lets infer it.
1518                 ## because $PPID does not work with qt4_konvi, the above case does not work
1519                 b_qt4_konvi=$( is_this_qt4_konvi )
1520                 if [[ $b_qt4_konvi == 'true' ]];then
1521                         KONVI=3
1522                         IRC_CLIENT='Konversation'
1523                         IRC_CLIENT_VERSION=" $( konversation -v | gawk '
1524                                 /Konversation:/ {
1525                                         for ( i=2; i<=NF; i++ ) {
1526                                                 if (i == NF) {
1527                                                         print $i
1528                                                 }
1529                                                 else {
1530                                                         printf $i" "
1531                                                 }
1532                                         }
1533                                         exit
1534                                 }' )"
1535                 else
1536                         IRC_CLIENT="PPID=\"$PPID\" - empty?"
1537                         unset IRC_CLIENT_VERSION
1538                 fi
1539         fi
1540         log_function_data "IRC_CLIENT: $IRC_CLIENT :: IRC_CLIENT_VERSION: $IRC_CLIENT_VERSION :: PPID: $PPID"
1541         eval $LOGFE
1542 }
1543
1544 ## try to infer the use of Konversation >= 1.2, which shows $PPID improperly
1545 ## no known method of finding Kovni >= 1.2 as parent process, so we look to see if it is running,
1546 ## and all other irc clients are not running.  
1547 is_this_qt4_konvi()
1548 {
1549         local konvi_qt4_client='' konvi_dbus_exist='' konvi_pid='' konvi_home_dir='' 
1550         local konvi='' konvi_qt4_ver='' b_is_qt4=''
1551
1552         konvi_dbus_exist=$( qdbus | grep "org.kde.konversation" )
1553          
1554         if [[ -n $konvi_dbus_exist && -e /usr/share/kde4/apps/konversation ]]; then
1555
1556                 konvi_pid=$( ps -A | grep -i 'konversation' )
1557                 konvi_pid=$( echo $konvi_pid | gawk '{ print $1 }' ) 
1558
1559                 konvi_home_dir=$( readlink /proc/$konvi_pid/exe )
1560                 konvi=$( echo $konvi_home_dir | sed "s/\// /g" )
1561                 konvi=($konvi)
1562
1563                 if [[ ${konvi[2]} == 'konversation' ]];then     
1564                         konvi_qt4_ver=$( konversation -v | grep -i 'konversation' )
1565                         konvi_qt4_client=$( echo "$konvi_qt4_ver" | gawk '{ print $2 }' )
1566
1567                         if [[ $konvi_qt4_client > 1.1 ]]; then
1568                                 b_is_qt4='true'
1569                         fi
1570                 fi
1571         else
1572                 konvi_qt4="qt3"
1573                 b_is_qt4='false'
1574         fi
1575
1576         echo $b_is_qt4
1577         ## for testing this module
1578         #qdbus org.kde.konversation /irc say $1 $2 "getpid_dir: $konvi_qt4  qt4_konvi: $konvi_qt4_ver   verNum: $konvi_qt4_ver_num  pid: $konvi_pid ppid: $PPID  konvi_home_dir: ${konvi[2]}"
1579 }
1580
1581 # This needs some cleanup and comments, not quite understanding what is happening, although generally output is known
1582 # Parse the null separated commandline under /proc/<pid passed in $1>/cmdline
1583 # args: $1 - $PPID
1584 get_cmdline()
1585 {
1586         eval $LOGFS
1587         local i=0 ppid=$1
1588
1589         if [[ ! -e /proc/$ppid/cmdline ]];then
1590                 echo 0
1591                 return
1592         fi
1593         ##print_screen_output "Marker"
1594         ##print_screen_output "\$ppid='$ppid' -=- $(< /proc/$ppid/cmdline)"
1595         unset A_CMDL
1596         ## note: need to figure this one out, and ideally clean it up and make it readable
1597         while read -d $'\0' L && [[ $i -lt 32 ]]
1598         do
1599                 A_CMDL[i++]="$L" ## note: make sure this is valid - What does L mean? ##
1600         done < /proc/$ppid/cmdline
1601         ##print_screen_output "\$i='$i'"
1602         if [[ $i -eq 0 ]];then
1603                 A_CMDL[0]=$(< /proc/$ppid/cmdline)
1604                 if [[ -n ${A_CMDL[0]} ]];then
1605                         i=1
1606                 fi
1607         fi
1608         CMDL_MAX=$i
1609         log_function_data "CMDL_MAX: $CMDL_MAX"
1610         eval $LOGFE
1611 }
1612
1613 #### -------------------------------------------------------------------
1614 #### get data types
1615 #### -------------------------------------------------------------------
1616 ## create array of sound cards installed on system, and if found, use asound data as well
1617 get_audio_data()
1618 {
1619         eval $LOGFS
1620         local i='' alsa_data='' alsa_driver='' device_count=''
1621         local usb_proc_file='' array_count='' usb_id='' usb_data=''
1622
1623         IFS=$'\n'
1624         # this first step handles the drivers for cases where the second step fails to find one
1625         device_count=$( echo "$Lspci_Data" | egrep -ic '(multimedia audio controller|audio device)' )
1626         if [[ $device_count -eq 1 ]] && [[ $B_ASOUND_CARDS == 'true' ]];then
1627                 alsa_driver=$( gawk -F ']: ' '
1628                 BEGIN {
1629                         IGNORECASE=1
1630                 }
1631                 # filtering out modems and usb devices like webcams, this might get a
1632                 # usb audio card as well, this will take some trial and error
1633                 $0 !~ /modem/ || $0 !~ /usb/ {
1634                         driver=gensub( /^(.+)( - )(.+)$/, "\\1", 1, $2 )
1635                         gsub(/^ +| +$/,"",driver)
1636                         if ( driver != "" ){
1637                                 print driver
1638                         }
1639                 }'  $DIR_ASOUND_DEVICE )
1640                 log_function_data 'cat' "$DIR_ASOUND_DEVICE"
1641         fi
1642
1643         # this is to safeguard against line breaks from results > 1, which if inserted into following
1644         # array will create a false array entry. This is a hack, not a permanent solution.
1645         alsa_driver=$( echo $alsa_driver )
1646         # now we'll build the main audio data, card name, driver, and port. If no driver is found,
1647         # and if the first method above is not null, and one card is found, it will use that instead.
1648         A_AUDIO_DATA=( $( echo "$Lspci_Data" | gawk -F ': ' -v alsaDriver="$alsa_driver" '
1649         BEGIN {
1650                 IGNORECASE=1
1651         }
1652         /multimedia audio controller|audio device/ {
1653                 audioCard=gensub(/^[0-9a-f:.]+ [^:]+: (.+)$/,"\\1","g",$0)
1654                 # The doublequotes are necessary because of the pipes in the variable.
1655                 gsub(/'"$BAN_LIST_NORMAL"'/, "", audioCard)
1656                 gsub(/,/, " ", audioCard)
1657                 gsub(/^ +| +$/, "", audioCard)
1658                 gsub(/ [ \t]+/, " ", audioCard)
1659
1660                 cards[audioCard]++
1661
1662                 # loop until you get to the end of the data block
1663                 while (getline && !/^$/) {
1664                         if (/driver in use/) {
1665                                 drivers[audioCard] = drivers[audioCard] gensub(/(.*): (.*)/,"\\2","g",$0) ""
1666                         }
1667                         else if (/kernel modules:/) {
1668                                 modules[audioCard] = modules[audioCard] gensub(/(.*): (.*)/,"\\2","g",$0) ""
1669                         }
1670                         else if (/I\/O/) {
1671                                 portsTemp = gensub(/\t*I\/O ports at (.*) \[.*\]/,"\\1","g",$0)
1672                                 ports[audioCard] = ports[audioCard] portsTemp " "
1673                         }
1674                 }
1675         }
1676
1677         END {
1678                 j=0
1679                 for (i in cards) {
1680                         useDrivers=""
1681                         useModules=""
1682                         usePorts=""
1683                         if (cards[i]>1) {
1684                                 a[j]=cards[i]"x "i
1685                                 if (drivers[i] != "") {
1686                                         useDrivers=drivers[i]
1687                                 }
1688                                 if (ports[i] != "") {
1689                                         usePorts = ports[i]
1690                                 }
1691                                 if (modules[i] != "" ) {
1692                                         useModules = modules[i]
1693                                 }
1694                         }
1695                         else {
1696                                 a[j]=i
1697                                 # little trick here to try to catch the driver if there is
1698                                 # only one card and it was null, from the first test of asound/cards
1699                                 if (drivers[i] != "") {
1700                                         useDrivers=drivers[i]
1701                                 }
1702                                 else if ( alsaDriver != "" ) {
1703                                         useDrivers=alsaDriver
1704                                 }
1705                                 if (ports[i] != "") {
1706                                         usePorts=ports[i]
1707                                 }
1708                                 if (modules[i] != "" ) {
1709                                         useModules = modules[i]
1710                                 }
1711                         }
1712                         # create array primary item for master array
1713                         print a[j] "," useDrivers "," usePorts "," useModules
1714                         j++
1715                 }
1716         }') )
1717
1718         # in case of failure of first check do this instead
1719         if [[ ${#A_AUDIO_DATA[@]} -eq 0 ]] && [[ $B_ASOUND_CARDS == 'true' ]];then
1720                 A_AUDIO_DATA=( $( gawk -F ']: ' '
1721                 BEGIN {
1722                         IGNORECASE=1
1723                 }
1724                 $1 !~ /modem/ && $2 !~ /modem/ {
1725                         card=gensub( /^(.+)( - )(.+)$/, "\\3", 1, $2 )
1726                         driver=gensub( /^(.+)( - )(.+)$/, "\\1", 1, $2 )
1727                         if ( card != "" ){
1728                                 print card","driver
1729                         }
1730                 }'  $DIR_ASOUND_DEVICE ) )
1731         fi
1732
1733         # alsa usb detection by damentz
1734         # for every sound card symlink in /proc/asound - display information about it
1735         for usb_proc_file in /proc/asound/*
1736         do
1737                 # if lsusb exists, the file is a symlink, and contains an important usb exclusive file: continue
1738                 if [[ -n $( which lsusb ) && -L $usb_proc_file && -e $usb_proc_file/usbid  ]]; then
1739                         # send error messages of lsusb to /dev/null as it will display a bunch if not a super user
1740                         # also, find the contents of usbid in lsusb and print everything after the 7th word on the
1741                         # corresponding line. Finally, strip out commas as they will change the driver :)
1742                         usb_id=$( cat $usb_proc_file/usbid )
1743                         usb_data=$( lsusb -v 2>/dev/null | grep "$usb_id" )
1744                         log_function_data 'raw' "usb_data:\n$usb_data"
1745                         usb_data=$( gawk '{
1746                                 gsub( /,/, " ", $0 )
1747                                 for( i=7; i <= NF; i++ ) {
1748                                         printf( $i " " )
1749                                 }
1750                         }' <<< "$usb_data" )
1751                         # this method is interesting, it shouldn't work but it does
1752                         #A_AUDIO_DATA=( "${A_AUDIO_DATA[@]}" "$usb_data,snd-usb-audio,," )
1753                         # but until we learn why the above worked, I'm using this one, which is safer
1754                         if [[ -n $usb_data ]];then
1755                                 array_count=${#A_AUDIO_DATA[@]}
1756                                 A_AUDIO_DATA[$array_count]="$usb_data,snd-usb-audio,,"
1757                         fi
1758                 fi
1759         done
1760         IFS="$ORIGINAL_IFS"
1761
1762         # handle cases where card detection fails, like in PS3, where lspci gives no output, or headless boxes..
1763         if [[ ${#A_AUDIO_DATA[@]} -eq 0 ]];then
1764                 A_AUDIO_DATA[0]='Failed to Detect Sound Card!'
1765         fi
1766         log_function_data "A_AUDIO_DATA: ${A_AUDIO_DATA[@]}"
1767         eval $LOGFE
1768 }
1769
1770 get_audio_alsa_data()
1771 {
1772         eval $LOGFS
1773         local alsa_data=''
1774
1775         # now we'll get the alsa data if the file exists
1776         if [[ $B_ASOUND_VERSION == 'true' ]];then
1777                 alsa_data=$( gawk '
1778                         BEGIN {
1779                                 IGNORECASE=1
1780                         }
1781                         # some alsa strings have the build date in (...)
1782                         # remove trailing . and remove possible second line if compiled by user
1783                         $0 !~ /compile/ {
1784                                 gsub( "Driver | [(].*[)]|\.$","",$0 )
1785                                 gsub(/,/, " ", $0)
1786                                 gsub(/^ +| +$/, "", $0)
1787                                 gsub(/ [ \t]+/, " ", $0)
1788                                 if ( $0 != "" ){
1789                                         print $0
1790                                 }
1791                 }' $DIR_ASOUND_VERSION )
1792                 log_function_data 'cat' "$DIR_ASOUND_VERSION"
1793         fi
1794         echo "$alsa_data"
1795         log_function_data "alsa_data: $alsa_data"
1796         eval $LOGFE
1797 }
1798
1799 ## create A_CPU_CORE_DATA, currently with two values: integer core count; core string text
1800 ## return value cpu core count string, this helps resolve the multi redundant lines of old style output
1801 get_cpu_core_count()
1802 {
1803         eval $LOGFS
1804         if [[ $B_CPUINFO == 'true' ]]; then
1805                 # load the A_CPU_TYPE_PCNT_CCNT core data array
1806                 get_cpu_ht_multicore_smp_data
1807                 ## Because of the upcoming release of cpus with core counts over 6, a count of cores is given after Deca (10)
1808                 # count the number of processors given
1809                 local cpu_physical_count=${A_CPU_TYPE_PCNT_CCNT[1]}
1810                 local cpu_core_count=${A_CPU_TYPE_PCNT_CCNT[2]}
1811                 local cpu_type=${A_CPU_TYPE_PCNT_CCNT[0]}
1812
1813                 # match the numberic value to an alpha value
1814                 case $cpu_core_count in
1815                         1) cpu_alpha_count='Single';;
1816                         2) cpu_alpha_count='Dual';;
1817                         3) cpu_alpha_count='Triple';;
1818                         4) cpu_alpha_count='Quad';;
1819                         5) cpu_alpha_count='Penta';;
1820                         6) cpu_alpha_count='Hexa';;
1821                         7) cpu_alpha_count='Hepta';;
1822                         8) cpu_alpha_count='Octa';;
1823                         9) cpu_alpha_count='Ennea';;
1824                         10) cpu_alpha_count='Deca';;
1825                         *) cpu_alpha_count='Multi';;
1826                 esac
1827                 # create array, core count integer; core count string
1828                 # A_CPU_CORE_DATA=( "$cpu_core_count" "$cpu_alpha_count Core$cpu_type" )
1829                 A_CPU_CORE_DATA=( "$cpu_physical_count" "$cpu_alpha_count" "$cpu_type" "$cpu_core_count" )
1830         fi
1831         log_function_data "A_CPU_CORE_DATA: ${A_CPU_CORE_DATA[@]}"
1832         eval $LOGFE
1833 }
1834
1835 ## main cpu data collector
1836 get_cpu_data()
1837 {
1838         eval $LOGFS
1839         local i='' j='' cpu_array_nu='' a_cpu_working='' multi_cpu='' bits=''
1840
1841         if [[ $B_CPUINFO == 'true' ]];then
1842                 IFS=$'\n'
1843                 A_CPU_DATA=( $( gawk -F': ' '
1844                 BEGIN {
1845                         IGNORECASE=1
1846                 }
1847                 # TAKE STRONGER NOTE: \t+ does NOT always work, MUST be [ \t]+
1848                 # TAKE NOTE: \t+ will work for $DIR_CPUINFO, but SOME ARBITRARY FILE used for TESTING might contain SPACES!
1849                 # Therefore PATCH to use [ \t]+ when TESTING!
1850                 /^processor[ \t]+:/ {
1851                         nr = $NF
1852                 }
1853
1854                 /^model name|^cpu\t+:/ {
1855                         gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF )
1856                         gsub(/'"$BAN_LIST_CPU"'/, "", $NF )
1857                         gsub(/,/, " ", $NF)
1858                         gsub(/^ +| +$/, "", $NF)
1859                         gsub(/ [ \t]+/, " ", $NF)
1860                         cpu[nr, "model"] = $NF
1861                 }
1862
1863                 /^cpu MHz|^clock\t+:/ {
1864                         if (!min) {
1865                                 min = $NF
1866                         }
1867                         else {
1868                                 if ($NF < min) {
1869                                         min = $NF
1870                                 }
1871                         }
1872
1873                         if ($NF > max) {
1874                                 max = $NF
1875                         }
1876                         gsub(/MHZ/,"",$NF) ## clears out for cell cpu
1877                         gsub(/.00[0]+$/,".00",$NF) ## clears out excessive zeros
1878                         cpu[nr, "speed"] = $NF
1879                 }
1880
1881                 /^cache size/ {
1882                         cpu[nr, "cache"] = $NF
1883                 }
1884
1885                 /^flags/ {
1886                         cpu[nr, "flags"] = $NF
1887                 }
1888
1889                 /^bogomips/ {
1890                         cpu[nr, "bogomips"] = $NF
1891                 }
1892
1893                 /vendor_id/ {
1894                         gsub(/genuine|authentic/,"",$NF)
1895                         cpu[nr, "vendor"] = tolower( $NF )
1896                 }
1897
1898                 END {
1899                         #if (!nr) { print ",,,"; exit } # <- should this be necessary or should bash handle that
1900                         for ( i = 0; i <= nr; i++ ) {
1901                                 print cpu[i, "model"] "," cpu[i, "speed"] "," cpu[i, "cache"] "," cpu[i, "flags"] "," cpu[i, "bogomips"] ","  cpu[nr, "vendor"]
1902                         }
1903                         if (!min) {
1904                                 print "not found"
1905                                 exit
1906                         }
1907                         if (min != max) {
1908                                 printf("Min:%s%s Max:%s%s\n", min, "Mhz", max, "Mhz")
1909                         }
1910                         else {
1911                                 printf("%s %s\n", max, "Mhz")
1912                         }
1913                 }' $DIR_CPUINFO ) )
1914                 log_function_data 'cat' "$DIR_CPUINFO"
1915         fi
1916
1917         IFS="$ORIGINAL_IFS"
1918         log_function_data "A_CPU_DATA: ${A_CPU_DATA[@]}"
1919         eval $LOGFE
1920 #       echo getMainCpu: ${[@]}
1921 }
1922
1923 ## this is for counting processors and finding HT types
1924 get_cpu_ht_multicore_smp_data()
1925 {
1926         eval $LOGFS
1927         # in /proc/cpuinfo
1928         #                               algorithm
1929         # if > 1 processor && processor id (physical id) == core id then Hyperthreaded (HT)
1930         # if > 1 processor && different processor ids then Multiple Processors (SMP)
1931         # if > 1 processor && processor id != core id then Multi-Core Processors (MCP)
1932         # if = 1 processor then single core/processor Uni-Processor (UP)
1933
1934         if [[ $B_CPUINFO == 'true' ]]; then
1935                 A_CPU_TYPE_PCNT_CCNT=( $(
1936                 gawk '
1937                 BEGIN {
1938                         FS=": "
1939                         IGNORECASE = 1
1940                         core_count = 0
1941                         i = 0
1942                         index_temp = ""
1943                         num_of_cores = 0
1944                         physical_cpu_count = 0
1945                         processor_logical_count = 0
1946                         processors = 1
1947                         type = "UP"
1948                 }
1949                 # counts logical processors, both HT and physical
1950                 /^processor/ {
1951                         processor_logical_count = $NF + 1
1952                 }
1953                 # counts physical cores (not used currently)
1954                 /^cpu cores/ {
1955                         num_of_cores = $NF
1956                 }
1957                 # array of physical cpus ids
1958                 /^physical/ {
1959                         a_physical_id[i] = $NF
1960                 }
1961                 # array of core ids
1962                 /^core id/ {
1963                         a_core_id[i] = $NF
1964                         i++
1965                 }
1966                 END {
1967                         # look for the largest id number, and assign it
1968                         for ( j = 0; j < processor_logical_count; j++ ) {
1969                                 if ( a_core_id[j] > core_count ) {
1970                                         core_count = a_core_id[j]
1971                                 }
1972                         }
1973                         core_count = core_count + 1
1974                         # trick, set the index equal to value, if the same, it will overwrite
1975                         # this lets us create the actual array of true cpu physical ids
1976                         for ( j in a_physical_id ) {
1977                                 index_temp = a_physical_id[j]
1978                                 a_cpu_physical_working[index_temp] = a_physical_id[j]
1979                         }
1980                         # note that length() is a gawk >= 3.1.5 only method, better to do it manually
1981                         for ( j in a_cpu_physical_working ) {
1982                                 ++physical_cpu_count
1983                         }
1984
1985                         # looking at logical processor counts over 1, which means either HT, SMP or MCP
1986                         # http://en.wikipedia.org/wiki/Symmetric_multiprocessing
1987                         if ( processor_logical_count > 1 ) {
1988                                 if ( processor_logical_count > core_count && physical_cpu_count > 1 ) {
1989                                         type = "SMP-HT" # could be Xeon/P4 HT dual cpu
1990                                 }
1991                                 else if ( processor_logical_count > core_count ) {
1992                                         type = "HT"  # this is more than likely a P4 w/HT or an Atom 270
1993                                 }
1994                                 else {
1995                                         type = "SMP"
1996                                 }
1997                         }
1998                         # make sure to handle up cpus too
1999                         else {
2000                                 core_count = 1
2001                                 physical_cpu_count = 1
2002                         }
2003                         print type " " physical_cpu_count " " core_count
2004                 }
2005                 ' $DIR_CPUINFO ) )
2006         fi
2007         log_function_data "A_CPU_TYPE_PCNT_CCNT: ${A_CPU_TYPE_PCNT_CCNT[@]}"
2008         eval $LOGFE
2009 }
2010
2011 # for more on distro id, please reference this python thread: http://bugs.python.org/issue1322
2012 ## return distro name/id if found
2013 get_distro_data()
2014 {
2015         eval $LOGFS
2016         local i='' j='' distro='' distro_file='' a_distro_glob=''
2017
2018         # get the wild carded array of release/version /etc files if present
2019         shopt -s nullglob
2020         cd /etc
2021         a_distro_glob=(*[-_]{release,version})
2022         cd "$OLDPWD"
2023         shopt -u nullglob
2024
2025         if [[ ${#a_distro_glob[@]} -eq 1 ]];then
2026                 distro_file="${a_distro_glob}"
2027         # use the file if it's in the known good lists
2028         elif [[ ${#a_distro_glob[@]} -gt 1 ]];then
2029                 for i in $DISTROS_DERIVED $DISTROS_PRIMARY
2030                 do
2031                         # Only echo works with ${var[@]}, not print_screen_output() or script_debugger()
2032                         # This is a known bug, search for the word "strange" inside comments
2033                         # echo "i='$i' a_distro_glob[@]='${a_distro_glob[@]}'"
2034                         if [[ " ${a_distro_glob[@]} " == *" $i "* ]];then
2035                                 # Now lets see if the distro file is in the known-good working-lsb-list
2036                                 # if so, use lsb-release, if not, then just use the found file
2037                                 # this is for only those distro's with self named release/version files
2038                                 # because Mint does not use such, it must be done as below
2039                                 ## this if statement requires the spaces and * as it is, else it won't work
2040                                 ##
2041                                 if [[ " $DISTROS_LSB_GOOD " == *" ${i} "* ]] && [[ $B_LSB_DIR == 'true' ]];then
2042                                         distro_file='lsb-release'
2043                                 else
2044                                         distro_file="${i}"
2045                                 fi
2046                                 break
2047                         fi
2048                 done
2049         fi
2050         log_function_data "distro_file: $distro_file"
2051         # first test for the legacy antiX distro id file
2052         if [[ -e /etc/antiX ]];then
2053                 distro="$( egrep -oi 'antix.*\.iso' <<< $( remove_erroneous_chars '/etc/antiX' ) | sed 's/\.iso//' )"
2054         # this handles case where only one release/version file was found, and it's lsb-release. This would
2055         # never apply for ubuntu or debian, which will filter down to the following conditions. In general
2056         # if there's a specific distro release file available, that's to be preferred, but this is a good backup.
2057         elif [[ -n $distro_file && -f $DIR_LSB_RELEASE && " $DISTROS_LSB_GOOD" == *" $distro_file "* ]];then
2058                 distro=$( get_distro_lsb_data )
2059         elif [[ $distro_file == 'lsb-release' ]];then
2060                 distro=$( get_distro_lsb_data )
2061         # then if the distro id file was found and it's not in the exluded primary distro file list, read it
2062         elif [[ -n $distro_file && -s /etc/$distro_file && " $DISTROS_EXCLUDE_LIST " != *" $distro_file "* ]];then
2063                 distro=$( remove_erroneous_chars "/etc/$distro_file" )
2064         # otherwise try  the default debian/ubuntu /etc/issue file
2065         elif [[ -f /etc/issue ]];then
2066                 # lsb gives more manageable and accurate output than issue, but mint should use issue for now
2067                 # some bashism, boolean must be in parenthesis to work correctly, ie [[ $(boolean) ]] not [[ $boolean ]]
2068                 if [[ $B_LSB_DIR == 'true' ]] && [[ -z $( grep -i 'mint' /etc/issue ) ]];then
2069                         distro=$( get_distro_lsb_data )
2070                 else
2071                         distro=$( gawk '
2072                         BEGIN {
2073                                 RS=""
2074                         }
2075                         {
2076                                 gsub(/\\[a-z]/, "")
2077                                 gsub(/,/, " ")
2078                                 gsub(/^ +| +$/, "")
2079                                 gsub(/ [ \t]+/, " ")
2080                                 print
2081                         }' /etc/issue )
2082                 fi
2083         fi
2084
2085         if [[ ${#distro} -gt 80 ]] &&  [[ $B_HANDLE_CORRUPT_DATA != 'true' ]];then
2086                 distro="${RED}/etc/${distro_file} corrupted, use -% to override${NORMAL}"
2087         fi
2088         ## note: would like to actually understand the method even if it's not used
2089         # : ${distro:=Unknown distro o_O}
2090         ## test for /etc/lsb-release as a backup in case of failure, in cases where > one version/release file
2091         ## were found but the above resulted in null distro value
2092         if [[ -z $distro ]] && [[ $B_LSB_DIR == 'true' ]];then
2093                 distro=$( get_distro_lsb_data )
2094         fi
2095         ## finally, if all else has failed, give up
2096         if [[ -z $distro ]];then
2097                 distro='Unknown distro o_O'
2098         fi
2099
2100         # this handles an arch bug where /etc/arch-release is empty and /etc/issue is corrupted
2101         if [[ -n $( grep -i 'arch linux' <<< $distro ) ]];then
2102                 distro='Arch Linux'
2103         fi
2104
2105         echo "$distro"
2106         log_function_data "distro: $distro"
2107         eval $LOGFE
2108 }
2109
2110 # args: $1 - optional, app, uses the app test, not being used now
2111 get_distro_lsb_data()
2112 {
2113         eval $LOGFS
2114         local distro=''
2115
2116         if [[ $B_LSB_DIR == 'true' ]] && [[ $1 != 'app' ]];then
2117                 distro=$( gawk -F '=' '
2118                 BEGIN {
2119                         IGNORECASE=1
2120                 }
2121                 # note: adding the spacing directly to variable to make sure distro output is null if not found
2122                 /^DISTRIB_ID/ {
2123                         gsub(/^ +| +$/, "", $NF)
2124                         # this is needed because grep for "arch" is too loose to be safe
2125                         if ( $NF == "arch" ) {
2126                                 distroId = "Arch Linux"
2127                         }
2128                         else if ( $NF != "n/a" ) {
2129                                 distroId = $NF " "
2130                         }
2131                 }
2132                 /^DISTRIB_RELEASE/ {
2133                         gsub(/^ +| +$/, "", $NF)
2134                         if ( $NF != "n/a" ) {
2135                                 distroRelease = $NF " "
2136                         }
2137                 }
2138                 /^DISTRIB_CODENAME/ {
2139                         gsub(/^ +| +$/, "", $NF)
2140                         if ( $NF != "n/a" ) {
2141                                 distroCodename = $NF " "
2142                         }
2143                 }
2144                 END {
2145                         print distroId distroRelease distroCodename
2146                 }' $DIR_LSB_RELEASE )
2147                 log_function_data 'cat' "$DIR_LSB_RELEASE"
2148         fi
2149         # this is HORRIBLY slow, but I don't know why, it runs fast in shell
2150 #       if [[  -n $( which lsb_release ) && $1 == 'app' ]];then
2151 #               distro=$( echo "$( lsb_release -irc )" | gawk '
2152 #               { IGNORECASE=1 }
2153 #               /^Distributor ID/ {
2154 #                       gsub(/^ +| +$/, "", $NF)
2155 #                       distroId = $NF
2156 #               }
2157 #               /^Release/ {
2158 #                       gsub(/^ +| +$/, "", $NF)
2159 #                       distroRelease = $NF
2160 #               }
2161 #               /^Codename/ {
2162 #                       gsub(/^ +| +$/, "", $NF)
2163 #                       distroCodename = $NF
2164 #               }
2165 #               END {
2166 #                       print distroId " " distroRelease " (" distroCodename ")"
2167 #               }' )
2168 #       fi
2169
2170         echo $distro
2171         log_function_data "distro: $distro"
2172         eval $LOGFE
2173 }
2174
2175 ## create array of gfx cards installed on system
2176 get_graphics_card_data()
2177 {
2178         eval $LOGFS
2179         local i=''
2180
2181         IFS=$'\n'
2182         A_GFX_CARD_DATA=( $( echo "$Lspci_Data" | gawk -F': ' '
2183         BEGIN {
2184                 IGNORECASE=1
2185         }
2186         /vga compatible controller/ {
2187                 gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF)
2188                 gsub(/,/, " ", $NF)
2189                 gsub(/^ +| +$/, "", $NF)
2190                 gsub(/ [ \t]+/, " ", $NF)
2191                 print $NF
2192         }' ) )
2193         IFS="$ORIGINAL_IFS"
2194 #       for (( i=0; i < ${#A_GFX_CARD_DATA[@]}; i++ ))
2195 #       do
2196 #               A_GFX_CARD_DATA[i]=$( sanitize_characters BAN_LIST_NORMAL "${A_GFX_CARD_DATA[i]}" )
2197 #       done
2198         # handle cases where card detection fails, like in PS3, where lspci gives no output, or headless boxes..
2199         if [[ ${#A_GFX_CARD_DATA[@]} -eq 0 ]];then
2200                 A_GFX_CARD_DATA[0]='Failed to Detect Video Card!'
2201         fi
2202
2203         # GFXMEM is UNUSED at the moment, because it shows AGP aperture size, which is not necessarily equal to GFX memory..
2204         # GFXMEM="size=[$(echo "$Lspci_Data" | gawk '/VGA/{while (!/^$/) {getline;if (/size=[0-9][0-9]*M/) {size2=gensub(/.*\[size=([0-9]+)M\].*/,"\\1","g",$0);if (size<size2){size=size2}}}}END{print size2}')M]"
2205         log_function_data "A_GFX_CARD_DATA: ${A_GFX_CARD_DATA[@]}"
2206         eval $LOGFE
2207 }
2208
2209 ## create array of glx data
2210 get_graphics_glx_data()
2211 {
2212         eval $LOGFS
2213         if [[ $B_X_RUNNING == 'true' && $B_ROOT != 'true' ]];then
2214                 IFS=$'\n'
2215                 A_GLX_DATA=( $( glxinfo | gawk -F ': ' '
2216                 # note: function declarations go before BEGIN? It appears so, confirm.
2217                 # the real question here though is why this function is even here, seems
2218                 # just to be a complicated way to pack/print a variable, but maybe the
2219                 # original idea was to handle > 1 cases of detections I guess
2220                 function join( arr, sep ) {
2221                         s=""
2222                         i=flag=0
2223                         for ( i in arr ) {
2224                                 if ( flag++ ) {
2225                                         s = s sep
2226                                 }
2227                                 s = s i
2228                         }
2229                         return s
2230                 }
2231
2232                 BEGIN {
2233                         IGNORECASE=1
2234                 }
2235                 /opengl renderer/ {
2236                         if ( $2 ~ /mesa/ ) {
2237                                 # Allow all mesas
2238 #                               if ( $2 ~ / r[3-9][0-9][0-9] / ) {
2239                                         gsub(/'"$BAN_LIST_NORMAL"'/, "", $2)
2240                                         a[$2]
2241                                         # this counter failed in one case, a bug, and is not needed now
2242 #                                       f++
2243 #                               }
2244                                 next
2245                         }
2246                         $2 && a[$2]
2247                 }
2248                 # dropping all conditions from this test to just show full mesa information
2249                 # there is a user case where not f and mesa apply, atom mobo
2250                 # /opengl version/ && ( f || $2 !~ /mesa/ ) {
2251                 /opengl version/ {
2252                         $2 && b[$2]
2253                 }
2254                 /direct rendering/ {
2255                         $2 && c[$2]
2256                 }
2257                 END {
2258                         printf( "%s\n%s\n%s\n", join( a, ", " ), join( b, ", " ), join( c, ", " ) )
2259                 }' ) )
2260                 IFS="$ORIGINAL_IFS"
2261
2262                 # GLXR=$(glxinfo | gawk -F ': ' 'BEGIN {IGNORECASE=1} /opengl renderer/ && $2 !~ /mesa/ {seen[$2]++} END {for (i in seen) {printf("%s ",i)}}')
2263                 #    GLXV=$(glxinfo | gawk -F ': ' 'BEGIN {IGNORECASE=1} /opengl version/ && $2 !~ /mesa/ {seen[$2]++} END {for (i in seen) {printf("%s ",i)}}')
2264         fi
2265         log_function_data "A_GLX_DATA: ${A_GLX_DATA[@]}"
2266         eval $LOGFE
2267 }
2268
2269 ## return screen resolution / tty resolution
2270 get_graphics_res_data()
2271 {
2272         eval $LOGFS
2273         local screen_resolution='' 
2274
2275         if [[ $B_X_RUNNING == 'true' && $B_ROOT != 'true' ]];then
2276                 # Added the two ?'s , because the resolution is now reported without spaces around the 'x', as in
2277                 # 1400x1050 instead of 1400 x 1050. Change as of X.org version 1.3.0
2278                 screen_resolution=$( xrandr | gawk '
2279                 /\*/ {
2280                         res[++m] = gensub(/^.* ([0-9]+) ?x ?([0-9]+)[_ ].* ([0-9\.]+)\*.*$/,"\\1x\\2@\\3hz","g",$0)
2281                 }
2282                 END {
2283                         for (n in res) {
2284                                 if (res[n] ~ /^[[:digit:]]+x[[:digit:]]+/) {
2285                                         line = line ? line ", " res[n] : res[n]
2286                                 }
2287                         }
2288                         if (line) {
2289                                 print(line)
2290                         }
2291                 }' )
2292                 if [[ -z $screen_resolution ]];then
2293                         screen_resolution=$( xdpyinfo | gawk '
2294                         /dimensions/ {
2295                                 print $2
2296                         }' )
2297                 fi
2298         else
2299                 screen_resolution=$( stty -F $( readlink /proc/$PPID/fd/0 ) size | gawk '{
2300                         print $2"x"$1
2301                 }' )
2302         fi
2303         echo "$screen_resolution"
2304         log_function_data "screen_resolution: $screen_resolution"
2305         eval $LOGFE
2306 }
2307
2308 ## for possible future data, not currently used
2309 get_graphics_agp_data()
2310 {
2311         eval $LOGFS
2312         local agp_module=''
2313
2314         if [[ $B_MODULES_DIR == 'true' ]];then
2315                 ## not used currently
2316                 agp_module=$( gawk '
2317                 /agp/ && !/agpgart/ && $3 > 0 {
2318                         print(gensub(/(.*)_agp.*/,"\\1","g",$1))
2319                 }' $DIR_MODULES )
2320                 log_function_data 'cat' "$DIR_MODULES"
2321         fi
2322         log_function_data "agp_module: $agp_module"
2323         eval $LOGFE
2324 }
2325
2326 ## create array of x vendor/version data
2327 get_graphics_x_data()
2328 {
2329         eval $LOGFS
2330         local x_vendor='' x_version=''
2331
2332         if [[ $B_X_RUNNING == 'true' && $B_ROOT != 'true' ]];then
2333                 # X vendor and version detection.
2334                 x_vendor=$( xdpyinfo | gawk -F': +' '
2335                 BEGIN {
2336                         IGNORECASE=1
2337                 }
2338                 /vendor string/ {
2339                         gsub(/the|inc|foundation|project|corporation/, "", $2)
2340                         gsub(/,/, " ", $2)
2341                         gsub(/^ +| +$/, "", $2)
2342                         gsub(/ [ \t]+/, " ", $2)
2343                         print $2
2344                 }' )
2345
2346                 # new method added since radeon and X.org and the disappearance of <X server name> version : ...etc
2347                 # Later on, the normal textual version string returned, e.g. like: X.Org version: 6.8.2
2348                 # A failover mechanism is in place. (if $x_version is empty, the release number is parsed instead)
2349                 x_version=$( xdpyinfo | gawk '/version:/ {
2350                         print $NF
2351                 }' )
2352                 if [[ -z $x_version ]];then
2353                         x_version=$(xdpyinfo | gawk -F': +' '
2354                         BEGIN {
2355                                 IGNORECASE=1
2356                         }
2357                         /vendor release number/ {
2358                                 gsub(/0+$/, "", $2)
2359                                 gsub(/0+/, ".", $2)
2360                                 print $2
2361                         }' )
2362                 fi
2363                 A_X_DATA[0]="$x_vendor"
2364                 A_X_DATA[1]="$x_version"
2365
2366                 #X -version 2>&1 | gawk '/^X Window System Version/ { print $5 }'
2367                 #This method could be used in the future to detect X when X is not running,
2368                 #however currently inxi ignores X checks when X is not found.
2369         fi
2370         log_function_data "A_X_DATA: ${A_X_DATA[@]}"
2371         eval $LOGFE
2372 }
2373
2374 # this gets just the raw data, total space/percent used and disk/name/per disk capacity
2375 get_hdd_data_basic()
2376 {
2377         eval $LOGFS
2378         local hdd_used=''
2379         local hdd_data="$( df --exclude-type=aufs --exclude-type=tmpfs --exclude-type=iso9660 )"
2380         log_function_data 'raw' "hdd_data:\n$hdd_data"
2381         
2382         hdd_used=$( echo "$hdd_data" | gawk '
2383         # also handles odd dm-1 type, from lvm
2384         /^\/dev\/(mapper\/|[hs]d[a-z][0-9]+|dm[-]?[0-9]+)/ {
2385                 # this handles the case where the first item is too long
2386                 # and makes df wrap output to next line, so here we advance
2387                 # it to the next line for that single case
2388                 if ( NF < 5 && $0 !~ /.*\%/ ) {
2389                         getline
2390                 }
2391                 # if the first item caused a wrap, use one less than standard
2392                 # testing for the field with % in it, ie: 34%, then go down from there
2393                 # this also protects against cases where the mount point has a space in the
2394                 # file name, thus breaking going down from $NF directly.
2395                 if ( $4 ~ /.*\%/ ) {
2396                         used += $2
2397                 }
2398                 # otherwise use standard
2399                 else if ( $5 ~ /.*\%/ ) {
2400                         used += $3
2401                 }
2402                 # and if this is not detected, give up, we need user data to debug
2403                 else {
2404                         next
2405                 }
2406         }
2407         END {
2408                 print used
2409         }' )
2410
2411         if [[ -z $hdd_used ]];then
2412                 hdd_used='na'
2413         fi
2414         log_function_data "hdd_used: $hdd_used"
2415         # create the initial array strings:
2416         # disk-dev, capacity, name, usb or not
2417         # final item is the total of the disk
2418         IFS=$'\n'
2419
2420         if [[ $B_PARTITIONS_DIR == 'true' ]];then
2421                 A_HDD_DATA=( $(
2422                 gawk -v hddused="$hdd_used" '
2423                 /[hs]d[a-z]$/ {
2424                         driveSize = $(NF - 1)*1024/1000**3
2425                         gsub(/,/, " ", driveSize)
2426                         gsub(/^ +| +$/, "", driveSize)
2427                         printf( $NF",%.1fGB,,\n", driveSize )
2428                 }
2429                 # See http://lanana.org/docs/device-list/devices-2.6+.txt for major numbers used below
2430                 # $1 ~ /^(3|22|33|8)$/ && $2 % 16 == 0  {
2431                 #       size += $3
2432                 # }
2433                 # special case from this data: 8     0  156290904 sda
2434                 $1 ~ /^(3|22|33|8)$/ && $NF ~ /[hs]d[a-z]$/ && ( $2 % 16 == 0 || $2 % 16 == 8 ) {
2435                         size += $3
2436                 }
2437
2438                 END {
2439                         size = size*1024/1000**3                   # calculate size in GB size
2440                         workingUsed = hddused*1024/1000**3         # calculate workingUsed in GB used
2441                         # this handles a special case with livecds where no hdd_used is detected
2442                         if ( size > 0 && hddused == "na" ) {
2443                                 size = sprintf( "%.1f", size )
2444                                 print size "GB,-"
2445                         }
2446                         else if ( size > 0 && workingUsed > 0 ) {
2447                                 diskUsed = workingUsed*100/size  # calculate used percentage
2448                                 diskUsed = sprintf( "%.1f", diskUsed )
2449                                 size = sprintf( "%.1f", size )
2450                                 print size "GB," diskUsed "% used"
2451                         }
2452                         else {
2453                                 print "NA,-" # print an empty array, this will be further handled in the print out function
2454                         }
2455                 }' $DIR_PARTITIONS ) )
2456                 log_function_data 'cat' "$DIR_PARTITIONS"
2457         fi
2458         IFS="$ORIGINAL_IFS"
2459         log_function_data "A_HDD_DATA: ${A_HDD_DATA[@]}"
2460         eval $LOGFE
2461 }
2462
2463 ## fills out the A_HDD_DATA array with disk names
2464 get_hard_drive_data_advanced()
2465 {
2466         eval $LOGFS
2467         local a_temp_working='' a_temp_scsi='' temp_holder='' temp_name='' i='' j=''
2468         local sd_ls_by_id='' ls_disk_by_id=''
2469
2470         ## check for all ide type drives, non libata, only do it if hdx is in array
2471         if [[ -n $( egrep 'hd[a-z]' <<< ${A_HDD_DATA[@]} ) ]];then
2472                 # remember, we're using the last array item to store the total size of disks
2473                 for (( i=0; i < ${#A_HDD_DATA[@]} - 1; i++ ))
2474                 do
2475                         IFS=","
2476                         a_temp_working=( ${A_HDD_DATA[i]} )
2477                         IFS="$ORIGINAL_IFS"
2478                         if [[ -n $( egrep '^hd[a-z]' <<< ${a_temp_working[0]} ) ]];then
2479                                 if [[ -e /proc/ide/${a_temp_working[0]}/model ]];then
2480                                         a_temp_working[2]="$( remove_erroneous_chars /proc/ide/${a_temp_working[0]}/model )"
2481                                 else
2482                                         a_temp_working[2]="Name n/a"
2483                                 fi
2484                                 # these loops are to easily extend the cpu array created in the gawk script above with more fields per cpu.
2485                                 for (( j=0; j < ${#a_temp_working[@]}; j++ ))
2486                                 do
2487                                         if [[ $j -gt 0 ]];then
2488                                                 A_HDD_DATA[i]="${A_HDD_DATA[i]},${a_temp_working[$j]}"
2489                                         else
2490                                                 A_HDD_DATA[i]="${a_temp_working[$j]}"
2491                                         fi
2492                                 done
2493                         fi
2494                 done
2495         fi
2496
2497         ## then handle libata names
2498         # first get the ata device names, put them into an array
2499         IFS=$'\n'
2500         if [[ $B_SCSI_DIR == 'true' ]]; then
2501                 a_temp_scsi=( $( gawk  '
2502                 BEGIN {
2503                         IGNORECASE=1
2504                 }
2505                 /host/ {
2506                         getline a[$0]
2507                         getline b[$0]
2508                 }
2509                 END {
2510                         for (i in a) {
2511                                 if (b[i] ~ / *type: *direct-access.*/) {
2512                                         #c=gensub(/^ *vendor: (.+) +model: (.+) +rev: (.+)$/,"\\1 \\2 \\3","g",a[i])
2513                                         #c=gensub( /^ *vendor: (.+) +model: (.+) +rev:.*$/,"\\1 \\2","g",a[i] )
2514                                         # the vendor: string is useless, and is a bug, ATA is not a vendor for example
2515                                         c=gensub( /^ *vendor: (.+) +model: (.+) +rev:.*$/, "\\2", "g", a[i] )
2516                                         gsub(/,/, " ", c)
2517                                         gsub(/^ +| +$/, "", c)
2518                                         gsub(/ [ \t]+/, " ", c)
2519                                         #print a[i]
2520                                         # we actually want this data, so leaving this off for now
2521 #                                       if (c ~ /\<flash\>|\<pendrive\>|memory stick|memory card/) {
2522 #                                               continue
2523 #                                       }
2524                                         print c
2525                                 }
2526                         }
2527                 }' $DIR_SCSI ) )
2528                 log_function_data 'cat' "$DIR_SCSI"
2529         fi
2530         IFS="$ORIGINAL_IFS"
2531
2532         ## then we'll loop through that array looking for matches.
2533         if [[ -n $( egrep 'sd[a-z]' <<< ${A_HDD_DATA[@]} ) ]];then
2534                 # first pack the main ls variable so we don't have to keep using ls /dev...
2535                 ls_disk_by_id="$( ls -l /dev/disk/by-id )"
2536                 for (( i=0; i < ${#A_HDD_DATA[@]} - 1; i++ ))
2537                 do
2538                         if [[ -n $( egrep '^sd[a-z]' <<< ${A_HDD_DATA[$i]} ) ]];then
2539                                 IFS=","
2540                                 a_temp_working=( ${A_HDD_DATA[$i]} )
2541                                 IFS="$ORIGINAL_IFS"
2542                                 if [[ ${#a_temp_scsi[@]} -gt 0 ]];then
2543                                         for (( j=0; j < ${#a_temp_scsi[@]}; j++ ))
2544                                         do
2545                                                 ## ok, ok, it's incomprehensible, search /dev/disk/by-id for a line that contains the
2546                                                 # discovered disk name AND ends with the correct identifier, sdx
2547                                                 # get rid of whitespace for some drive names and ids, and extra data after - in name
2548                                                 temp_name=$( tr ' ' '_' <<< ${a_temp_scsi[$j]} | cut -d '-' -f 1 )
2549                                                 sd_ls_by_id=$( egrep -m1 ".*$temp_name.*${a_temp_working[0]}$" <<< "$ls_disk_by_id" )
2550
2551                                                 if [[ -n $sd_ls_by_id ]];then
2552                                                         a_temp_working[2]=${a_temp_scsi[$j]}
2553                                                         if [[ -n $( grep 'usb-' <<< $sd_ls_by_id ) ]];then
2554                                                                 a_temp_working[3]='USB'
2555                                                         fi
2556                                                         break
2557                                                 else
2558                                                         a_temp_working[2]="Name n/a"
2559                                                 fi
2560                                         done
2561                                 else
2562                                         a_temp_working[2]="Name n/a"
2563                                 fi
2564
2565                                 # these loops are to easily extend the cpu array created in the gawk script above with more fields per cpu.
2566                                 for (( j=0; j < ${#a_temp_working[@]}; j++ ))
2567                                 do
2568                                         if [[ $j -gt 0 ]];then
2569                                                 A_HDD_DATA[i]="${A_HDD_DATA[i]},${a_temp_working[$j]}"
2570                                         else
2571                                                 A_HDD_DATA[i]="${a_temp_working[$j]}"
2572                                         fi
2573                                 done
2574                         fi
2575                 done
2576                 unset ls_disk_by_id # and then let's dump the data we don't need
2577         fi
2578         log_function_data "A_HDD_DATA: ${A_HDD_DATA[@]}"
2579         eval $LOGFE
2580 }
2581
2582 get_lspci_data()
2583 {
2584         eval $LOGFS
2585         local lspci_data="$( lspci -v | gawk '{
2586                 gsub(/\(prog-if[^)]*\)/,"")
2587                 print
2588         }' )"
2589         
2590         echo "$lspci_data"
2591         log_function_data 'raw' "lspci_data:\n$lspci_data"
2592         eval $LOGFE
2593 }
2594
2595 ## return memory used/installed
2596 get_memory_data()
2597 {
2598         eval $LOGFS
2599         local memory=''
2600         if [[ $B_MEMINFO == 'true' ]];then
2601                 memory=$( gawk '
2602                 /^MemTotal:/ {
2603                         tot = $2
2604                 }
2605                 /^(MemFree|Buffers|Cached):/ {
2606                         notused+=$2
2607                 }
2608                 END {
2609                         used = tot-notused
2610                         printf("%.1f/%.1fMB\n", used/1024, tot/1024)
2611                 }' $DIR_MEMINFO )
2612                 log_function_data 'cat' "$DIR_MEMINFO"
2613         fi
2614         echo "$memory"
2615         log_function_data "memory: $memory"
2616         eval $LOGFE
2617 }
2618
2619 # process and return module version data
2620 get_module_version_number()
2621 {
2622         eval $LOGFS
2623         local module_version=''
2624
2625         if [[ -n $( which modinfo ) ]];then
2626                 module_version=$( modinfo $1 | gawk '
2627                 BEGIN {
2628                         IGNORECASE=1
2629                 }
2630                 /^version/ {
2631                         gsub(/,/, " ", $2)
2632                         gsub(/^ +| +$/, "", $2)
2633                         gsub(/ [ \t]+/, " ", $2)
2634                         print $2
2635                 }
2636                 ' )
2637         fi
2638
2639         echo "$module_version"
2640         log_function_data "module_version: $module_version"
2641         eval $LOGFE
2642 }
2643
2644 ## create array of network cards
2645 get_networking_data()
2646 {
2647         eval $LOGFS
2648         IFS=$'\n'
2649         A_NETWORK_DATA=( $( echo "$Lspci_Data" | gawk '
2650         BEGIN {
2651                 IGNORECASE=1
2652         }
2653         /^[0-9a-f:.]+ (ethernet|network) (controller|bridge)/ || /^[0-9a-f:.]+ [^:]+: .*(ethernet|network).*$/ {
2654                 nic=gensub(/^[0-9a-f:.]+ [^:]+: (.+)$/,"\\1","g",$0)
2655                 gsub(/realtek semiconductor/, "Realtek", nic)
2656                 gsub(/davicom semiconductor/, "Davicom", nic)
2657                 # The doublequotes are necessary because of the pipes in the variable.
2658                 gsub(/'"$BAN_LIST_NORMAL"'/, "", nic)
2659                 gsub(/,/, " ", nic)
2660                 gsub(/^ +| +$/, "", nic)
2661                 gsub(/ [ \t]+/, " ", nic)
2662
2663                 eth[nic]++
2664                 while (getline && !/^$/) {
2665                         if (/I\/O/) {
2666                                 ports[nic] = ports[nic] $4 " "
2667                         }
2668                         if (/driver in use/) {
2669                                 drivers[nic] = drivers[nic] gensub(/(.*): (.*)/,"\\2","g",$0) ""
2670                         }
2671                         else if (/kernel modules/) {
2672                                 modules[nic] = modules[nic] gensub(/(.*): (.*)/,"\\2","g",$0) ""
2673                         }
2674                 }
2675         }
2676
2677         END {
2678                 j=0
2679                 for (i in eth) {
2680                         useDrivers=""
2681                         usePorts=""
2682                         useModules=""
2683                         if (eth[i]>1) {
2684                                 a[j]=eth[i]"x "i
2685                                 ## note: this loses the plural ports case, is it needed anyway?
2686                                 if (ports[i] != "") {
2687                                         usePorts=ports[i]
2688                                 }
2689                                 if (drivers[i] != "") {
2690                                         useDrivers=drivers[i]
2691                                 }
2692                                 if (modules[i] != "" ) {
2693                                         useModules = modules[i]
2694                                 }
2695                         }
2696                         else {
2697                                 a[j]=i
2698                                 if (ports[i] != "") {
2699                                         usePorts=ports[i]
2700                                 }
2701                                 if (drivers[i] != "") {
2702                                         useDrivers=drivers[i]
2703                                 }
2704                                 if (modules[i] != "" ) {
2705                                         useModules = modules[i]
2706                                 }
2707                         }
2708                         # create array primary item for master array
2709                         print a[j] "," useDrivers "," usePorts "," useModules
2710                         j++
2711                 }
2712         }') )
2713         IFS="$ORIGINAL_IFS"
2714         log_function_data "A_NETWORK_DATA: ${A_NETWORK_DATA[@]}"
2715         eval $LOGFE
2716 }
2717
2718 get_networking_wan_ip_data()
2719 {
2720         eval $LOGFS
2721         local ip=''
2722
2723         # get ip using wget redirect to stdout. This is a clean, text only IP output url.
2724         ip=$( wget -q -O - http://smxi.org/opt/ip.php | gawk -F 'is: ' '{
2725                 #gsub("\n","",$2")
2726                 print $2
2727         }' )
2728
2729         if [[ -z $ip ]];then
2730                 ip='None Detected!'
2731         fi
2732         echo "$ip"
2733         log_function_data "ip: $ip"
2734         eval $LOGFE
2735 }
2736
2737 get_networking_local_ip_data()
2738 {
2739         eval $LOGFS
2740         if [[ $B_IFCONFIG == 'true' ]];then
2741                 IFS=$'\n'
2742                 A_INTERFACES_DATA=( $( $DIR_IFCONFIG | gawk '
2743                 BEGIN {
2744                         IGNORECASE=1
2745                 }
2746                 $0 !~ /^lo/ {
2747                         # not clear on why inet is coming through, but this gets rid of it
2748                         # as first line item.
2749                         interface = $1
2750                         gsub(/,/, " ", interface)
2751                         gsub(/^ +| +$/, "", interface)
2752                         gsub(/ [ \t]+/, " ", interface)
2753
2754                         aInterfaces[interface]++
2755                         while (getline && !/^$/) {
2756                                 if (/inet addr:/) {
2757                                         ipAddresses[interface] = gensub( /addr:([0-9\.]+)/, "\\1", "g", $2 )
2758                                 }
2759                         }
2760                 }
2761
2762                 END {
2763                         j=0
2764                         for (i in aInterfaces) {
2765                                 useInterfaceIp = ""
2766                                 a[j] = i
2767                                 if (ipAddresses[i] != "") {
2768                                         useInterfaceIp = ipAddresses[i]
2769                                 }
2770                                 # create array primary item for master array
2771                                 # tested needed to avoid bad data from above, if null it is garbage
2772                                 # this is the easiest way to handle junk I found, improve if you want
2773                                 if ( useInterfaceIp != "" ) {
2774                                         print a[j] "," useInterfaceIp
2775                                 }
2776                                 j++
2777                         }
2778                 }') )
2779                 IFS="$ORIGINAL_IFS"
2780         fi
2781         log_function_data "A_INTERFACES_DATA: ${A_INTERFACES_DATA[@]}"
2782         eval $LOGFE
2783 }
2784
2785 get_partition_data()
2786 {
2787         eval $LOGFS
2788         #local excluded_file_types='--exclude-type=aufs --exclude-type=tmpfs --exclude-type=iso9660'
2789         # df doesn't seem to work in script with variables like at the command line
2790         local main_partition_data="$( df -h -T --exclude-type=aufs --exclude-type=tmpfs --exclude-type=iso9660 )"
2791         local swap_data="$( swapon -s )"
2792         log_function_data 'raw' "main_partition_data:\n$main_partition_data\n\nswap_data:\n$swap_data"
2793         
2794         IFS=$'\n'
2795         # sample line: /dev/sda2     ext3     15G  8.9G  4.9G  65% /home
2796         # $NF = partition name; $(NF - 4) = partition size; $(NF - 3) = used, in gB; $(NF - 1) = percent used
2797         ## note: by subtracting from the last field number NF, we avoid a subtle issue with LVM df output, where if
2798         ## the first field is too long, it will occupy its own line, this way we are getting only the needed data
2799         A_PARTITION_DATA=( $( echo "$main_partition_data" | gawk '
2800         BEGIN {
2801                 IGNORECASE=1
2802         }
2803         # this has to be nulled for every iteration so it does not retain value from last iteration
2804         devBase=""
2805         # this is required because below we are subtracting from NF, so it has to be > 4
2806         # the real issue is long file system names that force the wrap of df output: //fileserver/main
2807         # but we still need to handle more dynamically long space containing file names, but later.
2808         ( NF < 5 ) && ( $0 !~ /[0-9]+\%/ ) {
2809                 # set the dev location here for cases of wrapped output
2810                 if ( NF == 1 ){
2811                         devBase=gensub( /^(\/dev\/)(.+)$/, "\\2", 1, $1 )
2812                 }
2813                 getline
2814         }
2815         # this handles yet another fredforfaen special case where a mounted drive
2816         # has the search string in its name
2817         $NF ~ /^\/$|^\/boot$|^\/var$|^\/home$|^\/tmp$|^\/usr$/ {
2818                 print $NF "," $(NF - 4) "," $(NF - 3) "," $(NF - 1) ",main," devBase
2819         }
2820         # skip all these, including the first, header line. Use the --exclude-type
2821         # to handle new filesystems types we do not want listed here
2822         $NF !~ /^\/$|^\/boot$|^\/var$|^\/home$|^\/tmp$|^\/usr$|^filesystem/ {
2823                 # this is to avoid file systems with spaces in their names, that will make
2824                 # the test show the wrong data in each of the fields, if no x%, then do not use
2825                 # using 3 cases, first default, standard, 2nd, 3rd, handles one and two spaces in name
2826                 if ( $(NF - 1) ~ /[0-9]+\%/ ) {
2827                         print $NF "," $(NF - 4) "," $(NF - 3) "," $(NF - 1) ",secondary," devBase
2828                 }
2829                 # these two cases construct the space containing name
2830                 else if ( $(NF - 2) ~ /[0-9]+\%/ ) {
2831                         print $(NF - 1) " " $NF "," $(NF - 5) "," $(NF - 4) "," $(NF - 2) ",secondary," devBase
2832                 }
2833                 else if ( $(NF - 3) ~ /[0-9]+\%/ ) {
2834                         print $(NF - 2) " " $(NF - 1) " " $NF "," $(NF - 6) "," $(NF - 5) "," $(NF - 3) ",secondary," devBase
2835                 }
2836         }
2837         ' )
2838         # now add the swap partition data, don't want to show swap files, just partitions,
2839         # though this can include /dev/ramzswap0. Note: you can also use /proc/swaps for this
2840         # data, it's the same exact output as swapon -s
2841         $( echo "$swap_data" | gawk '
2842         BEGIN {
2843                 swapCounter = 1
2844         }
2845         /^\/dev/ {
2846                 size = sprintf( "%.2f", $3*1024/1000**3 )
2847                 devBase = gensub( /^(\/dev\/)(.+)$/, "\\2", 1, $1 )
2848                 used = sprintf( "%.2f", $4*1024/1000**3 )
2849                 percentUsed = sprintf( "%.0f", ( $4/$3 )*100 )
2850                 print "swap-" swapCounter "," size "GB," used "GB," percentUsed "\%,main," devBase
2851                 swapCounter = ++swapCounter
2852         }' ) )
2853         IFS="$ORIGINAL_IFS"
2854
2855         if [[ $B_SHOW_LABELS == 'true' || $B_SHOW_UUIDS == 'true' ]];then
2856                 get_partition_data_advanced
2857         fi
2858         log_function_data "A_PARTITION_DATA: ${A_PARTITION_DATA[@]}"
2859         eval $LOGFE
2860 }
2861
2862 # first get the locations of the mount points for label/uuid detection
2863 get_partition_data_advanced()
2864 {
2865         eval $LOGFS
2866         local a_partition_working='' dev_partition_data=''
2867         local dev_disk_label='' dev_disk_uuid='' dev_item='' dev_label='' dev_uuid=''
2868         local mount_point=''
2869
2870         if [[ -d /dev/disk/by-label ]];then
2871                 dev_disk_label="$( ls -l /dev/disk/by-label )"
2872         fi
2873         if [[ -d /dev/disk/by-uuid ]];then
2874                 dev_disk_uuid="$( ls -l /dev/disk/by-uuid )"
2875         fi
2876         log_function_data 'raw' "dev_disk_label:\n$dev_disk_label\n\ndev_disk_uuid:\n$dev_disk_uuid"
2877
2878         if [[ $B_MOUNTS_DIR == 'true' ]];then
2879                 for (( i=0; i < ${#A_PARTITION_DATA[@]}; i++ ))
2880                 do
2881                         IFS=","
2882                         a_partition_working=( ${A_PARTITION_DATA[i]} )
2883                         IFS="$ORIGINAL_IFS"
2884                         # note: for swap this will already be set
2885                         if [[ -z ${a_partition_working[5]} ]];then
2886                                 mount_point=$( sed 's|/|\\/|g'  <<< ${a_partition_working[0]} )
2887                                 #echo mount_point $mount_point
2888                                 dev_partition_data=$( gawk '
2889                                 BEGIN {
2890                                         IGNORECASE = 1
2891                                         partition = ""
2892                                         partTemp = ""
2893                                 }
2894                                 # trying to handle space in name
2895 #                               gsub( /\\040/, " ", $0 )
2896                                 /[ \t]'$mount_point'[ \t]/ && $1 != "rootfs" {
2897                                         # initialize the variables
2898                                         label = ""
2899                                         uuid = ""
2900
2901                                         # slice out the /dev
2902                                         partition=gensub( /^(\/dev\/)(.+)$/, "\\2", 1, $1 )
2903                                         # label and uuid can occur for root, set partition to null now
2904                                         if ( partition ~ /by-label/ ) {
2905                                                 label=gensub( /^(\/dev\/disk\/by-label\/)(.+)$/, "\\2", 1, $1 )
2906                                                 partition = ""
2907                                         }
2908                                         if ( partition ~ /by-uuid/ ) {
2909                                                 uuid=gensub( /^(\/dev\/disk\/by-uuid\/)(.+)$/, "\\2", 1, $1 )
2910                                                 partition = ""
2911                                         }
2912
2913                                         # handle /dev/root for / id
2914                                         if ( partition == "root" ) {
2915                                                 # if this works, great, otherwise, just set this to null values
2916                                                 partTemp="'$( readlink /dev/root 2>/dev/null )'"
2917                                                 if ( partTemp != "" ) {
2918                                                         if ( partTemp ~ /[hs]d[a-z][0-9]/ ) {
2919                                                                 partition=gensub( /^(\/dev\/)(.+)$/, "\\2", 1, partTemp )
2920                                                         }
2921                                                         else if ( partTemp ~ /by-uuid/ ) {
2922                                                                 uuid=gensub( /^(\/dev\/disk\/by-uuid\/)(.+)$/, "\\2", 1, partTemp )
2923                                                                 partition="" # set null to let real location get discovered
2924                                                         }
2925                                                         else if ( partTemp ~ /by-label/ ) {
2926                                                                 label=gensub( /^(\/dev\/disk\/by-label\/)(.+)$/, "\\2", 1, partTemp )
2927                                                                 partition="" # set null to let real location get discovered
2928                                                         }
2929                                                 }
2930                                                 else {
2931                                                         partition = ""
2932                                                         label = ""
2933                                                         uuid = ""
2934                                                 }
2935                                         }
2936                                         print partition "," label "," uuid
2937                                 }'      $DIR_MOUNTS )
2938
2939 #                               echo dev_partition_data: $dev_partition_data
2940                                 # assemble everything we could get for dev/h/dx, label, and uuid
2941                                 IFS=","
2942                                 A_PARTITION_DATA[i]=${a_partition_working[0]}","${a_partition_working[1]}","${a_partition_working[2]}","${a_partition_working[3]}","${a_partition_working[4]}","$dev_partition_data
2943                                 IFS="$ORIGINAL_IFS"
2944                         fi
2945                         ## now we're ready to proceed filling in the data
2946                         IFS=","
2947                         a_partition_working=( ${A_PARTITION_DATA[i]} )
2948                         IFS="$ORIGINAL_IFS"
2949
2950                         dev_item=${a_partition_working[5]}
2951                         dev_label=${a_partition_working[6]}
2952                         dev_uuid=${a_partition_working[7]}
2953
2954                         # then if dev data/uuid is incomplete, try to get missing piece
2955                         # it's more likely we'll get a uuid than a label. But this should get the
2956                         # dev item set no matter what, so then we can get the rest of any missing data
2957                         # first we'll get the dev_item if it's missing
2958                         if [[ -n $dev_disk_uuid ]] && [[ -z $dev_item && -n $dev_uuid ]];then
2959                                 dev_item=$( echo "$dev_disk_uuid" | gawk '
2960                                         /'$dev_uuid'/ {
2961                                                 item=gensub( /..\/..\/(.+)/, "\\1", 1, $NF )
2962                                                 print item
2963                                         }' )
2964                         elif [[ -n $dev_disk_label ]] && [[ -z $dev_item && -n $dev_label ]];then
2965                                 dev_item=$( echo "$dev_disk_label" | gawk '
2966                                         # first we need to change space x20 in by-label back to a real space
2967                                         #gsub( /x20/, " ", $0 )
2968                                         # then we can see if the string is there
2969                                         /'$dev_label'/ {
2970                                                 item=gensub( /..\/..\/(.+)/, "\\1", 1, $NF )
2971                                                 print item
2972                                         }' )
2973                         fi
2974                         if [[ -n $dev_disk_uuid ]] && [[ -n $dev_item && -z $dev_uuid ]];then
2975                                 dev_uuid=$( echo "$dev_disk_uuid" | gawk '
2976                                 /'$dev_item'$/ {
2977                                         print $(NF - 2)
2978                                 }' )
2979                         fi
2980                         if [[ -n $dev_disk_label ]] && [[ -n $dev_item && -z $dev_label ]];then
2981                                 dev_label=$( echo "$dev_disk_label" | gawk '
2982                                 /'$dev_item'/ {
2983                                         print $(NF - 2)
2984                                 }' )
2985
2986                         fi
2987                         # assemble everything we could get for dev/h/dx, label, and uuid
2988                         IFS=","
2989                         A_PARTITION_DATA[i]=${a_partition_working[0]}","${a_partition_working[1]}","${a_partition_working[2]}","${a_partition_working[3]}","${a_partition_working[4]}","$dev_item","$dev_label","$dev_uuid
2990                         IFS="$ORIGINAL_IFS"
2991                 done
2992                 log_function_data 'cat' "$DIR_MOUNTS"
2993         fi
2994         log_function_data "A_PARTITION_DATA: ${A_PARTITION_DATA[@]}"
2995         eval $LOGFE
2996 }
2997
2998 ## return uptime string
2999 get_uptime()
3000 {
3001         eval $LOGFS
3002         ## note: removing gsub(/ /,"",a); to get get space back in there, goes right before print a
3003         local uptime_value="$( uptime | gawk '{
3004                 a = gensub(/^.*up *([^,]*).*$/,"\\1","g",$0)
3005                 print a
3006         }' )"
3007         echo "$uptime_value"
3008         log_function_data "uptime_value: $uptime_value"
3009         eval $LOGFE
3010 }
3011
3012 #### -------------------------------------------------------------------
3013 #### special data handling for specific options and conditions
3014 #### -------------------------------------------------------------------
3015
3016 ## multiply the core count by the data to be calculated, bmips, cache
3017 # args: $1 - string to handle; $2 - cpu count
3018 calculate_multicore_data()
3019 {
3020         eval $LOGFS
3021         local string_number=$1 string_data=''
3022
3023         if [[ -n $( egrep -i '( mb| kb)' <<< $1 ) ]];then
3024                 string_data=" $( gawk '{print $2}' <<< $1 )" # add a space for output
3025                 string_number=$( gawk '{print $1}' <<< $1 )
3026         fi
3027         # handle weird error cases where it's not a number
3028         if [[ -n $( egrep '^[0-9\.,]+$' <<< $string_number ) ]];then
3029                 string_number=$( echo $string_number $2 | gawk '{
3030                         total = $1*$2
3031                         print total
3032                 }' )
3033         elif [[ $string_number == '' ]];then
3034                 string_number='Not Available'
3035         else
3036                 # I believe that the above returns 'unknown' by default so no need for extra text
3037                 string_number="$string_number "
3038         fi
3039         echo "$string_number$string_data"
3040         log_function_data "string_numberstring_data: $string_number$string_data"
3041         eval $LOGFE
3042 }
3043
3044 # prints out shortened list of flags, the main ones of interest
3045 # args: $1 - string of cpu flags to process
3046 process_cpu_flags()
3047 {
3048         eval $LOGFS
3049         local cpu_flags="$1"
3050
3051         # nx = AMD stack protection extensions
3052         # lm = Intel 64bit extensions
3053         # sse, sse2, pni = sse1,2,3 gfx extensions
3054         # svm = AMD pacifica virtualization extensions
3055         # vmx = Intel IVT (vanderpool) virtualization extensions
3056         cpu_flags=$( echo "$cpu_flags" | gawk '
3057         BEGIN {
3058                 RS=" "
3059                 ssel["sse"] = 1
3060                 ssel["sse2"] = 2
3061                 ssel["pni"] = 3
3062                 sses[1] = "sse"
3063                 sses[2] = "sse2"
3064                 sses[3] = "sse3"
3065         }
3066         /^(nx|lm|svm|vmx)$/ {
3067                         if (s) {
3068                                 s = s " " $0
3069                         }
3070                         else {
3071                                 s = $0
3072                         }
3073         }
3074         /^(sse2?|pni)$/ {
3075                 if (ssel[$0] > sse) {
3076                         sse = ssel[$0]
3077                 }
3078         }
3079         END {
3080                 if (sse) {
3081                         if (s) {
3082                                 s = sses[sse] " " s
3083                         }
3084                         else {
3085                                 s = sses[sse]
3086                         }
3087                 }
3088                 print s
3089         }' )
3090
3091         #grep -oE '\<(nx|lm|sse[0-9]?|pni|svm|vmx)\>' | tr '\n' ' '))
3092         if [[ -z $cpu_flags ]];then
3093                 cpu_flags="-"
3094         fi
3095         echo "$cpu_flags"
3096         log_function_data "cpu_flags: $cpu_flags"
3097         eval $LOGFE
3098 }
3099
3100 #### -------------------------------------------------------------------
3101 #### print and processing of output data
3102 #### -------------------------------------------------------------------
3103
3104 #### MASTER PRINT FUNCTION - triggers all line item print functions
3105 ## main function to print out, master for all sub print functions.
3106 print_it_out()
3107 {
3108         eval $LOGFS
3109         # note that print_it_out passes local variable values on to its children,
3110         # and in some cases, their children, with Lspci_Data
3111         local Lspci_Data='' # only for verbose
3112
3113         if [[ $B_SHOW_SHORT_OUTPUT == 'true' ]];then
3114                 print_short_data
3115         else
3116                 Lspci_Data="$( get_lspci_data )"
3117                 if [[ $VERBOSITY_LEVEL -ge 1 || $B_SHOW_SYSTEM == 'true' ]];then
3118                         print_system_data
3119                 fi
3120                 if [[ $VERBOSITY_LEVEL -ge 1 || $B_SHOW_CPU == 'true' ]];then
3121                         print_cpu_data
3122                 fi
3123                 if [[ $VERBOSITY_LEVEL -ge 1 || $B_SHOW_GRAPHICS == 'true' ]];then
3124                         print_gfx_data
3125                 fi
3126                 if [[ $VERBOSITY_LEVEL -ge 5 || $B_SHOW_AUDIO == 'true' ]];then
3127                         print_audio_data
3128                 fi
3129                 if [[ $VERBOSITY_LEVEL -ge 2 || $B_SHOW_NETWORK == 'true' ]];then
3130                         print_networking_data
3131                 fi
3132                 if [[ $VERBOSITY_LEVEL -ge 1 || $B_SHOW_DISK == 'true' ]];then
3133                         print_hard_disk_data
3134                 fi
3135                 if [[ $VERBOSITY_LEVEL -ge 4 || $B_SHOW_PARTITIONS == 'true' ]];then
3136                         print_partition_data
3137                 fi
3138                 if [[ $VERBOSITY_LEVEL -ge 1 || $B_SHOW_INFO == 'true' ]];then
3139                         print_info_data
3140                 fi
3141         fi
3142         eval $LOGFE
3143 }
3144
3145 #### SHORT OUTPUT PRINT FUNCTION, ie, verbosity 0
3146 # all the get data stuff is loaded here to keep execution time down for single line print commands
3147 # these will also be loaded in each relevant print function for long output
3148 print_short_data()
3149 {
3150         eval $LOGFS
3151         local current_kernel=$( uname -rm ) # | gawk '{print $1,$3,$(NF-1)}' )
3152         local processes="$(( $( ps aux | wc -l ) - 1 ))"
3153         local short_data='' i='' b_background_black='false'
3154         local memory=$( get_memory_data )
3155         local up_time="$( get_uptime )"
3156
3157         # set A_CPU_CORE_DATA
3158         get_cpu_core_count
3159         local cpc_plural='' cpu_count_print='' model_plural=''
3160         local cpu_physical_count=${A_CPU_CORE_DATA[0]}
3161         local cpu_core_count=${A_CPU_CORE_DATA[3]}
3162         local cpu_core_alpha=${A_CPU_CORE_DATA[1]}
3163         local cpu_type=${A_CPU_CORE_DATA[2]}
3164
3165         if [[ $cpu_physical_count -gt 1 ]];then
3166                 cpc_plural='(s)'
3167                 model_plural='s'
3168                 cpu_count_print="$cpu_physical_count "
3169         fi
3170
3171         local cpu_data_string="${cpu_count_print}${cpu_core_alpha} core"
3172 #       local cpu_core_count=${A_CPU_CORE_DATA[0]}
3173
3174         # load A_HDD_DATA
3175         get_hdd_data_basic
3176         ## note: if hdd_model is declared prior to use, whatever string you want inserted will
3177         ## be inserted first. In this case, it's desirable to print out (x) before each disk found.
3178         local a_hdd_data_count=$(( ${#A_HDD_DATA[@]} - 1 ))
3179         IFS=","
3180         local a_hdd_basic_working=( ${A_HDD_DATA[$a_hdd_data_count]} )
3181         IFS="$ORIGINAL_IFS"
3182         local hdd_capacity=${a_hdd_basic_working[0]}
3183         local hdd_used=${a_hdd_basic_working[1]}
3184
3185         # load A_CPU_DATA
3186         get_cpu_data
3187
3188         IFS=","
3189         local a_cpu_working=(${A_CPU_DATA[0]})
3190         IFS="$ORIGINAL_IFS"
3191         local cpu_model="${a_cpu_working[0]}"
3192         ## assemble data for output
3193         local cpu_clock="${a_cpu_working[1]}" # old CPU3
3194         # this gets that weird min/max final array item
3195         local min_max_clock_nu=$(( ${#A_CPU_DATA[@]} - 1 ))
3196         local min_max_clock=${A_CPU_DATA[$min_max_clock_nu]}
3197
3198         #set_color_scheme 12
3199         if [[ $B_RUNNING_IN_SHELL == 'false' ]];then
3200                 for i in $C1 $C2 $CN
3201                 do
3202                         case "$i" in
3203                                 "$GREEN"|"$WHITE"|"$YELLOW"|"$CYAN")
3204                                         b_background_black='true'
3205                                         ;;
3206                         esac
3207                 done
3208                 if [[ $b_background_black == 'true' ]];then
3209                         for i in C1 C2 CN
3210                         do
3211                                 ## these need to be in quotes, don't know why
3212                                 if [[ ${!i} == $NORMAL ]];then
3213                                         declare $i="${!i}15,1"
3214                                 else
3215                                         declare $i="${!i},1"
3216                                 fi
3217                         done
3218                         #C1="${C1},1"; C2="${C2},1"; CN="${CN},1"
3219                 fi
3220         fi
3221         short_data="${C1}CPU$cpc_plural${CN}[${C2}${SEP1}${cpu_data_string} ${cpu_model}$model_plural (${cpu_type}) clocked at ${min_max_clock}${SEP1}${CN}] ${C1}Kernel${CN}[${C2}${SEP1}${current_kernel}${SEP1}${CN}] ${C1}Up${CN}[${C2}${SEP1}${up_time}${SEP1}${CN}] ${C1}Mem${CN}[${C2}${SEP1}${memory}${SEP1}${CN}] ${C1}HDD${CN}[${C2}${SEP1}${hdd_capacity}($hdd_used)${SEP1}${CN}] ${C1}Procs${CN}[${C2}${SEP1}${processes}${SEP1}${CN}]"
3222
3223         if [[ $SHOW_IRC -gt 0 ]];then
3224                 short_data="${short_data} ${C1}Client${CN}[${C2}${SEP1}${IRC_CLIENT}${IRC_CLIENT_VERSION}${SEP1}${CN}]"
3225         fi
3226         short_data="${short_data} ${C1}$SCRIPT_NAME${C2}${CN}[${C2}${SEP1}$SCRIPT_VERSION_NUMBER${SEP1}${CN}]"
3227         if [[ $SCHEME -gt 0 ]];then
3228                 short_data="${short_data} $NORMAL"
3229         fi
3230         print_screen_output "$short_data"
3231         eval $LOGFE
3232 }
3233
3234 #### LINE ITEM PRINT FUNCTIONS
3235
3236 # print sound card data
3237 print_audio_data()
3238 {
3239         eval $LOGFS
3240         local i='' card_one='Card-1' audio_data='' a_audio_data='' port_data=''
3241         local a_audio_working='' alsa_driver='' alsa_data='' port_plural='' module_version=''
3242         # set A_AUDIO_DATA and get alsa data
3243         get_audio_data
3244         alsa_data=$( get_audio_alsa_data )
3245         IFS=","
3246         a_audio_working=(${A_AUDIO_DATA[0]})
3247         IFS="$ORIGINAL_IFS"
3248
3249         if [[ -n ${A_AUDIO_DATA[@]} ]];then
3250                 # slightly complicated because 2nd array item could be the alsa data
3251                 if [[ ${#A_AUDIO_DATA[@]} -le 1 ]];then
3252                         card_one='Card'
3253                 fi
3254
3255 #               if [[ -n ${a_audio_working[2]} ]];then
3256 #                       port_data=" ${C1}at port${C2} ${a_audio_working[2]}"
3257 #               fi
3258                 # this should only trigger if the $DIR_ASOUND_DEVICE data is used, not lspci -nn
3259                 if [[ -n ${a_audio_working[3]} && $B_EXTRA_DATA == 'true' ]];then
3260                         module_version=$( print_module_version "${a_audio_working[3]}" )
3261                 fi
3262                 if [[ -n ${a_audio_working[1]} ]];then
3263                         alsa_driver=" ${C1}driver${C2} ${a_audio_working[1]}$module_version"
3264                 fi
3265                 if [[ -n ${a_audio_working[2]} && $B_EXTRA_DATA == 'true' ]];then
3266                         if [[ $( wc -w <<< ${a_audio_working[2]} ) -gt 1 ]];then
3267                                 port_plural='s'
3268                         fi
3269                         port_data=" ${C1}at port$port_plural${C2} ${a_audio_working[2]}"
3270                 fi
3271                 audio_data="${C1}$card_one${C2} ${a_audio_working[0]}$alsa_driver$port_data"
3272                 audio_data=$( create_print_line "Audio:" "$audio_data" )
3273                 print_screen_output "$audio_data"
3274                 i=0 ## loop starts with 1 by auto-increment so it only shows cards > 1
3275                 while [[ -n ${A_AUDIO_DATA[++i]} ]]
3276                 do
3277                         IFS=","
3278                         a_audio_working=( ${A_AUDIO_DATA[i]} )
3279                         IFS="$ORIGINAL_IFS"
3280                         port_data=''
3281                         alsa_driver=''
3282                         port_plural=''
3283                         module_version=''
3284                         if [[ -n ${a_audio_working[3]} && $B_EXTRA_DATA == 'true' ]];then
3285                                 module_version=$( print_module_version "${a_audio_working[3]}" )
3286                         fi
3287                         # we're testing for the presence of the 2nd array item here, which is the driver name
3288                         if [[ -n ${a_audio_working[1]} ]];then
3289                                 alsa_driver="${C1}driver${C2} ${a_audio_working[1]}"
3290                         fi
3291                         if [[ -n ${a_audio_working[2]} && $B_EXTRA_DATA == 'true' ]];then
3292                                 if [[ $( wc -w <<< ${a_audio_working[2]} ) -gt 1 ]];then
3293                                         port_plural='s'
3294                                 fi
3295                                 port_data=" ${C1}at port$port_plural${C2} ${a_audio_working[2]}"
3296                         fi
3297                         if [[ -n ${a_audio_working[0]} ]];then
3298                                 audio_data="${C1}Card-$(( $i + 1 ))${C2} ${a_audio_working[0]}$alsa_driver$port_data"
3299                         fi
3300                         if [[ -n $audio_data ]];then
3301                                 audio_data=$( create_print_line " " "$audio_data" )
3302                                 print_screen_output "$audio_data"
3303                         fi
3304                 done
3305                 # alsa driver data only prints out if sound card data is found
3306                 if [[ -n $alsa_data ]];then
3307                         audio_data="${C1}Sound:${C2} $alsa_data"
3308                         audio_data=$( create_print_line " " "$audio_data" )
3309                         print_screen_output "$audio_data"
3310                 fi
3311         fi
3312         eval $LOGFE
3313 }
3314
3315 print_cpu_data()
3316 {
3317         eval $LOGFS
3318         local cpu_data='' i='' cpu_clock_speed='' cpu_multi_clock_data=''
3319         local bmip_data='' cpu_cache='' cpu_vendor='' cpu_flags=''
3320
3321         ##print_screen_output "A_CPU_DATA[0]=\"${A_CPU_DATA[0]}\""
3322         # Array A_CPU_DATA always has one extra element: max clockfreq found.
3323         # that's why its count is one more than you'd think from cores/cpus alone
3324         # load A_CPU_DATA
3325         get_cpu_data
3326
3327         IFS=","
3328         local a_cpu_working=(${A_CPU_DATA[0]})
3329         IFS="$ORIGINAL_IFS"
3330         local cpu_model="${a_cpu_working[0]}"
3331         ## assemble data for output
3332         local cpu_clock="${a_cpu_working[1]}"
3333
3334         cpu_vendor=${a_cpu_working[5]}
3335
3336         # set A_CPU_CORE_DATA
3337         get_cpu_core_count
3338         local cpc_plural='' cpu_count_print='' model_plural=''
3339         local cpu_physical_count=${A_CPU_CORE_DATA[0]}
3340         local cpu_core_count=${A_CPU_CORE_DATA[3]}
3341         local cpu_core_alpha=${A_CPU_CORE_DATA[1]}
3342         local cpu_type=${A_CPU_CORE_DATA[2]}
3343
3344         if [[ $cpu_physical_count -gt 1 ]];then
3345                 cpc_plural='(s)'
3346                 cpu_count_print="$cpu_physical_count "
3347                 model_plural='s'
3348         fi
3349
3350         local cpu_data_string="${cpu_count_print}${cpu_core_alpha} core"
3351         # Strange (and also some expected) behavior encountered. If print_screen_output() uses $1
3352         # as the parameter to output to the screen, then passing "<text1> ${ARR[@]} <text2>"
3353         # will output only <text1> and first element of ARR. That "@" splits in elements and "*" _doesn't_,
3354         # is to be expected. However, that text2 is consecutively truncated is somewhat strange, so take note.
3355         # This has been confirmed by #bash on freenode.
3356         # The above mentioned only emerges when using the debugging markers below
3357         ## print_screen_output "a_cpu_working=\"***${a_cpu_working[@]} $hostName+++++++\"----------"
3358
3359         if [[ -z ${a_cpu_working[2]} ]];then
3360                 a_cpu_working[2]="unknown"
3361         fi
3362
3363         cpu_data=$( create_print_line "CPU$cpc_plural:" "${C1}${cpu_data_string}${C2} ${a_cpu_working[0]}$model_plural (${cpu_type})" )
3364         if [[ $VERBOSITY_LEVEL -ge 3 || $B_SHOW_CPU == 'true' ]];then
3365                 # update for multicore, bogomips x core count.
3366                 if [[ $B_EXTRA_DATA == 'true' ]];then
3367 #                       if [[ $cpu_vendor != 'intel' ]];then
3368                                 bmip_data=$( calculate_multicore_data "${a_cpu_working[4]}" "$(( $cpu_core_count * $cpu_physical_count ))" )
3369 #                       else
3370 #                               bmip_data="${a_cpu_working[4]}"
3371 #                       fi
3372                         bmip_data=" ${C1}bmips${C2} $bmip_data"
3373                 fi
3374                 ## note: this handles how intel reports L2, total instead of per core like AMD does
3375                 # note that we need to multiply by number of actual cpus here to get true cache size
3376                 if [[ $cpu_vendor != 'intel' ]];then
3377                         cpu_cache=$( calculate_multicore_data "${a_cpu_working[2]}" "$(( $cpu_core_count * $cpu_physical_count ))"  )
3378                 else
3379                         cpu_cache=$( calculate_multicore_data "${a_cpu_working[2]}" "$cpu_physical_count"  )
3380                 fi
3381                 # only print shortened list
3382                 if [[ $B_CPU_FLAGS_FULL != 'true' ]];then
3383                         cpu_flags=$( process_cpu_flags "${a_cpu_working[3]}" )
3384                         cpu_flags=" ${C1}flags${C2} ($cpu_flags)"
3385                 fi
3386                 cpu_data="$cpu_data${C2} ${C1}cache${C2} $cpu_cache$cpu_flags$bmip_data${CN}"
3387         fi
3388         # we don't this printing out extra line unless > 1 cpu core
3389         if [[ ${#A_CPU_DATA[@]} -gt 2 ]] && [[ $B_SHOW_CPU == 'true' || $VERBOSITY_LEVEL -ge 5 ]];then
3390                 cpu_clock_speed='' # null < verbosity level 5
3391         else
3392                 cpu_data="$cpu_data ${C1}clocked at${C2} ${a_cpu_working[1]} MHz${CN}"
3393         fi
3394
3395         cpu_data="$cpu_data $cpu_clock_speed"
3396         print_screen_output "$cpu_data"
3397
3398         # we don't this&n