#!/bin/bash
# MAAS
# Multiplexer Adaptive Adminstrator Solution
# Configuration file

# Copyright (C) 2016 Aurélien DESBRIÈRES <aurelien@hackers.camp> 

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


tput clear

printf "\033[1;32mWelcome to MAAS - Multiplexer Adaptive Adminstrator Solution\033[0m%s\\n"
printf "\033[1;32mThis program will install needed dependencies\033[0m%s\\n"
printf "\033[1;32mTHIS VERSION OF MAASS WILL GRAB MUSSH TO USE IT WITHOUT INSTALL IT ON YOUR SYSTEM\033[0m%s\\n"
printf "\033[1;32mPlease report your needs to:\033[0m aurelien@hackers.camp%s\\n"
printf "\033[1;32mYou need root privileges to run it\033[0m%s\n"


the_user="$(whoami)"


# request the user approbation
while true; do
    read -p "Do you wish to run MAAS $the_user? Y/n " yn
    case $yn in
	[Yy]* ) break;;
	[Nn]* ) exit;;
	* ) echo "Please answer Yes or no.";;
    esac
done

# Grab MUSSH
while true; do
    read -p "Do you need MAAS to grab MUSSH for you? Y/n " yn
    case $yn in
	[Yy]* ) wget http://git.savannah.gnu.org/cgit/maas.git/tree/mussh-1.0.tgz ; tar xvf mussh-1.0.tgz ; break;;
	[Nn]* ) break;;
	* ) echo "Please answer Yes or no.";;
    esac
done


# Vefify and install the dependencies if needed
#printf "%s\nThe software will now get the needed dependencies for your\noperating system $the_user%s\n"

# load installation of the dependencies
# verification of the package manager
# NO MORE NEEDED FROM THE VERSION 0.2 BECAUSE MAAS GRAB ITSELF THE SOURCE OF MUSSH
# BUT WE KEEP THAT PART TO BE READY TO ADD SOME COMING DEPENDENCIES
#command_exists () {
#    type "$1" &> /dev/null ;
#}
#
## auto-selection of the package manager
## dnf - Fedora - fredora
#if command_exists dnf ; then
#    sudo dnf install mussh
#else
#    # yum - RedHat - old fredora
#    sudo yum install mussh
#fi
## apt - Debian - Trisquel
#if command_exists apt-get ; then
#    sudo apt-get install mussh
#fi
## pacman - Archlinux - Parabola
#if command_exists pacman ; then
#    sudo pacman -Sy mussh
#fi
## apt - Rooted smartphone
#if command_exists apt ; then
#    sudo apt install mussh
#fi
# 

# Verify that directory MAAS exist or create it (-p option)
mkdir -p /home/"$the_user"/MAAS/


# Verify that maas-hostfile exist or create it
touch /home/"$the_user"/MAAS/maas-hostfile || exit


# Clear or not the host target file
while true; do
    read -p "Do you wish to create/clear your old host file or use it $the_user? C/u " cu
    case $cu in
	[Cc]* ) # Clear the target file
		printf "%s\nMAAS will now create/clear your host list of remote machine%s\n%s\n"
		truncate -s 0 /home/"$the_user"/MAAS/maas-hostfile
		break;;
	[Uu]* ) break;;
	* ) echo "Please answer Clear or use.";;
    esac
done

# append or not new target in the target file
while true; do
    read -p "Do you wish to target new machine $the_user? Y/n " yn
    case $yn in
	[Yy]* ) # adding remote machine to the list of hostfile
		printf "MAAS will now create a host list of remote machine to analysis%s\n"
		printf "Enter one IP by line (ex:root@192.168.0.2) and hit Ctrl+d to finish%s\n"
		while
		      read line
		do
		    my_array=("${my_array[@]}" $line)
		done
		printf -- '%s\n ' "${my_array[@]}" | tee -a /home/"$the_user"/MAAS/maas-hostfile
		set -f /home/"$the_user"/MAAS/maas-hostfile
		break;;
	[Nn]* ) break;;
	* ) echo "Please answer Yes or no.";;
    esac
done


# append to the private key
while true; do
    read -p "Do you need to use a private key to connect on this cluster network? Y/n " yn
    case $yn in
	[Yy]* ) # adding a key
	    printf "%s\nHere is the list of your keys on this system $the_user%s\n"
	    ls -la /home/"$the_user"/.ssh/
	    printf "%s\nWrite the name of the private key you wish to use $the_user%s\n"
	    read private
	    break;;
	[Nn]* ) break;;
	* ) echo "Please answer Yes or no.";;
    esac
done

#cp maas update.sh operator.sh scan.awk ~/MAAS/

#chmod +x ~/MAAS/maas ~/MAAS/update.sh


echo "Loading MAAS please wait"

sleep 2

. maas

maas
