#!/bin/bash

trap "exit 1" TERM
export TOP_PID=$$

if [[ $UID != 0 ]]; then
    echo "Please start the script as root or sudo."
    exit 1
fi

function test {
    "$@"
    local status=$?
    if [ $status -ne 0 ]; then
        echo "Fatal error with $@" >&2
		kill -s TERM $TOP_PID
	else
	    printf "\n[  OK  ]\n"
    fi
    return $status
}

#Shared directories
printf "\nWhich local path do you want to share with LSD? Only this path will be accessible from LSD.\n"
SHARED_FOLDER=/home
read -e -p "Shared directory [$SHARED_FOLDER]: " SHARED_FOLDER
SHARED_FOLDER=${SHARED_FOLDER:-/home}

test mkdir -p $SHARED_FOLDER

#Install docker
echo "Installing docker..."
test wget -qO- https://get.docker.com/ | test sh

test usermod -aG docker $USER

#Start docker
echo "Starting docker..."
service docker start
/etc/init.d/docker start

DISTRO=$(lsb_release -is)
CODENAME=$(lsb_release -cs)

#Install LSD
echo "Installing LSD..."
cat << EOF > /tmp/lsd
#!/bin/bash

echo "Running LSD..."

if [[ "\$(docker ps -f ancestor=nvazquezg/lsd:latest -q 2> /dev/null)" == "" ]]; then
  echo "Checking for new version..."
  echo "sg docker -c docker pull nvazquezg/lsd:latest"
  sg docker -c 'docker pull nvazquezg/lsd:latest'
  sg docker -c 'docker run -e DISPLAY -v $HOME/.Xauthority:/.Xauthority -v "/$SHARED_FOLDER":/mnt/shared --net=host nvazquezg/lsd'
  if [ \$? -ne 0 ]; then
	echo "Error running LSD. Fixing possible problem with groups..." >&2
	echo "This can be fixed executing as root: "
	echo "    usermod -aG docker \$USER"
	echo "Please log off and try again after changing groups." >&2
	sleep 10
	exit
  else
    printf "\n[  OK  ]\n"
  fi
else
  echo "LSD already running"
fi

EOF

mkdir -p /opt/LSD 
cd /opt/LSD && wget -O passwdLSD https://sing.ei.uvigo.es/owncloud/index.php/s/R0LzNzpASoQCOrO/download --no-check-certificate && chmod 666 /opt/LSD/passwdLSD

mv /tmp/lsd /usr/local/bin/lsd
chmod +x /usr/local/bin/lsd

#Shortcut
mkdir -p /opt/LSD/icons/

wget https://sing.ei.uvigo.es/owncloud/index.php/s/rqHWqTGqpJLuEiY/download --no-check-certificate -O /opt/LSD/icons/lsd-icon.png

cat << EOF > /tmp/LSD.desktop
[Desktop Entry]
Encoding=UTF-8
Version=0.1
Name=LSD
GenericName=Lineage Sequence Discovery
Exec=/usr/local/bin/lsd
Terminal=true
Icon=/opt/LSD/icons/lsd-icon.png
Type=Application
Categories=Application;a
Comment=Lineage Sequence Discovery (LSD)
X-GNOME-FullName=LSD

EOF

test mv /tmp/LSD.desktop /usr/share/applications/LSD.desktop

case $DISTRO in
    "Ubuntu")
		if hash gsettings 2>/dev/null; then
			test gsettings set com.canonical.Unity.Launcher favorites "$(gsettings get com.canonical.Unity.Launcher favorites | sed "s/, *'application:\/\/LSD.desktop' *//g" | sed "s/'application:\/\/LSD.desktop' *, *//g" | sed -e "s/]$/, 'application:\/\/LSD.desktop']/")"
		fi
	;;
	"Debian" | "CentOS" | "RedHat" | "Fedora" )
		if hash gsettings 2>/dev/null; then
			test gsettings set org.gnome.shell favorite-apps "$(gsettings get org.gnome.shell favorite-apps | sed "s/, *'LSD.desktop' *//g" | sed "s/'LSD.desktop' *, *//g" | sed -e "s/]$/, 'LSD.desktop']/")"
		fi
	;;
esac

/usr/local/bin/lsd
