#!/bin/bash

# 
# srversion
# 
# Print information about EyeLink, EDF, and SDL libraries that are currently
# installed as part of the EyeLink Developers Kit/API using pkg-config meta-
# data.
# 
# Copyright (c) 1996-2024, SR Research Ltd., All Rights Reserved
# 
# For use by SR Research licencees only. Redistribution and use in source
# and binary forms, with or without modification, are NOT permitted.
# 
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the distribution.
# 
# Neither name of SR Research Ltd nor the name of contributors may be used
# to endorse or promote products derived from this software without
# specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
# IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# EyeLink is a registered trademark of SR Research Ltd.
#
# SR Research Ltd.                                Email:   info@sr-research.com
# 35 Beaufort Drive                               Phone:   613-271-8686 ext. 233
# Ottawa, Ontario, Canada, K2L 2B9            Toll free: 1-866-821-0731 ext. 233
# 


### Initialisation ###

# Set modules/library names that srversion will query and list
module=(eyelink_core eyelink_core_graphics eyelink_core_graphics_sdl2 edfapi \
            sdl SDL_gfx SDL_image SDL_mixer SDL_ttf sdl2 SDL2_gfx SDL2_image \
                SDL2_mixer SDL2_ttf)

# Declare associative arrays that will map module's name to properties
declare -A libver libdate libsize libpath

# Initialise variables that get size of longest string in each field
maxmod=6 # length of 'Module'
maxver=7 # length of 'Version'
maxdat=4 # length of 'Date'
maxsiz=4 # length of 'Size'
maxpat=4 # length of 'Path'


### First pass - Query the libraries for their properties ###

# For each module name
for m in ${module[@]}
do
    # Default values in event of a failure
    libver[$m]='-------'
    libdate[$m]='----'
    libsize[$m]='----'
    libpath[$m]='not found'
    
    # Library version number.
    ret=$(pkg-config --modversion --silence-errors $m)
    
    # Failed to query module's meta data ... skip to next module
    if  [ $? -ne 0 ] ; then continue ; else libver[$m]=$ret ; fi
    
    # Get parent directory of library files ...
    libdir=$(pkg-config --variable=libdir $m)
    if  [ $? -ne 0 ] ; then continue ; fi
    
    # ... and library's link option (e.g. -leyelink_core) ...
    libs=($(pkg-config --libs $m))
    if  [ $? -ne 0 ] ; then continue ; fi
    
    # ... to build path to library's link name.
    linkname="$libdir/lib${libs[0]#-l}.so"
    
    # Resolve the link name to get the path to the library's real name.
    ret=$(realpath $linkname)
    if  [ $? -ne 0 ] ; then continue ; else libpath[$m]=$ret ; fi
    
    # Call to stat returns library's data modification date & size in bytes.
    sout=($(stat -c "%Y %s" ${libpath[$m]}))
    if  [ $? -ne 0 ] ; then continue ; fi
    
    # Date conversion from Unix epoch to human-readable format.
    ret=$(date -d "@${sout[0]}" "+%Y/%m/%d %T")
    if  [ $? -ne 0 ] ; then continue ; else libdate[$m]=$ret ; fi
    
    # Fetch size in bytes.
    libsize[$m]=${sout[1]}
    
done # first pass


### Second pass - Measure length of strings ###

# For each module name
for m in ${module[@]}
do
    # Length of each string in this record
    lenmod=${#m}
    lenver=${#libver[$m]}
    lendat=${#libdate[$m]}
    lensiz=${#libsize[$m]}
    lenpat=${#libpath[$m]}
    
    # Search for size of longest strings
    maxmod=$(( $lenmod > $maxmod ? $lenmod : $maxmod ))
    maxver=$(( $lenver > $maxver ? $lenver : $maxver ))
    maxdat=$(( $lendat > $maxdat ? $lendat : $maxdat ))
    maxsiz=$(( $lensiz > $maxsiz ? $lensiz : $maxsiz ))
    maxpat=$(( $lenpat > $maxpat ? $lenpat : $maxpat ))
    
done


### Third pass - Print table to stdout ###

# Format string for records of table
fmt="%-"$maxmod"s │ %-"$maxver"s │ %-"$maxdat"s │ %"$maxsiz"s │ %-"$maxpat"s\n"

# Compute total length of each row, including spaces between columns
rowlen=$(( $maxmod + $maxver + $maxdat + $maxsiz + $maxpat + 4 ))

# Print column headers
printf "$fmt" Module Version Date Size Path

# Initialise separator line
printf '─%0.s' $(seq 1 $maxmod)

# Remaining segments of separator line with cross section for column borders
for m in $maxver $maxdat $maxsiz $maxpat ;
do
    printf '─┼─'
    printf '─%0.s' $(seq 1 $m)
done

# End of separator line
printf "\n"

# For each module name
for m in ${module[@]}
do
    printf "$fmt" "$m" "${libver[$m]}" "${libdate[$m]}" "${libsize[$m]}" \
                  "${libpath[$m]}"
done 


### DONE srversion ###

