#!/bin/bash

# Skygge@2022
# www.skygge.com

user=$(whoami)
group=$(id -ng $user)
config=$HOME/.config/argit.conf

# Checking for configuration file
if [ ! -f $config ]; then
    echo "I can't find the configuration file: $config. Starting configuration wizard."
    echo "After that run argit command to download Ardour sources."
    arconfig
    exit 1
else
    . $config
fi

# Checking for sources directory
if [ ! -d $srcdir ]; then
    echo "Ardour sources directory: $srcdir doesn't exist. Starting configuration wizard."
    echo "After that run argit command to download Ardour sources."
    arconfig
    exit 1
fi

# Checking for installation directory
if [ ! -d $installdir ]; then
    echo "The installation directory: $installdir doesn't exist. Starting configuration wizard."
    echo "After that run argit command to download Ardour sources."
    arconfig
    exit 1
fi

# Checking for valid Ardour sources in source directory
if [ ! -d $srcdir/ardour ]; then
    echo "I can't find Ardour sources in $srcdir. Run argit to download the sources."
    exit 1
else
    cd $srcdir/ardour
    version=`git describe 2>/dev/null|rev|cut -d "-" -f 2-|rev`
    if [ $version ]; then
	echo "I found Ardour sources version: $version."
    else
	echo "I can't find Ardour sources in $srcdir/ardour. Run argit to download the sources."
	exit 1
    fi
fi

# Kompilacja
echo "Starting compilation process. Version: $version, CPU cores: $cpucores."
echo "Edit $config file to set different number of cores if you want to."

sleep 1

[[ ! -d $installdir/$version ]] && mkdir $installdir/$version

cd $srcdir/ardour
./waf clean
time (./waf configure --optimize --lv2 --with-backends=jack,pulseaudio,dummy,alsa --prefix=$installdir/$version && ./waf build -j$cpucores && ./waf i18n_mo && ./waf install )

[[ $? -eq 0 ]] && installation="OK"

if [ "$installation" = "OK" ]; then
    echo ""
    echo "Compilation and installation succeded. Ardour version compiled: $version"

    # remove old versions
    cd $installdir
    remove=`find . -maxdepth 1 -mindepth 1 -type "d"|sort -V|head -n -$versionstokeep|cut -d "/" -f 2`
    for oldversion in $remove
    do
	rm -fr $installdir/$oldversion
    done

    # teraz można zainstalować skrót do aplikacji w menu jeśli go jeszcze nie ma.
    if [ ! -f /usr/share/applications/ardour.desktop ] || [ ! -f $HOME/.local/share/applications/ardour.desktop ]; then
	echo ""
	echo "Now you can decide who can see Ardour shortcut in the application launcher. You have two options: everyone or just you."
	echo "By default the shortcut will be created in your profile only and other users can't see it."
	echo "If you choose to create shortcut for all users, the system may ask you for the sudo password."
	echo ""
	echo "What is your choice?"
	select wybor in ME ALL
	do
	  case $wybor in
	    "ME") echo "Installing shortcut in your profile only." ;;
	    "ALL") echo "Installing shortcut for all users." ;;
	    *) echo "Other choice. Installing shortcut in your profile only."
	  esac
	break
	done
    fi

if [ "$wybor" = "ALL" ]; then

# shortcut file
sudo bash -c "cat <<EOF > /usr/share/applications/ardour.desktop
[Desktop Entry]
Comment=Record, mix and master multi-track audio
Exec=$installdir/ardour-git
GenericName=Ardour Digital Audio Workstation
Icon=ardour
Keywords=audio;sound;jackd,DAW,multitrack,ladspa,lv2,midi
Name=Ardour
NoDisplay=false
Path[$e]=
StartupNotify=true
Terminal=0
TerminalOptions=
Type=Application
Version=1.0
X-KDE-SubstituteUID=false
X-KDE-Username=
EOF"

# icon file (svg)
    if [ -f /usr/share/icons/ardour.desktop ]; then

sudo bash -c 'cat <<EOF > /usr/share/icons/ardour.svg
<svg width="48" xmlns="http://www.w3.org/2000/svg" height="48" xmlns:xlink="http://www.w3.org/1999/xlink">
 <defs>
  <linearGradient id="a" y1="32.998" x1="34.5" y2="11.998" gradientUnits="userSpaceOnUse" x2="11.786">
   <stop stop-color="#dd1627"/>
   <stop offset="1" stop-color="#df3e52"/>
  </linearGradient>
  <linearGradient id="b" y1="10.38" x1="46.23" y2="35.663" x2="73.03" gradientUnits="userSpaceOnUse" gradientTransform="translate(-52)">
   <stop stop-color="#f8bac0" stop-opacity=".731"/>
   <stop offset="1" stop-color="#f8bac0" stop-opacity="0"/>
  </linearGradient>
  <path id="c" d="m.735 43.06l23.265-42.35 23.09 42.31c0 0-2.212-.155-5.272-.291-2.851-.127-1.794.114-8.278-1.089-4.911-.91-8.522-2.865-15.253-2.727-7.453.153-17.549 4.149-17.549 4.149"/>
 </defs>
 <use fill="#85151f" xlink:href="#c"/>
 <g color="#000000">
  <path fill="url(#a)" d="m6.03 37.701l17.947-32.938 19.246 35.625c0 0 .111.275-.39.133-.471-.134-.874-.529-1.362-.487-.411.035-.656.63-1.063.565-.582-.093-.88-1.202-1.469-1.185-.519.015-.391 1.01-1.166 1.072-.687.058-.575-1.827-1.267-1.885-.469-.039-.574 1.942-1.156 1.941-.616 0 .025-2.85-.863-2.897-.695-.037-.313 2.501-.937 2.504-.861.004-.13-4.157-1.192-4.139-.755.012.057 3.851-.926 3.839-.905-.011-.033-5.941-1.049-5.869-.838.059.103 5.304-.819 5.326-1.117.026-.005-7.306-1.114-7.315-.981-.009-.102 6.486-1 6.486-1.094 0 .003-8.38-1.037-8.435-1.132-.06.277 8.418-.957 8.409-1.191-.009.34-10.639-1.04-10.741-1.13-.083.309 9.951-.851 9.995-1.218.046.368-11.688-1.073-11.691-1.055-.003.074 11.242-1.019 11.4-1.01-.223.202-12.413-.978-12.417-1.218-.004.206 12.12-1.055 12.148-1.057.014.449-12.166-.939-12.12-1.276.038.049 12.07-1 12.05-1.132-.013.29-11.13-.979-11.06-.967.5-.037 8.599-.965 11.171-1.017-1.01.073-7.962-1.058-9.186-.928 1.336-.202 9.355-.957 9.385-.927.045-.045-6.291-1.039-6.351-.595-.036-.911 7.177-1.64 7.205-.847.085-.754-4.057-1.34-4.044-.679.087-.501 4.532-1.392 4.543-.512.122-.636-1.752-1.094-1.708-.411.039-.488 1.971-1.167 2.319-.621.084-.319-.592.164-1.647"/>
  <use fill="url(#b)" xlink:href="#c"/>
 </g>
</svg>
EOF'

    fi # icon file

else # wybor=me

# shortcut for current user

[[ ! -d $HOME/.local/share/applications/ ]] && mkdir $HOME/.local/share/applications

cat <<EOF > $HOME/.local/share/applications/ardour.desktop
[Desktop Entry]
Comment=Record, mix and master multi-track audio
Exec=$installdir/ardour-git
GenericName=Ardour Digital Audio Workstation
Icon=ardour
Keywords=audio;sound;jackd,DAW,multitrack,ladspa,lv2,midi
Name=Ardour
NoDisplay=false
Path[$e]=
StartupNotify=true
Terminal=0
TerminalOptions=
Type=Application
Version=1.0
X-KDE-SubstituteUID=false
X-KDE-Username=
EOF

    # icon file (svg)
    if [ -f $HOME/.local/share/icons/ardour.svg ]; then
	[[ ! -d $HOME/.local/share/icons ]] && mkdir $HOME/.local/share/icons

cat <<EOF > $HOME/.local/share/icons/ardour.svg
<svg width="48" xmlns="http://www.w3.org/2000/svg" height="48" xmlns:xlink="http://www.w3.org/1999/xlink">
 <defs>
  <linearGradient id="a" y1="32.998" x1="34.5" y2="11.998" gradientUnits="userSpaceOnUse" x2="11.786">
   <stop stop-color="#dd1627"/>
   <stop offset="1" stop-color="#df3e52"/>
  </linearGradient>
  <linearGradient id="b" y1="10.38" x1="46.23" y2="35.663" x2="73.03" gradientUnits="userSpaceOnUse" gradientTransform="translate(-52)">
   <stop stop-color="#f8bac0" stop-opacity=".731"/>
   <stop offset="1" stop-color="#f8bac0" stop-opacity="0"/>
  </linearGradient>
  <path id="c" d="m.735 43.06l23.265-42.35 23.09 42.31c0 0-2.212-.155-5.272-.291-2.851-.127-1.794.114-8.278-1.089-4.911-.91-8.522-2.865-15.253-2.727-7.453.153-17.549 4.149-17.549 4.149"/>
 </defs>
 <use fill="#85151f" xlink:href="#c"/>
 <g color="#000000">
  <path fill="url(#a)" d="m6.03 37.701l17.947-32.938 19.246 35.625c0 0 .111.275-.39.133-.471-.134-.874-.529-1.362-.487-.411.035-.656.63-1.063.565-.582-.093-.88-1.202-1.469-1.185-.519.015-.391 1.01-1.166 1.072-.687.058-.575-1.827-1.267-1.885-.469-.039-.574 1.942-1.156 1.941-.616 0 .025-2.85-.863-2.897-.695-.037-.313 2.501-.937 2.504-.861.004-.13-4.157-1.192-4.139-.755.012.057 3.851-.926 3.839-.905-.011-.033-5.941-1.049-5.869-.838.059.103 5.304-.819 5.326-1.117.026-.005-7.306-1.114-7.315-.981-.009-.102 6.486-1 6.486-1.094 0 .003-8.38-1.037-8.435-1.132-.06.277 8.418-.957 8.409-1.191-.009.34-10.639-1.04-10.741-1.13-.083.309 9.951-.851 9.995-1.218.046.368-11.688-1.073-11.691-1.055-.003.074 11.242-1.019 11.4-1.01-.223.202-12.413-.978-12.417-1.218-.004.206 12.12-1.055 12.148-1.057.014.449-12.166-.939-12.12-1.276.038.049 12.07-1 12.05-1.132-.013.29-11.13-.979-11.06-.967.5-.037 8.599-.965 11.171-1.017-1.01.073-7.962-1.058-9.186-.928 1.336-.202 9.355-.957 9.385-.927.045-.045-6.291-1.039-6.351-.595-.036-.911 7.177-1.64 7.205-.847.085-.754-4.057-1.34-4.044-.679.087-.501 4.532-1.392 4.543-.512.122-.636-1.752-1.094-1.708-.411.039-.488 1.971-1.167 2.319-.621.084-.319-.592.164-1.647"/>
  <use fill="url(#b)" xlink:href="#c"/>
 </g>
</svg>
EOF

    fi # icon file
fi # choice=ALL/ME
else # installation=OK
    echo "Compilation error. Check messages on the screen..."
fi # installation=OK
