Kovri: vastly improve + fix installation script

- Use correct interpreter
- Create error handler
- Simplify $OSTYPE detection + add DragonFlyBSD
- Backup configuration files if already present
- Remove existing installation if present
- Refactor + fix existing installation methods
- Move binary to dedicated location
- Add BSD-3 license
- Add color output

References:
- monero-project/meta#35
- monero-project/kovri#362
This commit is contained in:
anonimal 2017-02-21 10:18:24 +00:00
parent 39b77b4683
commit d9e25266ef
No known key found for this signature in database
GPG key ID: 66A76ECF914409F1

View file

@ -1,48 +1,127 @@
#!/bin/bash
# TODO(unassigned): /bin/sh isn't smart enough
#!/usr/bin/env bash
# Trivial script to install binary/resources using the makeself installer
# Not to substitute real packaging (this is meant for nightly/branch-tip builds)
# Copyright (c) 2017, The Kovri I2P Router Project
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be
# used to endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
case "$OSTYPE" in
linux*)
#
# Kovri installer script: installs binary/resources for nightly/branch-tip builds
#
# Test if terminal is color capable
if [[ $(tput colors) ]]; then
_red="$(tput setaf 1)"
_green="$(tput setaf 2)"
_yellow="$(tput setaf 3)"
_normal="$(tput sgr0)"
fi
_banner="${_yellow}The Kovri I2P Router Project Installer${_normal}"
echo $_banner
# Error handler
catch() {
if [[ $? -ne 0 ]]; then
echo " ${_red}[ERROR] Failed to install: '$1' ${_normal}"
exit 1
fi
echo " ${_green}[OK]${_normal}"
}
# Get platform
case $OSTYPE in
linux* | freebsd* | dragonfly*)
_data="$HOME/.kovri"
_path="$HOME/.bin"
;;
darwin*)
_data="$HOME/Library/Application Support/Kovri"
_path="$HOME/Library/Desktop"
;;
freebsd*)
_data="$HOME/.kovri"
_path="$HOME/bin"
;;
msys)
_data="$APPDATA\\Kovri"
_path="$HOMEPATH\\Desktop"
_data="$APPDATA/Kovri"
;;
*)
echo "Unsupported platform"
exit 1
false
catch "unsupported platform"
;;
esac
_backup="${_data}-$(date +%Y.%m.%d)" # TODO(anonimal): we'll probably want to backup using revision hash
_binary="kovri"
# Backup existing installation
_config=${_data}/config
_kovri_conf=${_config}/kovri.conf
_tunnels_conf=${_config}/tunnels.conf
# Create bin dir if needed
[ ! -d "$_path" ] && mkdir "$_path"
# Create backup if needed
[ -d "$_data" ] && mv "$_data" "$_backup" && [ -f "${_path}/${_binary}" ] && mv "${_path}/${_binary}" "$_backup"
# Move resources
mkdir "$_data" && mv $(ls -A . | grep -v $(basename "$0")) "$_data" && mv "${_data}/${_binary}" "$_path"
if [ $? -ne 0 ]; then
echo "Failed to install. See above error messages"
exit 1
if [[ -d $_data ]]; then
echo -n "Begin configuration backup..."
if [[ -f $_kovri_conf ]]; then
mv "$_kovri_conf" "${_kovri_conf}.bak"
fi
if [[ -f $_tunnels_conf ]]; then
mv "$_tunnels_conf" "${_tunnels_conf}.bak"
fi
catch "could not backup configuration"
fi
echo "Kovri binary '$_binary' is in $_path"
echo "Consider adding $_path to your \$PATH"
# Remove existing install
_core=${_data}/core
_client=${_data}/client
_installed="$_core $_client/address_book/addresses* $_client/certificates"
if [[ -d $_installed ]]; then
echo -n "Removing existing install"
rm -fr $_installed
catch "could not remove existing install"
fi
# Create new install
_path=$HOME/bin
_binary=kovri
if [[ ! -d $_data ]]; then
echo -n "Creating ${_data}"
mkdir "$_data"
catch "could not create $_data"
fi
if [[ ! -d $_path ]]; then
echo -n "Creating ${_path}"
mkdir "$_path"
catch "could not create $_path"
fi
_resources=(client config $_binary)
for _i in ${_resources[@]}; do
if [[ $_i != $_binary ]]; then
echo -n "Copying $_i to $_data"
cp -fR $_i "$_data"
else
echo -n "Copying $_i to $_path"
cp -f $_i "$_path"
fi
catch "could not copy resource"
done
echo "${_green}Installation success!${_normal}"