#!/bin/bash
#
###############################################################################
#                                                                             
#                     How to run this script?                               
#                     http://pi-star:4200                                    
#                     https://pi-star:4200                                    
#          Mourse right click Copy and Paste
# curl -Ls https://www.bi7jta.org/files/W0CHP-Dashboard-patch/pistar-M2_patch | sudo bash
#
#                            by @BI7JTA                                       
#                                                                             
###############################################################################
#
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi

exec 200>/var/lock/pistar-update.lock || exit 1
if ! flock -n 200 ; then
  echo -e "Another instance is already running...\n"
  exit 1
fi

TICK="[✓]"
INFO="[i]"

mount -o remount,rw / # for shits and giggles


change_git_resp() {
  
	mkdir -p /home/pi-star/w0chp_patch-M2-patch
	cd /home/pi-star/w0chp_patch-M2-patch
	
	rm -f bin.git.config
	rm -f sbin.git.config
	rm -f dashboard.git.config
	rm -f pistar-update
	
	#Change bin/.git
	curl -OL  https://www.bi7jta.org/files/W0CHP-Dashboard-patch/bin.git.config
	mv bin.git.config /usr/local/bin/.git/config
	cat /usr/local/bin/.git/config 
	
	#Change sbin/.git
	curl -OL https://www.bi7jta.org/files/W0CHP-Dashboard-patch/sbin.git.config
	mv sbin.git.config /usr/local/sbin/.git/config
	cat /usr/local/sbin/.git/config 
	
	#Change dashboard/.git
	curl -OL https://www.bi7jta.org/files/W0CHP-Dashboard-patch/dashboard.git.config
	mv dashboard.git.config /var/www/dashboard/.git/config
	cat /var/www/dashboard/.git/config 
	
	#Change shellinabox to NonSSL
	curl -OL https://www.bi7jta.org/files/W0CHP-Dashboard-patch/shellinabox
	mv shellinabox /etc/default/shellinabox
	cat /etc/default/shellinabox 

}

git_update() {
	# Set the function variables
	gitFolder=${1}
	# get proper W0CHP dash branch user is running...
	dash_branch="$( git --git-dir=${gitFolder}/.git branch | grep '*' | awk {'print $2'} )"

  git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git pull -q origin master &> /dev/null
  git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git reset --hard origin/master
}

main_function() {
  echo "Change git resp from W0CHP to BPi-M2/BI7JTA github"
  change_git_resp 

	# Make the disk writable and stop cron to prevent it from making it R/O
    	systemctl stop cron.service > /dev/null 2<&1
    	pkill pistar-hourly.cron > /dev/null 2>&1
    	pkill pistar-daily.cron > /dev/null 2>&1
 

	echo "Stopping Services..."
	pistar-services fullstop > /dev/null 2>&1
	echo -e "${TICK} Done!\n"

	# ensure FS is RW
	touch /root/.WPSD-updating > /dev/null 2>&1
	if [ ! -f /root/.WPSD-updating ] ; then
	    touch /root/.WPSD-updating > /dev/null 2>&1
	    if [ ! -f /root/.WPSD-updating ] ; then
		echo -e "Filesystem could not enter R/W mode...please try updating again.\n"
		exit 1
	    fi
	fi

	echo "Updating W0CHP-PiStar-Dash Scripts and Support Programs..."
	git_update /usr/local/sbin
	#systemctl daemon-reload > /dev/null 2>&1
	echo -e "${TICK} Done!\n"

	echo "Updating W0CHP-PiStar-Dash Digital Voice and Related Binaries..."
	git_update /usr/local/bin
	echo -e "${TICK} Done!\n"

	echo "Updating W0CHP-PiStar-Dash Web Dashboard Software..."
	git_update /var/www/dashboard
	echo -e "${TICK} Done!\n"
 
 
	echo "Starting Services..."
	pistar-services start > /dev/null 2>&1
	echo -e "${TICK} Done!\n"
	
	echo "Updates complete, syncing disk cache..."
	rm /root/.WPSD-updating > /dev/null 2>&1
	/bin/sync
	/bin/sync
	/bin/sync
	systemctl start cron.service > /dev/null 2<&1
	echo -e "${TICK} Update Process Finished!"
	
  echo "Run patch for BPi-M2 done! "
}

	if [ -t 1 ]; then
 		# run via terminal, only output to screen
 		main_function
	else
 		# if not run via terminal, log everything into a log file
 		main_function >> /var/log/pi-star/pi-star_update.log 2>&1
	fi
	
  echo "Execute sudo service shellinabox restart , if using https://pi-star:4200 SSL connection."
	
	#BPiM2：
	#dpkg -s php7.0-zip
  #sudo apt-get install -y php7.0-zip
  #sudo systemctl restart php7.0-fpm

  #BPi-M2z：
  #dpkg -s php7.3-zip
  #sudo apt-get install -y php7.3-zip
  #sudo systemctl restart php7.3-fpm

exit 0