#!/bin/bash

# Skygge@2022
# www.skygge.com

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

echo "Ardour compilation/installation wizard"
echo ""
read -p "Enter a directory path for Ardour sources (/usr/src/ardour-git): " -e srcdir
read -p "Enter Ardour installation path (/opt/ardour): " -e installdir
read -p "Enter a number of previous versions to keep before delete (3): " -e versionstokeep
echo ""

[[ ! $srcdir ]] && srcdir=/usr/src/ardour-git
[[ ! $installdir ]] && installdir=/opt/ardour
[[ ! $versionstokeep ]] && versionstokeep=3

# Validating user input

# srcdir
if [ ! -d $srcdir ]; then
    echo "Creating directory for sources."
    #check if the dir is located under user homedir (sudo not required)
    if [ $(echo $srcdir|grep $HOME) ]; then 
	mkdir $srcdir
    else
	echo "I'm going to use sudo command, please enter your password if asked."
	sudo mkdir $srcdir
	sudo chown $user:$group $srcdir
    fi
    if [ $? -ne 0 ]; then
	echo "ERROR: Cannot create the directory. Re-run arconfig and enter correct path."
	exit 1
    else
	echo "The source directory is created successfully."
    fi
else
    echo -n "The directory already exists. Checking write access...."
    touch $srcdir/testfile 2>/dev/null && rm $srcdir/testfile 2>/dev/null
    if [ $? -ne 0 ]; then
	echo "ERROR: Cannot write to the directory. Changing owner and group to $user:$group."
	sudo chown $user:$group $srcdir
    else
	echo "OK."
    fi
fi

# installdir
if [ ! -d $installdir ]; then
    echo "Creating installation directory."
    #check if the dir is located under user homedir (sudo not required)
    if [ $(echo $installdir|grep $HOME) ]; then 
	mkdir $installdir
    else
	echo "I'm going to use sudo command, please enter your password if asked."
	sudo mkdir $installdir
	sudo chown $user:$group $installdir
    fi
    if [ $? -ne 0 ]; then
	echo "ERROR: Cannot create the directory. Re-run arconfig and enter correct path."
	exit 1
    else
	echo "The installation directory is created successfully."
    fi
else
    echo -n "The directory already exists. Checking write access...."
    touch $installdir/testfile && rm $installdir/testfile
    if [ $? -ne 0 ]; then
	echo "ERROR: Cannot write to the directory. Changing owner and group to $user:$group."
	sudo chown $user:$group $installdir
    else
	echo "OK."
    fi
fi

# Number of versions - is it Integer?

if [[ ! "$versionstokeep" =~ ^[0-9]+$ ]]; then
    echo "ERROR: Number of versions to keep is not a valid integer. Run arconfig again and enter correct value or leave empty for default (3)."
    exit 1
fi

# Number of cores used in compilation process"
cpucores=$(cat /proc/cpuinfo |grep processor|wc -l)

# Creating script to run latest Ardour version from $installdir

cat <<'EOF' > ./ardour-git
#!/bin/bash

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

# Check 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 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

cd $installdir

# Find latest vesion
versions=`find . -mindepth 1 -maxdepth 1 -type d|sort -V|cut -d "/" -f2`

for version in $versions
do
    # if executable file exists in version dir
    for number in $(seq 1 10)
    do
	[[ -x $version/bin/ardour$number ]] && currentversion=$version && currentnumber=$number
    done
done

[[ $currentversion ]] && $installdir/$currentversion/bin/ardour$currentnumber
EOF

chmod +x ardour-git
mv ardour-git $installdir

echo "#argit/arcompile configuration file">$config
echo "">>$config
echo "srcdir=$srcdir">>$config
echo "installdir=$installdir">>$config
echo "versionstokeep=$versionstokeep">>$config
echo "cpucores=$cpucores">>$config


echo "Configuration file created in $config successfully."
echo "Discovered $cpucores CPU cores. For compilation will be used all of them."
echo "If you want to change this, set the cores parameter in $config file."

echo "Now I'm going to install all dependencies required by Ardour and the compilation process."
echo "If some of them are already installed in your system, they will not be re-installed."
echo "This operation requires root privileges (sudo), so you may be asked for password."
sleep 3
sudo apt-get install -y git python libboost-all-dev jackd2 libjack-jackd2-dev libasound2-dev libpulse-dev libglibmm-2.4-dev libsndfile1-dev libcurl4-gnutls-dev libarchive-dev liblo-dev libtaglib-cil-dev libtaglib-ocaml-dev vamp-plugin-sdk \
librubberband-dev libudev-dev libfftw3-dev libaubio-dev libxml2-dev libcppunit-dev libusb-1.0-0-dev libwebsockets-dev libcwiid-dev libpangomm-1.4-dev liblrdf0-dev libsamplerate0-dev lv2-dev libserd-dev libsord-dev libsratom-dev liblilv-dev \
libsuil-dev libsoundtouch-dev libgtkmm-2.4-dev

echo ""

echo "To make Ardour work best, the user account must be added to the \"audio\" system group."
echo "Otherwise Ardour can display warning about memory limit at start."
echo "To apply the change you need to re-login. You can do it later."
echo "This operation requires root privileges (sudo), so you may be asked for password."
echo ""

sudo usermod -a -G audio $user
echo "User $user was added to audio group."
echo "Now you can get the Ardour sources using argit command."
