#!/bin/bash
#
# netbook-compatibility-levels.sh
#
#    Copyright (C) 2013  Matti Lammi
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# This script will produce information about the compatibility levels 
# of netbooks found in the h-node's hardware database with 100% free 
# GNU/Linux distributions endorsed by the Free Software Foundation. 
# The script will use tools commonly found in GNU/Linux distributions 
# and a source file to do this. Results will be saved to a file and 
# also displayed immediately.
#
# The source file is an XML file which contains detailed information 
# about all the notebooks found in the database. The source file [1] 
# can be found in the h-node's download page [2]. Anyone is free to 
# copy, modify, publish, use, sell, or distribute the file's content 
# for any purpose, commercial or non-commercial, and by any means. 
# For more details, please check the license [3].
#
# [1] http://h-node.org/download/notebooks/en
# [2] http://h-node.org/download/index/en
# [3] http://creativecommons.org/publicdomain/zero/1.0/
#
# Netbook are given different compatibility levels in h-node depending 
# on their compatibility with free software. From highest to lowest, 
# the levels are; A, B, C, D and E. In simple terms, they are as 
# follows:
# 
# 	A-Platinum - All devices work with full performance
#	B-Gold     - All devices work but not with full performance
#	C-Silver   - One device is not supported
#	D-Bronze   - More than one device is not supported
#	E-Garbage  - Netbook is not usable with free software
#
# I have used many line breaks in this script so that I can keep the 
# layout clean when adding this script to my final thesis as an 
# appendix.
#
# Please note that the output of this script may not be 100% accurate.
# I think, however, that the results are accurate enough.
#
#
# SOURCE FILE
#
# File name:            source-netbook-compatibility-levels.xml
# Download date:        26 March 2013
#


### Print instructions.
###
# "echo" program's -e option allows special characters like 
# \n to be implemented.
# \n = new line
# \t = tab
echo -e "H-node script: Netbook compatibility levels\n"
echo -e "First argument:\t\t   Source file"
echo -e "Second argument: \t   Output file\n"


### Quit if source file [1] is not given.
###
# Check if first argument is null.
#  ! = not
# -n = not null
if [ ! -n "$1" ]
then
echo "ERROR: No source given. Read source code for more info."
exit 1
fi

# Check if source [1] does not exist.
#  ! = not
# -f = file exists
if [ ! -f "$1" ]
then
echo "ERROR: Source not found. Read source code for more info."
exit 1
fi


### Quit if output file is not given.
###
# Check if second argument is null.
# !  = not
# -n = not null
if [ ! -n "$2" ]
then
echo "ERROR: No output file given. Read source code for more info."
exit 1
fi


### Create output file.
###
# \n = new line
touch $2
echo -e "Output file created.\n\n"


### Define table format and lines
###
# I will print the data processed from the source files into easy 
# to read tables. I use "printf" instead of "echo" because it has 
# more options to format the output. I'll define a format for the 
# tables so I can easily use it to print several tables and keep 
# their formatting unanimous. The format is a 16 character long space 
# followed by 17 and 20 character long space and a new line character. 
# I added the new line character because "printf" does not include 
# it to its output by default. All character spaces will align 
# characters printed into them to the left. 
# %s = string
# NN = reserved character space in %NNs
#  - = alignment to left
# \n = new line 
TABLE_FORMAT="%-16s%-17s%-20s\n"

# I'll be using two kinds of "lines" of characters to make the 
# output look more readable. I'll define them here.
THICK_LINE="============================================"
THIN_LINE="--------------------------------------------"


### Find out how compatible the netbooks are:
###
printf "$TABLE_FORMAT" "Level" "Amount" "Percentage" > $2
printf "%s\n\n" $THICK_LINE >> $2

# Find out how many netbooks there are and save the result to a 
# variable. Run a loop to check each compatibility level separately 
# to find out how many netbooks have a particular compatibility 
# level. Format the result to a nice table. The "bc" program is used 
# for math when a floating point is required and "let" when simple 
# operations with variables are performed. Variables used are as 
# follows:
#
# TOTAL                 total amount of netbooks in the database
# CALC_TOTAL            total amount of netbooks calculated from each 
#                       compatibility level
# NETBOOKS              amount of netbooks with a certain 
#                       compatibility level
# PERCENTAGE            netbooks of one compatibility level compared 
#                       to total (not accurate, but good enough)
# CALC_PERCENTAGE       netbooks of all compatibility levels 
#                       compared to total
#
# %f = float
#    => %NN.Zf = reserve a space of NN characters and input a float  
#                type number to that space defined in <argument> 
#                aligning right and define the number of decimal 
#                digits in Z.
#  - = alignment to left
# \n = new line 


TOTAL=$(cat $1 | grep "<compatibility>" | wc -l)
CALC_TOTAL=0
PERCENTAGE=0

for LEVEL in A-platinum B-gold C-silver D-bronze E-garbage
do

   NETBOOKS=$(cat $1 | grep "<compatibility>" | grep $LEVEL | wc -l)
   PERCENTAGE=$(echo "scale=4; ($NETBOOKS/$TOTAL)*100" | bc)
   printf "%-15s%7s%21.1f\n" $LEVEL $NETBOOKS $PERCENTAGE >> $2
   let "CALC_TOTAL=$CALC_TOTAL+$NETBOOKS"

done

CALC_PERCENTAGE=$(echo "scale=3; ($CALC_TOTAL/$TOTAL)*100" | bc)
printf "%s\n" $THIN_LINE >> $2
printf "%-15s%7s%21.0f\n" "Total:" $CALC_TOTAL $CALC_PERCENTAGE  >> $2


### Show results.
###
cat $2


### Quit
###
# \n = new line
echo -e "\nAll done.\n"
exit 0

