#!/bin/bash

##########################################################################
#
# SECTION: Init
#
########################################################################## 

# Version: 3.1.4	Author: Mike O'Connor   Date: 21-Dec, 2021
#    Credits:	
#    Inspired by: https://help.jacktrip.org/hc/en-us/articles/1500009330002-Linode-1-Setup-Remote-Server-with-JackTrip-Installed
#    First version built by:  Ralph Jenson
#	 Community of supporters:  Sarah Weaver and the Nownet Arts Hub ensemble

	
# Install update-logs and status-file script

		curl -o  /usr/local/bin/PGJTT-update_logs-3.1.4.sh https://PGJTT.com/Scripts/PGJTT-update_logs-3.1.4.sh
		chmod +x /usr/local/bin/PGJTT-update_logs-3.1.4.sh
		
# Post startup messages to logs and status files

	PGJTT-update_logs-3.1.4.sh  "MainInstall3.1.4 started"
	echo                        "MainInstall3.1.4 started"    
	
# Load 1st-login .profile into root 
	
		curl -o ~/.profile 	https://pgjtt.com/Root_Profiles/root-profile_1st-login3.1.4

# Load shell-session tips file 
	
		curl -o ~/shell-session-tips https://PGJTT.com/Tips-files/shell-session-tips-3.1.4.txt

# Update apt command 

	PGJTT-update_logs-3.1.4.sh  "Updating apt packages"	
	echo                        "Updating apt packages"    
	     
		DEBIAN_FRONTEND='noninteractive' apt-get update && apt-get -y upgrade  
	
# Add scripts directories to PATH so they can run from anywhere

	PGJTT-update_logs-3.1.4.sh  "Updating PATH variable"
	echo                        "Updating PATH variable"
	
	# Load new PATH in etc/environment 
	
cat << EOF >/etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin/Connect-Players_Commands:/usr/local/bin/JMess_Templates/"
EOF

	# Reload PATH

		source /etc/environment

#	# Note: Include that PATH-reload in all versions of .profile
	

##########################################################################
#
# SECTION: Main
#
##########################################################################

	
# Build web server

# 	Configure the web server to provide a repository for sharing audio recordings at
# 	http://[server-IP-address]/Recordings, update lighttpd (web server) for directory 
# 	browsing, and create an alias for the folder in Root's home directory

	PGJTT-update_logs-3.1.4.sh  "Building web server"
	echo                        "Building web server"

	
#	Install web server and related software
		
		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq  net-tools lighttpd   

#	Configure the server
		
		mkdir -p /var/www/html/Recordings
		echo '$HTTP["url"] =~ "^/Recordings($|/)" { server.dir-listing = "enable" }' >> /etc/lighttpd/conf-enabled/99-unconfigured.conf 
		service lighttpd restart
		ln -s /var/www/html/Recordings /root/Recordings 

# Install audio apps

# 	Load Jacktrip binaries (1.3 and 1.4 -- leaving the plain "jacktrip" command at 1.3 for the moment) 
# 	Load Jacktrip licenses
# 	Load JMess binary 
# 	Load JMess connect-players scripts
# 	Load Qjackctl-old binary
#	Load Qjackctl-new binary
# 	Make them executable

#	Load Jacktrip binaries and licenses
	
	PGJTT-update_logs-3.1.4.sh  "Loading Jacktrip binaries"
	echo                        "Loading Jacktrip binaries"

		
		curl -o /usr/local/bin/jacktrip                https://PGJTT.com/Binaries/jacktrip              
		curl -o /usr/local/bin/jacktrip1.3             https://PGJTT.com/Binaries/jacktrip1.3	        
		curl -o /usr/local/bin/jacktrip1.4             https://PGJTT.com/Binaries/jacktrip1.4	        
		curl -o /usr/local/bin/Jacktrip_Licenses.zip   https://PGJTT.com/Binaries/Jacktrip_Licenses.zip	

#	Load JMess (and a supporting library, libqt5xml5), Qjackctl-old, qjackctl?

	PGJTT-update_logs-3.1.4.sh  "Loading JMess, Qjackctl-old"	
	echo "Loading JMess, Qjackctl-old"

		
		curl -o /usr/local/bin/jmess                   https://PGJTT.com/Binaries/jmess                 
		DEBIAN_FRONTEND='noninteractive' sudo apt-get  install  -y -qq libqt5xml5                       
		curl -o /usr/local/bin/qjackctl-old            https://PGJTT.com/Binaries/qjackctl-old          
		# curl -o /usr/local/bin/qjackctl                https://PGJTT.com/Binaries/qjackctl              

# 	Make all Jacktrip, Jmess and Qjackctl binaries in the local bin directories executable

	PGJTT-update_logs-3.1.4.sh  "Making binaries executabled"
	echo                        "Making binaries executable" 
	
	
		chmod +x /usr/local/bin/*
		
#	Load Ardour DAW

	PGJTT-update_logs-3.1.4.sh  "Loading Ardour DAW"	
	echo                        "Loading Ardour DAW"	

		
 		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq ardour  

#	Add audio-abstraction settings to modules

	PGJTT-update_logs-3.1.4.sh  "Adding module settings"
	echo                        "Adding module settings"	

	
		echo "snd-bcm2835" >>/etc/modules
		echo "snd-dummy" >>/etc/modules

# Install utility commands

	PGJTT-update_logs-3.1.4.sh  "Installing utility commands"
	echo                        "Installing utility commands"

	
		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq zip     
		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq unzip   
		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq screen  

# Install desktop

#	Install xfce4 desktop

	PGJTT-update_logs-3.1.4.sh  "Installing xfce4 desktop -- this one takes a few minutes to install..."
	echo                        "Installing xfce4 desktop -- this one takes a few minutes to install..."	

		
		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq xfce4   

#	Install xfce4 goodies package

	PGJTT-update_logs-3.1.4.sh  "Installing xfce4 goodies -- also a bit slow..."
	echo                        "Installing xfce4 goodies -- also a bit slow..."

	
		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq xfce4-goodies 

#	Install VNC and enable cut/paste

	PGJTT-update_logs-3.1.4.sh  "Installing VNC server"
	echo                        "Installing VNC server"	

	
		# Install needed apps
	
		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq tightvncserver 
		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq autocutsel 
		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq dbus-x11 
	
		# Load an xstartup file that includes 'autocutsel -fork' to enable cut and paste 
		# in and out of VNC from the client computer
	
		mkdir ~/.vnc
		curl -o ~/.vnc/xstartup https://PGJTT.com/Config_Files/xstartup 
		chmod +x ~/.vnc/xstartup 

#	Install Firefox web browser

	PGJTT-update_logs-3.1.4.sh  "Installing Firefox"
	echo                        "Installing Firefox"	

		
		DEBIAN_FRONTEND='noninteractive' sudo apt-get install -y -qq firefox-esr # > /dev/null

#	Upload templates and scripts

	PGJTT-update_logs-3.1.4.sh  "Loading templates and scripts"
	echo                        "Loading templates and scripts"


	# Unzip the Ardour templates into a newly-created .config/ardour6/templates folder

		mkdir -p  ~/.config/ardour6/templates
		curl -o   ~/.config/ardour6/templates/Ardour_Mixer_Templates_3.1.3.zip     https://PGJTT.com/Tmplts_Ardour/Ardour_Mixer_Templates_3.1.3.zip  
		unzip     ~/.config/ardour6/templates/Ardour_Mixer_Templates_3.1.3.zip -d  ~/.config/ardour6/templates  
		rm        ~/.config/ardour6/templates/Ardour_Mixer_Templates_3.1.3.zip 

	# Unzip the Renogate  VST into a newly-created /usr/local/lib/lxvst

		mkdir -p  /usr/local/lib/lxvst
		curl -o   /usr/local/lib/lxvst/Linux-64b-VST2-FREE.zip     https://www.pgjtt.com/VST_Plugins/Linux-64b-VST2-FREE.zip
		unzip     /usr/local/lib/lxvst/Linux-64b-VST2-FREE.zip -d  /usr/local/lib/lxvst/Linux-64b-VST2-FREE  

		
	# Unzip JMess templates into newly-created /usr/local/bin/JMess_Templates folder
	
 		mkdir     /usr/local/bin/JMess_Templates 
		curl -o   /usr/local/bin/JMess_Templates/JMess_Templates.3.1.3.zip         https://pgjtt.com/Tmplts_JMess/JMess_Templates.3.1.3.zip 
		unzip     /usr/local/bin/JMess_Templates/JMess_Templates.3.1.3.zip  -d     /usr/local/bin/JMess_Templates 
		rm        /usr/local/bin/JMess_Templates/JMess_Templates.3.1.3.zip 
		
	# Unzip Connect-Players templates into newly-created directory
	 
		curl -o /usr/local/bin/Connect-Players_Commands.zip     https://pgjtt.com/Tmplts_Connect-Players/Connect-Players_Commands_3.1.4.zip 
		unzip   /usr/local/bin/Connect-Players_Commands.zip -d  /usr/local/bin/
		sudo rm /usr/local/bin/Connect-Players_Commands.zip

		# Make all the scripts in the directory executable

		chmod +x /usr/local/bin/*
		
	# Unzip current_Mixer_connections_templates into /usr/local/etc/
	
		curl -o /usr/local/etc/current_mixer_connections_templates.zip     https://pgjtt.com/Tmplts_Connect-Players/current_mixer_connections_templates_3.1.4.zip 
		unzip   /usr/local/etc/current_mixer_connections_templates.zip -d  /usr/local/etc/
		sudo rm /usr/local/etc/current_mixer_connections_templates.zip
		
		
	# Unzip Desktop-Launchers into newly-created root Desktop directory

		mkdir   ~/Desktop  
		curl -o ~/Desktop_launchers.zip     https://pgjtt.com/Config_Files/Desktop_launchers3.1.4.zip 
		unzip   ~/Desktop_launchers.zip -d  ~/Desktop 
		sudo rm ~/Desktop_launchers.zip

		# Make all the scripts in the directory executable

		chmod +x ~/Desktop/*				

##########################################################################
#
# SECTION: Finish
#
##########################################################################


# Update logs to "Almost-Finished"

	PGJTT-update_logs-3.1.4.sh  "Almost-Finished"
	echo                        "Almost-Finished"





