#!/bin/bash
#===========================
# UMVIRT LINUX FROM SCRATCH 
#===========================
# Compilation script.
# Network mode. 
#===========================
# Release: 0.2.2
# Package: v4l-utils
#===========================
# DB commit: a32a16b5f59557e98086285561660418604e0237
# APP commit: 1ca4178aea919e4c9e869e3d650ff8af9678bd6f
#===========================

echo "ULFS Package installation start"
echo "==============================="
echo "Package: v4l-utils" 
echo "Release: 0.2.2" 

echo "checking config file"
if [ -f $ULFS_CONFIG_FILE ]
then
echo "loading config file $ULFS_CONFIG_FILE..."
. $ULFS_CONFIG_FILE
fi
#Creating log directory
mkdir -p /var/log/ulfs-packages/v4l-utils/
#Saving start timestamp
date +%s > /var/log/ulfs-packages/v4l-utils/start.time
#Going to source directory...
cd /sources
#Checking dependances...
      #Checking llvm...
      if [ ! -f /var/cache/ulfs-packages/llvm ]; then
           echo "Dependance \"llvm\" not found. Trying to install...";
           wget --no-check-certificate https://umvirt.com/linux/packages//0.2.2/llvm/install -O - | bash
           if [ ! -f /var/cache/ulfs-packages/llvm ]; then
	             echo "Dependance \"llvm\" is not installed. Exiting..."
               exit
           fi
      fi
      #Checking libjpeg-turbo...
      if [ ! -f /var/cache/ulfs-packages/libjpeg-turbo ]; then
           echo "Dependance \"libjpeg-turbo\" not found. Trying to install...";
           wget --no-check-certificate https://umvirt.com/linux/packages//0.2.2/libjpeg-turbo/install -O - | bash
           if [ ! -f /var/cache/ulfs-packages/libjpeg-turbo ]; then
	             echo "Dependance \"libjpeg-turbo\" is not installed. Exiting..."
               exit
           fi
      fi
      #Checking glu...
      if [ ! -f /var/cache/ulfs-packages/glu ]; then
           echo "Dependance \"glu\" not found. Trying to install...";
           wget --no-check-certificate https://umvirt.com/linux/packages//0.2.2/glu/install -O - | bash
           if [ ! -f /var/cache/ulfs-packages/glu ]; then
	             echo "Dependance \"glu\" is not installed. Exiting..."
               exit
           fi
      fi
      #Checking alsa-lib...
      if [ ! -f /var/cache/ulfs-packages/alsa-lib ]; then
           echo "Dependance \"alsa-lib\" not found. Trying to install...";
           wget --no-check-certificate https://umvirt.com/linux/packages//0.2.2/alsa-lib/install -O - | bash
           if [ ! -f /var/cache/ulfs-packages/alsa-lib ]; then
	             echo "Dependance \"alsa-lib\" is not installed. Exiting..."
               exit
           fi
      fi
#Saving downloading timestamp
date +%s > /var/log/ulfs-packages/v4l-utils/download.time
#Downloading source package archive...
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/v/v4l-utils-1.28.1.tar.xz.md5sum
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/v/v4l-utils-1.28.1.tar.xz
#Checking source package file existance
if [ ! -f v4l-utils-1.28.1.tar.xz ]; then
echo "Error: Can't find v4l-utils-1.28.1.tar.xz. Exiting!"
exit
fi
#Checking source package file checksum
if [ -f v4l-utils-1.28.1.tar.xz.md5sum ]; then
    MD5=`LANG=C md5sum -c v4l-utils-1.28.1.tar.xz.md5sum | grep OK`
    if [ "$MD5" == "" ] ; then
    echo "Error: Checksum of v4l-utils-1.28.1.tar.xz is wrong. Exiting!"
    exit
    fi
fi
    #Saving cleanup timestamp
    date +%s > /var/log/ulfs-packages/v4l-utils/cleanup.time
rm -rfv /sources/v4l-utils-1.28.1/
#Saving extracting timestamp
date +%s > /var/log/ulfs-packages/v4l-utils/unpack.time
#Extracting tar source package archive with default parameters...
tar -xf v4l-utils-1.28.1.tar.xz
#Checking package directory size after unpack...
du -s v4l-utils-1.28.1 | awk 'NR==1 {print $1}' > /var/log/ulfs-packages/v4l-utils/unpack.size 
#Going to source package directory...
cd v4l-utils-1.28.1
#Saving configuration timestamp
date +%s > /var/log/ulfs-packages/v4l-utils/configure.time
#Sleep 1 second
sleep 1
if [[ "$ULFS_PKG_DATERESET" == "YES" ]]
then
#Changing all files creation time (except build configuration files) in source directory to find them after installation
find /sources/v4l-utils-1.28.1 \! -path "*/configure*" \! -path "*/Makefile*" \! -path "*.make" \! -path "*.m4" \! -path "*.am" \! -path "*.mk" \! -path "*.stamp" \! -path "*gentpl.py"  -exec touch -m {} +
fi
#Running configuration script...
echo "checking config file"
if [ -f $ULFS_CONFIG_FILE ]
then
echo "loading config file $ULFS_CONFIG_FILE..."
. $ULFS_CONFIG_FILE
fi
cat > ulfs_configure.sh << EOIS
echo "Initializing distributed build environment... "
if [[ "\$ULFS_ICECC" == "YES" ]]
then
    export PATH="\$ULFS_ICECC_PATH:\$PATH"
    echo "ICECC"
fi

echo "Environment debug..."
echo "PATH: \$PATH"
echo "MAKEFLAGS: \$MAKEFLAGS"
echo "NINJAJOBS: \$NINJAJOBS"
env | grep ULFS


mkdir build &&
cd    build &&

meson setup ..            \\
      --prefix=/usr       \\
      --buildtype=release \\
      --libdir=/usr/lib   \\
      -D gconv=disabled   \\
      -D doxygen-doc=disabled

EOIS
cat ulfs_configure.sh | bash 2>&1 | tee /var/log/ulfs-packages/v4l-utils/configure.log 
#Saving build timestamp
date +%s > /var/log/ulfs-packages/v4l-utils/build.time
#Running build script...
echo "checking config file"
if [ -f $ULFS_CONFIG_FILE ]
then
echo "loading config file $ULFS_CONFIG_FILE..."
. $ULFS_CONFIG_FILE
fi
cat > ulfs_build.sh << EOIS
echo "Initializing distributed build environment... "
if [[ "\$ULFS_ICECC" == "YES" ]]
then
    export PATH="\$ULFS_ICECC_PATH:\$PATH"
    echo "ICECC"
fi

echo "Environment debug..."
echo "PATH: \$PATH"
echo "MAKEFLAGS: \$MAKEFLAGS"
echo "NINJAJOBS: \$NINJAJOBS"
env | grep ULFS


cd    build &&
ninja

EOIS
cat ulfs_build.sh | bash 2>&1 | tee /var/log/ulfs-packages/v4l-utils/build.log 
#Saving install timestamp
date +%s > /var/log/ulfs-packages/v4l-utils/install.time
#Running install script...
cat > ulfs_install.sh << EOIS
cd    build &&
ninja install

for prog in v4l2gl v4l2grab
do
   cp -v contrib/test/\$prog /usr/bin
done

EOIS
USER=`whoami`
if [ "$USER" == "root" ] ; then 
cat ulfs_install.sh | bash 2>&1 | tee /var/log/ulfs-packages/v4l-utils/install.log 
else
cat ulfs_install.sh | sudo bash 2>&1 | tee /var/log/ulfs-packages/v4l-utils/install.log 
fi
#Saving finish timestamp
date +%s > /var/log/ulfs-packages/v4l-utils/finish.time
#Checking package directory size after unpack...
cd /sources 
du -s v4l-utils-1.28.1 | awk 'NR==1 {print $1}' > /var/log/ulfs-packages/v4l-utils/install.size 
echo "ULFS package installation completed."
#Producing files list
echo "Looking for installed files..."
if [  -f /var/log/ulfs-packages/v4l-utils/files.txt ]; then
rm /var/log/ulfs-packages/v4l-utils/files.txt
fi
USER=`whoami`
if [ "$USER" == "root" ] ; then 
find /bin -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
find /sbin -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
find /usr -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
find /etc -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
find /opt -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
find /lib -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
find /lib64 -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
find /var -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time \! -path "/var/log/ulfs-packages/v4l-utils/*" >> /var/log/ulfs-packages/v4l-utils/files.txt
else
sudo find /bin -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
sudo find /sbin -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
sudo find /usr -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
sudo find /etc -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
sudo find /opt -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
sudo find /lib -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
sudo find /lib64 -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time >> /var/log/ulfs-packages/v4l-utils/files.txt
sudo find /var -type f -newer /var/log/ulfs-packages/v4l-utils/configure.time \! -newer /var/log/ulfs-packages/v4l-utils/finish.time \!  -path "/var/log/ulfs-packages/v4l-utils/*" >> /var/log/ulfs-packages/v4l-utils/files.txt
fi
#Marking package as installed...
mkdir -p /var/cache/ulfs-packages
USER=`whoami`
if [ "$USER" == "root" ] ; then 
touch /var/cache/ulfs-packages/v4l-utils
else
sudo touch /var/cache/ulfs-packages/v4l-utils
fi
#Calculate delta size
a=`cat /var/log/ulfs-packages/v4l-utils/unpack.size`
b=`cat /var/log/ulfs-packages/v4l-utils/install.size`
c=$(($b-$a))
echo $c > /var/log/ulfs-packages/v4l-utils/delta.size 
#Calculate prepare time
a=`cat /var/log/ulfs-packages/v4l-utils/start.time`
b=`cat /var/log/ulfs-packages/v4l-utils/configure.time`
dp=$(($b-$a))
#Calculate download time
a=`cat /var/log/ulfs-packages/v4l-utils/download.time`
b=`cat /var/log/ulfs-packages/v4l-utils/unpack.time`
dd=$(($b-$a))
#Calculate delta time
a=`cat /var/log/ulfs-packages/v4l-utils/configure.time`
b=`cat /var/log/ulfs-packages/v4l-utils/finish.time`
db=$(($b-$a))
echo $db > /var/log/ulfs-packages/v4l-utils/delta.time 
#Report
echo ""
echo "ULFS Package installation report"
echo "================================"
echo "Package: v4l-utils" 
echo "Release: 0.2.2" 
echo "Build size: $c" 
echo "Prepare time: $dp sec." 
echo "Download time: $dd sec." 
echo "Build time: $db sec." 

#End of script