Skip to content

Commit

Permalink
Merge pull request #15 from gbdev/more-shellcheck-fixes
Browse files Browse the repository at this point in the history
Address various shellcheck warnings
  • Loading branch information
ZoomTen authored Nov 19, 2023
2 parents d27fa0d + ac26366 commit a75dc14
Showing 1 changed file with 59 additions and 59 deletions.
118 changes: 59 additions & 59 deletions rgbenv
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ fi
RGBENV_DEFAULT="$RGBENV_ROOT/default"
RGBENV_VERSIONS="$RGBENV_ROOT/versions"

RGBDS_PREFIX_LEN=${#RGBDS_PREFIX}
RGBDS_PREFIX_LEN="${#RGBDS_PREFIX}"

# available offline
if [ -d $RGBENV_VERSIONS ]; then
AVAILABLE_VERSIONS=$(find $RGBENV_VERSIONS -maxdepth 1 -type d | grep -Eo "${RGBDS_PREFIX}[0-9]+.[0-9]+.[0-9]+(-[a-zA-Z0-9_]+)?")
if [ -d "$RGBENV_VERSIONS" ]; then
AVAILABLE_VERSIONS=$(find "$RGBENV_VERSIONS" -maxdepth 1 -type d | grep -Eo "${RGBDS_PREFIX}[0-9]+.[0-9]+.[0-9]+(-[a-zA-Z0-9_]+)?")
else
AVAILABLE_VERSIONS=''
fi
Expand All @@ -56,7 +56,7 @@ fi

check_online_versions () {
# set the relevant variable
ONLINE_VERSIONS=$(git ls-remote --tags $RGBDS_GIT | grep -Eo "[0-9]+.[0-9]+.[0-9]+(-[A-Za-z0-9_]+)?$")
ONLINE_VERSIONS=$(git ls-remote --tags "$RGBDS_GIT" | grep -Eo "[0-9]+.[0-9]+.[0-9]+(-[A-Za-z0-9_]+)?$")
}

help () {
Expand Down Expand Up @@ -96,7 +96,7 @@ help () {
}

checkver () {
if [ -z $1 ]; then
if [ -z "$1" ]; then
echo "No version number set!"
point_to_available_command_and_terminate
elif [[ ! $1 =~ [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+(-[A-Za-z0-9_]+)? ]]; then
Expand All @@ -108,12 +108,12 @@ checkver () {
checkverinstall () {
local version=$1
local has_choice=0
checkver $version;
if [ -d $RGBENV_VERSIONS/$RGBDS_PREFIX$version ]; then
checkver "$version";
if [ -d "$RGBENV_VERSIONS/$RGBDS_PREFIX$version" ]; then
while [ $has_choice -eq 0 ]; do
read -p "Version $version is already installed. Do you want to reinstall? [y/n] " choice
case $choice in
[Yy]* ) has_choice=1; rm -r $RGBENV_VERSIONS/$RGBDS_PREFIX$version; break;;
[Yy]* ) has_choice=1; rm -r "${RGBENV_VERSIONS:?}/${RGBDS_PREFIX:?}${version:?}"; break;;
[Nn]* ) exit; break;;
* ) echo "Please answer y or n.";;
esac
Expand All @@ -128,7 +128,7 @@ _remove () {
while [ $has_choice -eq 0 ]; do
read -p "Remove rgbenv directories? [y/n] " choice
case $choice in
[Yy]* ) rm -r $RGBENV_DEFAULT; rm -r $RGBENV_VERSIONS; has_choice=1; break;;
[Yy]* ) rm -r "$RGBENV_DEFAULT"; rm -r "$RGBENV_VERSIONS"; has_choice=1; break;;
[Nn]* ) has_choice=1; exit;;
* ) echo "Please answer y or n.";;
esac
Expand Down Expand Up @@ -174,8 +174,8 @@ setup () {
}

make_dirs () {
mkdir -p $RGBENV_DEFAULT
mkdir -p $RGBENV_VERSIONS
mkdir -p "$RGBENV_DEFAULT"
mkdir -p "$RGBENV_VERSIONS"
}

show_install_instructions () {
Expand Down Expand Up @@ -241,27 +241,27 @@ _available () {
}

_use () {
if [ -z $1 ]; then
if [ -f $RGBENV_DEFAULT/version ]; then
cat $RGBENV_DEFAULT/version
if [ -z "$1" ]; then
if [ -f "$RGBENV_DEFAULT/version" ]; then
cat "$RGBENV_DEFAULT/version"
else
echo "Use which version?"
point_to_available_command_and_terminate
fi
else
checkver $1;
checkver "$1";
# check if we have a working build of RGBASM (at the very least)
if [ -x $RGBENV_VERSIONS/$RGBDS_PREFIX$1/rgbasm ]; then
if [ -x "$RGBENV_VERSIONS/$RGBDS_PREFIX$1/rgbasm" ]; then
# create symlinks to the proper RGBDS version
mkdir -p $RGBENV_DEFAULT/bin
mkdir -p "$RGBENV_DEFAULT/bin"

echo "Making symlinks..."
for i in rgbasm rgblink rgbfix rgbgfx; do
ln -sf $RGBENV_VERSIONS/$RGBDS_PREFIX$1/$i $RGBENV_DEFAULT/bin/$i
ln -sf "$RGBENV_VERSIONS/$RGBDS_PREFIX$1/$i" "$RGBENV_DEFAULT/bin/$i"
done
echo

echo $1 > $RGBENV_DEFAULT/version
echo "$1" > "$RGBENV_DEFAULT/version"

echo "The default RGBDS has been set to $1."
if [[ ":$PATH:" != *":$RGBENV_DEFAULT/bin:"* ]]; then
Expand All @@ -274,18 +274,18 @@ _use () {
fi
else
echo "Version $1 has not been installed yet."
show_install_instructions $1
show_install_instructions "$1"
point_to_available_command_and_terminate
fi
fi
}

_no_use () {
if [ -f $RGBENV_DEFAULT/version ]; then
rm $RGBENV_DEFAULT/version
if [ -f "$RGBENV_DEFAULT/version" ]; then
rm "${RGBENV_DEFAULT:?}/version"
fi
if [ -d $RGBENV_DEFAULT/bin ]; then
rm -r $RGBENV_DEFAULT/bin
if [ -d "$RGBENV_DEFAULT/bin" ]; then
rm -r "${RGBENV_DEFAULT:?}/bin"
fi
echo "Symlinks and version configuration deleted."
echo "RGBDS is now managed by the system."
Expand All @@ -299,18 +299,18 @@ _install () {
local version=$1
local got_version=0
local download_link="$RGBDS_DOWNLOAD_BASE$version"
if [ -z $version ]; then
if [ -z "$version" ]; then
echo "Which version to install?"
point_to_available_online_and_terminate
fi
checkverinstall $version
checkverinstall "$version"

check_online_versions
local available=$ONLINE_VERSIONS
local available="$ONLINE_VERSIONS"

# check if the version we want is in the git version tags list
for i in $available; do
if [ $version = $i ]; then
if [ "$version" = "$i" ]; then
got_version=1
continue
fi
Expand All @@ -323,16 +323,16 @@ _install () {
# if so, download the tarball and extract it
echo "Downloading from $download_link..."
tempfile=$(mktemp)
curl -L $download_link > $tempfile
dirname=$(tar tzf $tempfile | head -1 | cut -f1 -d'/')
cd $RGBENV_VERSIONS
tar xzf $tempfile
mv $RGBENV_VERSIONS/$dirname $RGBENV_VERSIONS/$RGBDS_PREFIX$version
rm $tempfile
curl -L "$download_link" > "$tempfile"
dirname=$(tar tzf "$tempfile" | head -1 | cut -f1 -d'/')
cd "$RGBENV_VERSIONS"
tar xzf "$tempfile"
mv "$RGBENV_VERSIONS/$dirname" "$RGBENV_VERSIONS/$RGBDS_PREFIX$version"
rm "$tempfile"

# then build it
echo "Building RGBDS $version..."
cd $RGBENV_VERSIONS/$RGBDS_PREFIX$version
cd "$RGBENV_VERSIONS/$RGBDS_PREFIX$version"
if make; then
echo "Build successful."
echo
Expand All @@ -347,18 +347,18 @@ _install () {

_uninstall () {
local version=$1
if [ -z $version ]; then
if [ -z "$version" ]; then
echo "Which version to uninstall?"
point_to_available_command_and_terminate
else
checkver $version
if [ -d $RGBENV_VERSIONS/$RGBDS_PREFIX$version ]; then
rm -r $RGBENV_VERSIONS/$RGBDS_PREFIX$version
checkver "$version"
if [ -d "$RGBENV_VERSIONS/$RGBDS_PREFIX$version" ]; then
rm -r "${RGBENV_VERSIONS:?}/${RGBDS_PREFIX:?}${version:?}"

if [ -d $RGBENV_DEFAULT/version ]; then
if [ $(cat $RGBENV_DEFAULT/version) = $version ]; then
rm $RGBENV_DEFAULT/version
rm $RGBENV_DEFAULT/bin/*
if [ -d "$RGBENV_DEFAULT/version" ]; then
if [ "$(cat "$RGBENV_DEFAULT/version")" = "$version" ]; then
rm "${RGBENV_DEFAULT:?}/version"
rm "${RGBENV_DEFAULT:?}/bin/*"
echo "Removed current RGBDS version, RGBDS is now system-managed"
fi
fi
Expand All @@ -372,37 +372,37 @@ _exec () {
if [ "$1" == "--version" ] || [ "$1" == "-v" ] ; then
shift
local version=$1
checkver $version
if [ -x $RGBENV_VERSIONS/$RGBDS_PREFIX$version/rgbasm ]; then
checkver "$version"
if [ -x "$RGBENV_VERSIONS/$RGBDS_PREFIX$version/rgbasm" ]; then
shift
if [ ${#@} -gt 0 ]; then
echo "Executing with RGBDS version $version."
env PATH="$RGBENV_VERSIONS/$RGBDS_PREFIX$version:$PATH" "$@"
fi
else
echo "Version $version is not installed."
show_install_instructions $version
show_install_instructions "$version"
point_to_available_command_and_terminate
fi
elif [ -f .rgbds-version ]; then
local version=$(cat .rgbds-version | tr -d " \t\n\r")
checkver $version
if [ -x $RGBENV_VERSIONS/$RGBDS_PREFIX$version/rgbasm ]; then
local version=$(tr -d " \t\n\r" < .rgbds-version)
checkver "$version"
if [ -x "$RGBENV_VERSIONS/$RGBDS_PREFIX$version/rgbasm" ]; then
if [ ${#@} -gt 0 ]; then
echo "Executing with RGBDS version $version."
env PATH="$RGBENV_VERSIONS/$RGBDS_PREFIX$version:$PATH" "$@"
fi
else
echo "Version $version is not installed."
show_install_instructions $version
show_install_instructions "$version"
point_to_available_command_and_terminate
fi
else
# when PATH is set properly, this would pretty much be useless
# This *does* have a use if it isn't though
if [ -f $RGBENV_DEFAULT/version ]; then
version=$(cat $RGBENV_DEFAULT/version)
if [ ! -x $RGBENV_VERSIONS/$RGBDS_PREFIX$version/rgbasm ]; then
if [ -f "$RGBENV_DEFAULT/version" ]; then
version=$(cat "$RGBENV_DEFAULT/version")
if [ ! -x "$RGBENV_VERSIONS/$RGBDS_PREFIX$version/rgbasm" ]; then
echo "Version not set."
point_to_use_and_terminate
else
Expand All @@ -420,7 +420,7 @@ _exec () {
# --------------- Entry point -------------------------------------------------

# Perform setup
if [ ! -d $RGBENV_DEFAULT ]; then
if [ ! -d "$RGBENV_DEFAULT" ]; then
if [ -d "$HOME/.$RGBENV_ROOT_SUFFIX" ]; then
offer_to_move
else
Expand All @@ -430,12 +430,12 @@ fi

# Main program
case $1 in
use* ) shift; _use $1;;
no-use* ) shift; _no_use $1;;
install* ) shift; _install $1;;
uninstall* ) shift; _uninstall $1;;
use* ) shift; _use "$1";;
no-use* ) shift; _no_use "$1";;
install* ) shift; _install "$1";;
uninstall* ) shift; _uninstall "$1";;
exec* ) shift; _exec "$@";;
available* ) shift; _available $1;;
available* ) shift; _available "$1";;
remove*) shift; _remove;;
*) if [ ! -z "$1" ]; then echo -e "Unknown command $1.\n"; fi; help;;
esac

0 comments on commit a75dc14

Please sign in to comment.