Skip to content

Instantly share code, notes, and snippets.

@savely-krasovsky
Last active October 18, 2016 21:59
Show Gist options
  • Save savely-krasovsky/08baf825a3d642f773ca776ee32c33c0 to your computer and use it in GitHub Desktop.
Save savely-krasovsky/08baf825a3d642f773ca776ee32c33c0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Trap Ctrl+C interupt
trap finish INT
exec_cmd_nobail() {
bash -c "$1"
}
exec_cmd() {
exec_cmd_nobail "$1" || bail
}
# Ok message
ok()
{
echo -e "\e[32mOK.\e[0m"
}
# Error message
err()
{
echo -e "\e[33mOK, but maybe some warnings or errors have occurred. See '.err_usa' file.\e[0m"
}
# If file bigger than 0 bytes - error, else - ok
check() {
if [ "$1" == "usa" ]
then
# Import vars
. ../.temp_usa
# Get current size of error log file
CURRSIZE=`stat -c %s ../.err_usa`
# Replace size with new current size
bash -c "perl -p -i.old -e 's/(?<=SIZE=\")\d+/$CURRSIZE/g' ../.temp_usa"
else
. .temp_usa
CURRSIZE=`stat -c %s .err_usa`
bash -c "perl -p -i.old -e 's/(?<=SIZE=\")\d+/$CURRSIZE/g' .temp_usa"
fi
# If current size bigger than saved - error, else - ok
[[ $CURRSIZE -gt $SIZE ]] && err || ok
}
# Func to handle Ctrl+C
finish() {
echo "Stopping NodeJS daemon..."
forever stopall &> /dev/null
exit
}
# Creating temp cfg file
touch .temp_usa
touch .err_usa
# If file empty - add two zero vars
if [ ! -s .temp_usa ]
then
exec_cmd "echo 'MAIN=\"0\"' > .temp_usa"
exec_cmd "echo 'ADD=\"0\"' >> .temp_usa"
exec_cmd "echo 'SIZE=\"0\"' >> .temp_usa"
fi
# Make executable to import vars
chmod +x .temp_usa
. .temp_usa
# Install basic things
if [ "$MAIN" != "1" ]
then
echo "Installing git, curl and basic tools for building..."
bash -c "sudo apt-get install -y git curl build-essential 2>> .err_usa 1> /dev/null"
check
echo "Installing nodejs..."
bash -c "curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - 2>> .err_usa 1> /dev/null"
bash -c "sudo apt-get install -y nodejs 2>> .err_usa 1> /dev/null"
check
echo "Installing Chromium..."
bash -c "sudo apt-get install -y chromium chromium-l10n chromium-inspector 2>> .err_usa 1> /dev/null"
check
echo "Cloning, please, enter your GitLab credentials..."
git clone https://gitlab.com/mr14mr/usa-game.git
ok
# Set installed flag
bash -c "perl -p -i.old -e 's/(?<=MAIN=\")\d+/1/g' .temp_usa"
fi
cd usa-game
# Install project things
if [ "$ADD" != "1" ]
then
echo "Installing all node modules for the project..."
bash -c "npm install 2>> ../.err_usa 1> /dev/null"
check usa
echo "Installing bower and forever..."
bash -c "sudo npm install -g bower forever 2>> ../.err_usa 1> /dev/null"
check usa
# echo "Installing all bower modules for the project..."
# bash -c "bower install 2>> ../.err_usa 1> /dev/null"
# check usa
echo "Creating tmp.txt file for the project..."
touch tmp.txt
if [[ -z "$2" ]]
then
exec_cmd "echo '1' > tmp.txt"
else
exec_cmd "echo '$2' > tmp.txt"
fi
ok
# Set installed flag
bash -c "perl -p -i.old -e 's/(?<=ADD=\")\d+/1/g' ../.temp_usa"
fi
# Start NodeJS and Chromium
echo "Starting NodeJS daemon..."
bash -c "forever start index.js 2>> ../.err_usa 1> /dev/null"
sleep 2
check usa
echo "Starting Chromium..."
if [ "$1" == "-k" ]
then
chromium --kiosk http://localhost:8080/game &> /dev/null
else
chromium http://localhost:8080/game &> /dev/null
fi
ok
echo "Stopping NodeJS daemon..."
bash -c "forever stopall 2>> ../.err_usa 1> /dev/null"
check usa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment