#!/bin/bash
# Script author: MrVaykadji http://askubuntu.com/users/176689/mrvaykadji
# Changes made by Andrew <andrew@webupd8.org>:
#  - use the NodeJS PPA to avoid build failure with grunt-cli
#  - use the Popcorn Time icon
#  - added check for apt, dpkg, etc.
#  - added some checks when creating symbolic links
#  - added video playback work-around for Ubuntu 32bit
#  - latest GIT requires ruby-compass
#  - build app CSS with ruby compass and place it in the correct folder
#  - it's no longer needed to replace the package.json file
#  - new way of selecting the architecture to build the app for
#  - added installation of compass via gem for Ubuntu 12.04 because the ruby-compass package from Precise is too old and causes the css compilation to fail
#  - re-wrote some parts of the script to get it to work with Popcorn Time 0.3.0 beta
#  - disable 32bit playback work-around, it's no longer needed
#  - Popcorn Time now requires libudev1 instead of libudev0 so use reverse work-around for systems without libudev1 (like Ubuntu 12.04)
#  - use dev-0.3 branch

function checkAPT()
{
sleep 1
for lock in synaptic update-manager software-center apt-get "dpkg " aptitude
do
 if ps -U root -u root u | grep "$lock" | grep -v grep > /dev/null; then 
  echo "Installation won't work. Please close $lock first then try again.";
  exit
 fi
done
}

#information
echo "
Popcorn-Time 0.3.0 beta for Ubuntu
------------------------------------
WARNING: Popcorn Time streams movies from Torrents. Downloading copyrighted material may be illegal in your country. Use at your own risk.
"

#verify awareness
read -p "Do you wish to install Popcorn Time [y/n] ? "
if [ "$REPLY" == "y" ] ; then
 sudo echo -e "\nThis may take a while...\n"
else
 exit 0
fi

#dependencies install
checkAPT
echo "- Installing dependencies from repositories..."
sudo add-apt-repository -y ppa:chris-lea/node.js
sudo apt-get update
if [ `lsb_release -cs` == "precise" ]; then
#work-around for Ubuntu precise having a too old ruby-compass
    sudo apt-get install nodejs git wget rubygems -y
    sudo gem install compass && echo -e "...Ok.\n"
else
    sudo apt-get install nodejs ruby-compass git wget -y && echo -e "...Ok.\n"
fi

#npm install for CLI
echo "- Installing CLI from 'npm'..."
sudo npm install -g grunt-cli bower && echo -e "...Ok.\n"

#clean up ~/tmp folder or npm install below will fail
sudo rm -rf ~/tmp

#git clone
echo "- Cloning GITHUB repository..."
git clone https://github.com/popcorn-official/popcorn-app.git && echo -e "...Ok.\n"

#check architecture
if [ -n "`arch | grep 64`" ] ; then
    #64bits
    ARCH=linux64
else
    #32bits
    ARCH=linux32
fi

#repair broken nodejs symlink
if [ ! -e /usr/bin/node ]; then
 sudo ln -s /usr/bin/nodejs /usr/bin/node
fi

#use dev-0.3 branch
cd popcorn-app/
git checkout dev-0.3

#install NPM dependencies
echo "- Installing NPM dependencies..."
npm install && echo -e "...Ok.\n"

#build with grunt
echo "- Building with 'grunt'..."
grunt build --platforms=all && echo -e "...Ok.\n"

#libudev1 fix
#cd build/releases/Popcorn-Time/$ARCH/
#if [ ! -e /lib/x86_64-linux-gnu/libudev.so.0 ] && [ ! -e /lib/i386-linux-gnu/libudev.so.0 ]; then
# sudo sed -i 's/\x75\x64\x65\x76\x2E\x73\x6F\x2E\x30/\x75\x64\x65\x76\x2E\x73\x6F\x2E\x31/g' Popcorn-Time/Popcorn-Time
#fi
if [ "$ARCH" == "linux64" ] ; then
 if [ ! -e /lib/x86_64-linux-gnu/libudev.so.1 ]; then
  sudo ln -s /lib/x86_64-linux-gnu/libudev.so.0 /lib/x86_64-linux-gnu/libudev.so.1
 fi
elif [ "$ARCH" == "linux32" ] ; then
 if [ ! -e /lib/i386-linux-gnu/libudev.so.1 ]; then
  sudo ln -s /lib/i386-linux-gnu/libudev.so.0 /lib/i386-linux-gnu/libudev.so.1
 fi
fi

#Copy program into /opt
echo "- Installing Popcorn-Time in '/opt/Popcorn-Time/'"
sudo mkdir -p /opt
sudo cp -rf build/releases/Popcorn-Time/"$ARCH"/Popcorn-Time /opt
sudo chmod +x /opt/Popcorn-Time/Popcorn-Time
sudo rm -f "/usr/bin/popcorn-time"
sudo ln -s /opt/Popcorn-Time/Popcorn-Time /usr/bin/popcorn-time && echo -e "...Ok.\n"

#fixing playback on 32bit
#if [ "$ARCH" == "linux32" ] ; then
# sudo rm -f "/opt/Popcorn-Time/libffmpegsumo.so"
# wget https://github.com/hotice/webupd8/raw/master/libffmpegsumo.so -O /tmp/libffmpegsumo.so
# sudo cp -f /tmp/libffmpegsumo.so /opt/Popcorn-Time/
#fi

#testing app
echo "The application 'Popcorn-Time' will now be tested for 7 secondes.
"
build/releases/Popcorn-Time/"$ARCH"/Popcorn-Time/Popcorn-Time &
sleep 7
killall Popcorn-Time
read -p "
Did you see the Popcorn-Time application ?
If yes, do you wish to remove all the unecessary files now that the program works [y/n] ? "
if [ "$REPLY" == "y" ] ; then
    #remove building files
    echo "- Removing building files now unwanted..."
    cd ..
    sudo rm -rf popcorn-app ~/tmp ~/.npm && echo -e "...Ok.\n"
fi

#create launcher
echo "- Creating launcher/shortcut in '/usr/share/applications/'..."
wget https://github.com/hotice/webupd8/raw/master/popcorntime.png -O /tmp/popcorntime.png
sudo cp -f /tmp/popcorntime.png /usr/share/pixmaps/
echo "[Desktop Entry]
Comment=Watch movies in streaming with P2P.
Name=Popcorn Time
Exec=/usr/bin/popcorn-time
StartupNotify=false
Type=Application
Icon=popcorntime" | sudo tee /usr/share/applications/popcorn-time.desktop
sudo chmod +x /usr/share/applications/popcorn-time.desktop && echo -e "...Ok.\n"

echo "
Installation done ! Popcorn-time should be now available through : 
- Dash
- Commandline 'popcorn-time'"

exit 0