#!/bin/bash

####################################
# Writen by: Juan Lorenzana        #
#                                  #
# Unix Installer Script            #
# Proprietary and Confidential     #
# Copyrighted() 2022               #
# Digital Asset Management         #
####################################

####################################
# Perform some prerequisite checks #
####################################

echo -e "Checking for requisites\n";
USER=$(whoami);
echo -e "You are $USER\n";
sleep 1;

downloader=0;
downloader2=0;
ecode=0;

echo "Checking for ..... wget";
if ! command -v wget &> /dev/null;
then
	echo "The command: wget could not be found";
else 
	echo "Found commnand: wget";
	downloader=1;
	ecode=1;
fi
sleep 1;

echo -e "\nChecking for ..... curl";
if ! command -v curl &> /dev/null;
then
	echo "The command: curl could not be found";
else 
	echo "Found commnand: curl";
	downloader2=2;
	ecode=1;
fi

sleep 3;

if [[ $ecode -eq 0 ]] 
then
	echo -e "\nMissing or could not find wget or curl";
	echo -e "\nPlease install wget or curl to use this installer";
	exit 0;
fi

echo -e "\nChecking for ..... tar";
if ! command -v tar &> /dev/null;
then
	echo "The command: tar could not be found";
else 
	echo "Found commnand: tar";
	ecode=1;
fi
sleep 1;

echo -e "\nChecking for ..... gunzip";
if ! command -v gunzip &> /dev/null;
then
	echo "The command: gunzip could not be found";
else 
	echo "Found commnand: gunzip";
	ecode=1;
fi
sleep 3;


echo -e "\n\nLaunching Installer";
sleep 2;


echo -e "Using $downloader\n";
sleep 2;


run_exit(){
	echo -e "\n\nThank you for using our Installer script";
	echo -e "\n\nYou can run the simplewallet by typing:  ./simplewallet";
	echo -e "\n\n\n\n";
	exit 0;
}

get_home(){
	local result; result="$(getent passwd "$1")"  || return;
	echo $result | cut -d : -f 6;
}

###################################################
#  Function - consensus                            #
###################################################
consensus () {
	echo -e "\nDownloading the latest consensus file";
	if [[ $downloader -eq 1 ]] 
	then
		echo -e "\nUsing wget to download the consensus file";
		sleep 2;
		echo -e "Downloading consensus file\n\n";
		wget "http://www.damcoin.io/package/consensus/consensus.tar.gz";
		sleep 4; 
	else
		echo -e "\nUsing curl to download the consensus file";
		sleep 2;
		echo -e "Downloading consensus file\n\n";
		curl -O "http://www.damcoin.io/package/consensus/consensus.tar.gz";
		sleep 4; 
	fi
	sleep 2;
	echo -e "Identifying where to save the consensus file - please wait\n";
	sleep 1;
	echo -e "Identifying user: $USER";
	sleep 3;
	da_home="$(get_home $USER)"
	echo -e "Found Home directory: $da_home";
	DIR="$da_home/.DAMcoin";
	if [ -d "$DIR" ]; then
		echo -e "Found $DIR directory";
		sleep 3;
		echo -e "Installing consensus file - this can take a while, please wait\n";
		mv consensus.tar.gz $DIR/consensus.tar.gz;
		cd $DIR; gunzip consensus.tar.gz
		echo -e "Extracting consensus file\n";
		cd $da_home/.DAMcoin; tar xf consensus.tar;
		sleep 2;
	else
		echo -e "Creating directory for consensus file\n";
		mkdir $DIR;
		chmod 755 $DIR;
		echo -e "Installing consensus file - this can take a while, please wait\n";
		mv consensus.tar.gz $DIR/consensus.tar.gz;
		cd $DIR; gunzip consensus.tar.gz
		echo -e "Extracting consensus file\n";
		cd $da_home/.DAMcoin; tar xf consensus.tar;
		sleep 2;
	fi
	echo -e "Cleaning up\n";
	cd $DIR; rm consensus.tar;
	sleep 3;

} # End function - consensus

###################################################
#  Function - show_help                           #
###################################################
show_help () { 
	echo -e "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
	echo -e "                                                   Help Menu\n\n";
	echo -e "This installer downloads the latest Unix simplewallet for the DAM coin.  It automatically upgrades your simplewallet if you";
	echo -e "already have downloaded the simplewallet before and saves the previous version as simplewallet.sav.";
	echo -e "\nThe prerequisites to run the installer is that you must be on a Unix system with the following programs installed: ";
	echo -e "   tar";
	echo -e "   gunzip";
	echo -e "   wget or curl";
	echo -e "\nAdditionally, this script is capable of boot strapping the simplewallet by downloading the conensus file for the blockchain";
	echo -e "All the necessary files are downloaded and installed into the .DAMcoin directory located in your home directory.  It does not";
	echo -e "make any effort to preserve previous synced blockchain information and literally overrights what was previously there.";
	sleep 5;
	echo -e "\nThis script also assumes that it is installed in the same directory where you currently have the simplewallet program installed";
	sleep 10;
	echo -e "\nPress Enter to return to the Main Menu";
	read EVAR;

} # End function - show_help

###################################################
#  Function - download                            #
###################################################
download () {
	echo -e "\nDownloading latest DAM Simple Wallet";
	if [[ $downloader -eq 1 ]] 
	then
		echo -e "\nUsing wget to download DAM Simple Wallet";
		sleep 2;
		echo -e "If you have another version, attempting to save previous version";
		mv simplewallet simplewallet.sav
		sleep 3;
		echo -e "Downloading simplewallet\n\n";
		wget "http://www.damcoin.io/package/simplewallet";
		sleep 4; 
	else
		echo -e "\nUsing curl to download DAM Simple Wallet";
		sleep 2;
		echo -e "If you have another version, attempting to save previous version";
		mv simplewallet simplewallet.sav
		sleep 3;
		echo -e "Downloading simplewallet\n\n";
		curl -O "http://www.damcoin.io/package/simplewallet";
		sleep 4; 
	fi
		echo -e "Updating permissions";
		chmod 755 simplewallet
		sleep 3;
		echo -e "Done downloading DAM Simple Wallet";
		sleep 4;
			

} # Function - download

###################################################
#  Function - menu                                #
###################################################
menu () {

echo -e "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
echo -e "                   Digital Asset Management";
echo -e "                   ________________________\n\n";
echo -e "    Wallet Instaler Version 2.25";
echo -e "    Published August 17, 2022\n";
echo -e " Please select one of the following:";
echo -e " 1 - Download the latest version of the DAM Simple Wallet";
echo -e " 2 - Boot strap the latest consenus file for your wallet";
echo -e " 3 - Print the Help Screen\n";
echo -e " 4 - Exit\n";

read VAR;

if [[ $VAR -eq 1 ]]
then
	download;
	menu;
elif [[ $VAR -eq 2 ]]
then
	consensus;
	menu;
elif [[ $VAR -eq 3 ]]
then
	show_help;
	menu;
else 
	run_exit;
fi

}  # End Funciton - menu


menu;
