AqualinkD/extras/aqua.sh

235 lines
5.8 KiB
Bash
Raw Normal View History

2018-09-21 15:19:26 +00:00
#!/bin/bash
2018-12-04 17:45:09 +00:00
#
# Run using
# curl -s https://raw.githubusercontent.com/sfeakes/AqualinkD/master/extras/aqua.sh | bash -s <script parms> --
#
# list latest release version (This is zip install)
# curl --silent "https://api.github.com/repos/sfeakes/AqualinkD/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")'
#
# list latest dev version
2019-07-30 13:48:26 +00:00
# curl --silent "https://raw.githubusercontent.com/sfeakes/AqualinkD/master/version.h" | grep AQUALINKD_VERSION | cut -d '"' -f 2
2018-12-04 17:45:09 +00:00
#
2018-12-04 17:57:19 +00:00
2018-12-04 17:45:09 +00:00
NAME="AqualinkD"
SOURCE_LOCATION="/extras/aqua.sh"
HOMEDIR=~
eval HOMEDIR=$HOMEDIR
BASE="$HOMEDIR/software/"
2018-09-21 15:19:26 +00:00
AQUA="$BASE/AqualinkD"
2018-12-04 17:45:09 +00:00
AQUA_GIT="$BASE/AqualinkD/.git"
2018-09-21 15:19:26 +00:00
echoerr() { printf "%s\n" "$*" >&2; }
function print_usage {
echoerr "No arguments supplied, please use use:-"
2018-12-04 17:45:09 +00:00
echoerr "$0 release <- Download & install latest $NAME stable release."
echoerr "$0 latest <- Download & install latest $NAME version. (development release)"
echoerr "$0 force_latest <- Force latest option (don't run any version checks)"
2020-08-28 19:12:38 +00:00
echoerr "$0 install <- Install to local filesystem, (config files will not be overwritten)"
2018-12-04 17:45:09 +00:00
echoerr "$0 clean <- Remove everything, including configuration files"
2019-07-30 13:48:26 +00:00
echoerr "$0 <option> downloadonly <- Download using one of above options, but don't unstall"
2018-09-21 15:19:26 +00:00
echoerr ""
}
2018-09-21 16:00:54 +00:00
function upgrade_self {
2018-12-04 17:45:09 +00:00
#echo "NOT UPGRADING SELF, MAKE SURE TO EDIT BEFORE COMMIT"
#return;
2018-12-04 17:57:19 +00:00
LOC=$(realpath $0)
if [ "$LOC" == "/nas/data/Development/Raspberry/AqualinkD/extras/aqua.sh" ]; then
echo "NOT UPGRADING SELF, (Development environment)"
return
2018-12-04 18:20:53 +00:00
elif [ "$0" == "bash" ] || "$0" == "$AQUA/$SOURCE_LOCATION" ]; then
return
2018-12-04 17:57:19 +00:00
fi
2018-12-04 17:45:09 +00:00
if [ -f "$AQUA/$SOURCE_LOCATION" ]; then
cp "$AQUA/$SOURCE_LOCATION" $0
2018-09-21 16:00:54 +00:00
fi
}
2018-09-21 15:19:26 +00:00
function git_install {
sudo apt-get install git
}
2018-12-04 17:45:09 +00:00
function local_version {
if [ -f $AQUA/version.h ]; then
echo `cat $AQUA/version.h | grep AQUALINKD_VERSION | cut -d '"' -f 2 | tr '\n' ' '`
else
echo 0
fi
}
function git_release_version {
echo `curl --silent "https://api.github.com/repos/sfeakes/AqualinkD/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")'`
}
2019-07-30 13:48:26 +00:00
function get_development_version {
echo `curl --silent "https://raw.githubusercontent.com/sfeakes/AqualinkD/master/version.h" | grep AQUALINKD_VERSION | cut -d '"' -f 2`
}
2018-09-21 15:19:26 +00:00
function remove {
2018-12-04 17:45:09 +00:00
#cd ~
2018-09-21 15:19:26 +00:00
if [ -d "$AQUA" ]; then
2018-12-04 17:45:09 +00:00
if [ -L "$AQUA" ]; then
unlink "$AQUA"
else
rm -rf "$AQUA"
fi
2018-09-21 15:19:26 +00:00
fi
}
2018-12-04 17:45:09 +00:00
function isvalid_gitinstall {
#cd "$AQUA"
isgit=$(git --git-dir "$AQUA_GIT" status 2>&1)
return $?
}
2018-12-04 16:58:26 +00:00
2018-12-04 17:45:09 +00:00
function development_version_download {
2019-07-30 13:48:26 +00:00
echo "Upgrading $NAME from $(local_version) to $(get_development_version)"
2018-12-04 17:45:09 +00:00
if isvalid_gitinstall; then
update
else
force_dev_download
2018-12-04 17:41:28 +00:00
fi
2018-12-04 17:45:09 +00:00
}
function force_dev_download {
remove
make-base
2018-09-21 15:19:26 +00:00
2018-12-04 17:45:09 +00:00
#cd $BASE
git clone "https://github.com/sfeakes/AqualinkD.git" "$AQUA"
2018-09-21 15:19:26 +00:00
}
2018-12-04 17:45:09 +00:00
function update {
#cd ~
#cd $AQUA
git --git-dir "$AQUA_GIT" pull
2018-09-21 15:19:26 +00:00
}
function clean {
2018-12-04 17:45:09 +00:00
#cd ~
2018-09-21 16:00:54 +00:00
sudo "$AQUA/release/install.sh" "clean"
2018-09-21 15:19:26 +00:00
remove
}
function install {
2018-12-04 19:14:38 +00:00
if [ ! -f "$AQUA/release/install.sh" ]; then
echo "ERROR Can not find install script, please download from git using either :-"
echo "$0 release"
echo "$0 latest"
exit 1
fi
2018-12-04 17:45:09 +00:00
echo "Installing $NAME"
#cd ~
2018-09-21 15:19:26 +00:00
sudo "$AQUA/release/install.sh"
}
2018-12-04 17:45:09 +00:00
function make-base {
if [ ! -d "$BASE" ]; then
mkdir -p $BASE
fi
if [ ! -d "$BASE" ]; then
echoerr "Error Can't create $BASE";
exit 1;
fi
}
function release_version_download {
version=$(local_version)
git_version=$(git_release_version)
if [ "v$version" != "$git_version" ]; then
echo "Installing $NAME $git_version"
make-base
#cd $BASE
# This is correct way, but tar is made with crap dir structure
#tar_url=$(curl --silent "https://api.github.com/repos/sfeakes/AqualinkD/releases/latest" | grep -Po '"tarball_url": "\K.*?(?=")')
#curl --silent -L "$tar_url" | tar xz
# Use this tar instead, it's correct dir structure
curl --silent -L "https://github.com/sfeakes/AqualinkD/archive/$git_version.tar.gz" | tar xz -C "$BASE"
2019-07-30 13:48:26 +00:00
ver=$(echo $git_version | sed 's/^[^0-9]*//' )
2018-12-04 17:45:09 +00:00
if [ -d "$AQUA" ]; then
2019-07-30 13:48:26 +00:00
if [ -d "$AQUA-$version" ]; then
2018-12-04 17:45:09 +00:00
mv "$AQUA" "$AQUA-$RANDOM"
2019-07-30 13:48:26 +00:00
else
mv "$AQUA" "$AQUA-$version"
2018-12-04 17:45:09 +00:00
fi
fi
2019-07-30 13:48:26 +00:00
if [ -L "$AQUA" ]; then
unlink "$AQUA"
fi
2018-12-04 17:45:09 +00:00
ln -s "$BASE/AqualinkD-$ver" "$AQUA"
else
echo "Local $NAME version $version is latest, not downloading"
fi
}
function check_git_installed {
# Check git is installed
command -v git >/dev/null 2>&1 || { echoerr "git is not installed. Installing"; git_install; }
command -v git >/dev/null 2>&1 || { echoerr "git is not installed. Aborting"; exit 1; }
}
function check_curl_installed {
command -v curl >/dev/null 2>&1 || { echoerr "curl is not installed. Installing"; exit 1; }
}
2018-09-21 15:19:26 +00:00
# Check something was passed
if [[ $# -eq 0 ]]; then
print_usage
exit
fi
# Pass command line
2018-12-04 17:45:09 +00:00
if [ "$1" == "release" ]; then
check_curl_installed
release_version_download
elif [ "$1" == "latest" ]; then
check_curl_installed
check_git_installed
development_version_download
elif [ "$1" == "force_latest" ]; then
check_curl_installed
check_git_installed
force_dev_download
2018-12-04 19:14:38 +00:00
elif [ "$1" == "install" ]; then
echo -n ""
2018-12-04 17:45:09 +00:00
# install
2018-09-21 15:19:26 +00:00
elif [ "$1" == "clean" ]; then
clean
2018-09-21 15:28:47 +00:00
exit
2018-09-21 15:19:26 +00:00
else
print_usage
exit;
fi
2019-07-30 13:48:26 +00:00
if [ "$2" == "downloadonly" ]; then
echo "No install, download only"
else
install
fi
2018-09-21 16:00:54 +00:00
2018-09-21 16:40:31 +00:00
echo "Installed "`cat $AQUA/version.h | cut -d '"' -f 2 | tr '\n' ' '`
2018-09-21 15:19:26 +00:00
2018-09-21 16:06:01 +00:00
upgrade_self
2018-12-04 17:45:09 +00:00
#cd $CWD
2018-12-04 17:40:05 +00:00
2018-12-04 18:00:42 +00:00
echo ""
2018-12-04 17:45:09 +00:00
echo "Please make sure to read release notes https://github.com/sfeakes/AqualinkD/blob/master/README.md#release"
2018-12-04 19:14:38 +00:00
if [ "$0" == "bash" ]; then
2018-12-04 17:45:09 +00:00
# Was probably run from curl
2018-12-04 18:10:28 +00:00
echo -n "Source directory "
echo $AQUA | tr -s '/' '/'
echo -n "To run this script in the future, "
echo $AQUA/$SOURCE_LOCATION | tr -s '/' '/'
2018-12-04 19:14:38 +00:00
fi