#!/bin/bash

# Short URL to this raw file: http://git.io/vWPiu
#
# First make sure curl is installed:
#    sudo apt-get install curl
# 
# Then run the script:
#    bash <(curl -sL http://git.io/vWPiu)

#WARNING: Use an all lowercase username.
#LEMONS_NAME=botter
#LEMONS_PASS=
#LEMONS_VNCPASS=
#LEMONS_VNCOFFSET=7001

# BEGIN PROMPTS

echo "Lemon's Ubuntu OSBot install script"

if [ "$EUID" -ne 0 ]; then
	echo "Please run this as root. To drop to a root prompt please run:"
	echo "   sudo su -"
	exit;
fi

echo "Hello! This is a helpful tool to setup a OSBot-ready server on"
echo "Ubuntu 16.04 LTS. Note other Ubuntu distributions are not supported."
if [ -z ${LEMONS_NAME+x} ]; then
	echo "First enter the username of your new non-root account. This should"
	echo "be something non-generic, such as 'potato' or 'yourUserName'."
	echo "Usernames MUST BE all lowercase!"
	read -p "Username: " LEMONS_NAME
else
	echo "Username is $LEMONS_NAME"
fi
if [ -z ${LEMONS_PASS+x} ]; then 
	echo "Now enter the password you will use to login to SSH with the above username"
	read -s -p "Password: " LEMONS_PASS
	echo 
else
	echo "User password was already given."
fi
if [ -z ${LEMONS_VNCPASS+x} ]; then 
	echo "Now enter the password you will use to login to VNC"
	read -s -p "VNC Password: " LEMONS_VNCPASS
else
	echo "VNC password was already given."
fi
if [ -z ${LEMONS_VNCOFFSET+x} ]; then 
	echo "Sets the VNC Offset, defaults to 7001. VNC Port is 5900 + offset, default port is 12901"
	echo "If you don't know what this means, simply press enter and us port 12901 with VNC."
	read -s -p "VNC Offset: " LEMONS_VNCOFFSET
	LEMONS_VNCOFFSET=${LEMONS_VNCOFFSET:-7001};
else
	echo "VNC offset was already given."
fi

echo "And off we go!"
echo ""
echo ""

# END PROMPTS
# BEGIN INSTALL

# Add 32 bit libs
dpkg --add-architecture i386

# Update OS
apt-get --force-yes -y update
apt-get --force-yes -y upgrade
DEBIAN_FRONTEND="noninteractive apt-get -y -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade"

apt-get --force-yes -y install software-properties-common

# Add java PPA
add-apt-repository ppa:webupd8team/java -y
# Add chrome PPA
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee /etc/apt/sources.list.d/google-chrome.list  
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -  
# Add btsync PPA (we use 14.04 cause PPA is fubar atm :\)
#apt-key adv --keyserver keys.gnupg.net --recv-keys 6BF18B15
#echo "deb http://ppa.launchpad.net/tuxpoldo/btsync/ubuntu trusty main" >> /etc/apt/sources.list.d/btsync.list
apt-get -y update

# Add user
adduser $LEMONS_NAME --gecos "" --disabled-password --ingroup sudo
addgroup $LEMONS_NAME
usermod -a -G $LEMONS_NAME $LEMONS_NAME
echo "$LEMONS_NAME:$LEMONS_PASS" | chpasswd

# Install stuff we need
apt-get --force-yes -y install htop nano xorg lxde-core lxterminal tightvncserver google-chrome-stable\
    leafpad autocutsel screen libxext6:i386 libxrender1:i386 libxtst6:i386 libxi6:i386

mkdir -p "/home/$LEMONS_NAME/Desktop"

# Create a osbot run helper
cat >"/home/$LEMONS_NAME/Desktop/OSBotRun.sh" <<EOL
#!/bin/bash

cd "\$HOME/OSBot"
java -jar "\$(ls -Art | grep -e "[Oo][Ss][Bb][Oo][Tt].*\.jar" | tail -n1)"
EOL
chown $LEMONS_NAME:$LEMONS_NAME "/home/$LEMONS_NAME/Desktop/OSBotRun.sh"
chmod +x "/home/$LEMONS_NAME/Desktop/OSBotRun.sh"

# Create .vnc directory
mkdir -p "/home/$LEMONS_NAME/.vnc"

# Setup tightvnc password
echo "$LEMONS_VNCPASS" | tightvncpasswd -f > "/home/$LEMONS_NAME/.vnc/passwd"
chmod  600 "/home/$LEMONS_NAME/.vnc/passwd"

# Setup the xstartup for this
cat >"/home/$LEMONS_NAME/.vnc/xstartup" <<XSTARTUP
#!/bin/sh

xrdb $HOME/.Xresources
xsetroot -solid grey
autocutsel -fork

export XKL_XMODMAP_DISABLE=1
/etc/X11/Xsession
XSTARTUP
chmod +x "/home/$LEMONS_NAME/.vnc/xstartup"

# Setup java
su $LEMONS_NAME -c "cd /home/$LEMONS_NAME ; \
	wget -c --header \"Cookie: oraclelicense=accept-securebackup-cookie\" http://download.oracle.com/otn-pub/java/jdk/8u144-b01/090f390dda5b47b9b721c7dfaa008135/jre-8u144-linux-i586.tar.gz -O /home/$LEMONS_NAME/jre-8u144-linux-i586.tar.gz ; \
	tar -xvzf /home/$LEMONS_NAME/jre-8u144-linux-i586.tar.gz"

update-alternatives --install "/usr/bin/java" java "/home/$LEMONS_NAME/jre1.8.0_144/bin/java" 1
update-alternatives --set java "/home/$LEMONS_NAME/jre1.8.0_144/bin/java"

# Correct permissions
chown -R $LEMONS_NAME:$LEMONS_NAME "/home/$LEMONS_NAME/.vnc"

# Add the startx for the botter user, drops to desktop for quick use
crontab -l > /tmp/tmpcron
echo "@reboot su $LEMONS_NAME -c \"tightvncserver -geometry 1024x768 -depth 24 :$LEMONS_VNCOFFSET\"">> /tmp/tmpcron
crontab /tmp/tmpcron
rm /tmp/tmpcron

# Add some swap for now
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile   none    swap    sw    0   0" >> /etc/fstab

# Download OSBot for first time
sudo su $LEMONS_NAME -c "mkdir -p /home/$LEMONS_NAME/OSBot/"
sudo su $LEMONS_NAME -c "curl -k -o /home/$LEMONS_NAME/OSBot/osbot.jar https://osbot.org/mvc/get"
sudo su $LEMONS_NAME -c "curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash"
sudo su $LEMONS_NAME -c "export NVM_DIR=\"$HOME/.nvm\""
sudo su $LEMONS_NAME -c "chmod +x ~/.nvm/nvm.sh"
sudo su $LEMONS_NAME -c "~/.nvm/nvm.sh"
sudo su $LEMONS_NAME -c "nvm install 6.10.0"

# Correct user permissions
chown -R $LEMONS_NAME:$LEMONS_NAME "/home/$LEMONS_NAME"

echo "Please restart the server with 'sudo reboot' for changes to take effect."
# Reboot server to start everything up