VOL-426 This commit solves the problems caused when the voltha user
already exists on the install target. The installer now checks and
re-uses the user if it's already there.

Change-Id: Ie81989a09a66981db6a462799d7d2e9422acd147
diff --git a/install/BashLogin.sh b/install/BashLogin.sh
index 9f85351..7b355e5 100644
--- a/install/BashLogin.sh
+++ b/install/BashLogin.sh
@@ -4,6 +4,7 @@
 echo "vinstall ALL=(ALL) NOPASSWD:ALL" > tmp
 sudo chown root.root tmp
 sudo mv tmp /etc/sudoers.d/vinstall
+sudo chmod 664 /etc/sudoers.d/vinstall
 mkdir .ssh
 chmod 0700 .ssh
 ssh-keygen -f /home/vinstall/.ssh/id_rsa -t rsa -N ''
diff --git a/install/BashLoginTarget.sh b/install/BashLoginTarget.sh
new file mode 100644
index 0000000..972b2f5
--- /dev/null
+++ b/install/BashLoginTarget.sh
@@ -0,0 +1,67 @@
+#!/bin/bash
+
+key="{{ key }}"
+
+lBlue='\033[1;34m'
+green='\033[0;32m'
+orange='\033[0;33m'
+NC='\033[0m'
+red='\033[0;31m'
+yellow='\033[1;33m'
+dGrey='\033[1;30m'
+lGrey='\033[1;37m'
+lCyan='\033[1;36m'
+
+
+if grep voltha /etc/passwd 2>&1 > /dev/null; then
+	echo -e "${yellow}WARNING:${lBlue}a ${yellow}voltha ${lBlue} user exists on the system!!"
+	echo -e "This account will be re-used by the installer. If you encounter any problems"
+	echo -e "please review the account setup to ensure it is correctly set up to run voltha."
+fi
+
+if [  -d /home/voltha ]; then
+	echo -e "${lBlue}A directory ${yellow}/home/voltha ${green}exists ${red}NOT ${lBlue}creating...${NC}"
+else
+	sudo mkdir /home/voltha
+fi
+
+if [ -f /home/voltha/.ssh/id_rsa ]; then
+	echo -e "${lBlue}A ssh key file ${yellow}/home/voltha/ssh/id_rsa ${green}exists ${red}NOT ${lBlue}creating...${NC}"
+else
+	mkdir voltha_ssh
+	ssh-keygen -f ~/voltha_ssh/id_rsa -t rsa -N ''
+	sudo mv voltha_ssh /home/voltha/.ssh
+fi
+
+if [ -f /etc/sudoers.d/voltha ]; then
+	echo -e "${lBlue}A sudoers file ${yellow}/etc/sudoers.d/voltha ${green}exists ${red}NOT ${lBlue}creating...${NC}"
+else
+	echo "voltha ALL=(ALL) NOPASSWD:ALL" > tmp
+	sudo chown root.root tmp
+	sudo mv tmp /etc/sudoers.d/voltha
+fi
+
+if sudo test -f /home/voltha/.ssh/authorized_keys ; then
+	sudo chmod ugo+w /home/voltha/.ssh/authorized_keys
+	echo $key > key.tmp
+	sudo cat key.tmp >> /home/voltha/.ssh/authorized_keys
+	rm key.tmp
+	sudo chmod 400 /home/voltha/.ssh/authorized_keys
+
+else
+	sudo echo $key > /home/voltha/.ssh/authorized_keys
+	sudo chmod 400 /home/voltha/.ssh/authorized_keys
+fi
+
+if grep voltha /etc/passwd 2>&1 > /dev/null; then
+	echo -e "${lBlue}A ${yellow}voltha ${lBlue} user account ${green}exists ${red}NOT ${lBlue}creating...${NC}"
+	sudo chown voltha.`id -gn voltha` /home/voltha/.ssh/authorized_keys
+else
+	sudo useradd -b /home -d /home/voltha voltha -s /bin/bash
+	echo 'voltha:voltha' | sudo chpasswd
+	sudo chown -R voltha.voltha /home/voltha
+fi
+
+rm .bash_login
+logout
+
diff --git a/install/cleanup.sh b/install/cleanup.sh
index 4a2fba9..a08d620 100755
--- a/install/cleanup.sh
+++ b/install/cleanup.sh
@@ -20,6 +20,6 @@
 git checkout ../voltha/voltha.yml
 git checkout ../ofagent/ofagent.yml
 git checkout ../netconf/netconf.yml
-pushd /cord/component/chameleon
+pushd ~/cord/component/chameleon
 git checkout chameleon.yml
 popd
diff --git a/install/installer.sh b/install/installer.sh
index 4279c38..f1f9732 100755
--- a/install/installer.sh
+++ b/install/installer.sh
@@ -26,6 +26,12 @@
 	exit
 fi
 
+if [  "$iUser" == "voltha" ]; then
+	echo -e "${yellow}voltha ${red}can't be used as be install user!!!${NC}"
+	echo -e "${red}Please delete the ${yellow}voltha ${red}user on the targets and create a different installation user${NC}"
+	exit
+fi
+
 # Configure barrier file sizes but only if a value was provided in the config file
 
 if [ -v logLimit ]; then
@@ -61,26 +67,14 @@
 
 	# Generate the pre-configuration script
 	echo -e "${lBlue}Creating the pre-configuration script${NC}"
-	cat <<HERE > bash_login.sh
-#!/bin/bash
-	echo "voltha ALL=(ALL) NOPASSWD:ALL" > tmp
-	sudo chown root.root tmp
-	sudo mv tmp /etc/sudoers.d/voltha
-	sudo mkdir /home/voltha
-	mkdir voltha_ssh
-	ssh-keygen -f ~/voltha_ssh/id_rsa -t rsa -N ''
-	sudo mv voltha_ssh /home/voltha/.ssh
-HERE
-	echo "sudo cat <<HERE > /home/voltha/.ssh/authorized_keys" >> bash_login.sh
+	head -n +1 BashLoginTarget.sh > bash_login.sh
+	echo "" >> bash_login.sh
+	echo -n 'key="' >> bash_login.sh
+	sed -i -e 's/$/"/' $i.pub
 	cat $i.pub >> bash_login.sh
-	echo "HERE" >> bash_login.sh
-	echo "chmod 400 /home/voltha/.ssh/authorized_keys" >> bash_login.sh
-	echo "sudo useradd -b /home -d /home/voltha voltha -s /bin/bash" >> bash_login.sh
-	echo "sudo chown -R voltha.voltha /home/voltha" >> bash_login.sh
-	echo "echo 'voltha:voltha' | sudo chpasswd" >> bash_login.sh
-	echo "rm .bash_login" >> bash_login.sh
-	echo "logout" >> bash_login.sh
+	tail -n +2 BashLoginTarget.sh | grep -v "{{ key }}" >> bash_login.sh
 	rm $i.pub
+
 	# Copy the pre-config file to the VM
 	echo -e "${lBlue}Transfering pre-configuration script to ${yellow}$i${NC}"
 	if [ -d ".test" ]; then