Commit 0cf5de32 authored by PoroCYon's avatar PoroCYon
Browse files

ran shellcheck on everything & fixed most warnings

parent 5ae6a353
......@@ -4,7 +4,6 @@ export SSPT_THISSCRIPT=$(realpath "$0")
export SSPT_RUNDIR=$(dirname "$SSPT_THISSCRIPT")
ALL_ARGS=("$@")
ARG_COUNT=$#
exit_cleanup() {
unset -f escape_string
......@@ -26,7 +25,7 @@ exit_cleanup() {
unset SSPT_RUNDIR
unset SSPT_CONFIG_FILE
exit $@
exit "$@"
}
escape_string() {
......@@ -34,7 +33,7 @@ escape_string() {
}
export -f escape_string
temp_filename() {
echo "/tmp/sspt-$(cat /dev/urandom | head -c 4 | base32)"
echo "/tmp/sspt-$(/dev/urandom head -c 4 /dev/urandom | base32)"
}
export -f temp_filename
file_exists_glob() {
......@@ -97,7 +96,7 @@ load_config() {
exit_cleanup 1
fi
source "$SSPT_CONFIG_FILE"
. "$SSPT_CONFIG_FILE"
}
export -f load_config
......@@ -155,9 +154,9 @@ parse_arg() {
;;
--list-subcommands|--list|-L)
load_config
for sc in $(find "$SSPT_SUB_DIR/" -type f -name "sspt-*"); do
echo "$(basename "$sc")"
done
while read -r sc; do
basename "$sc"
done <<< "$(find "$SSPT_SUB_DIR/" -type f -name "sspt-*")"
;;
--mk-default-config)
if ! [ -d "$CONFIG_PATH" ]; then
......@@ -180,7 +179,7 @@ EOF
*)
load_config
ISSUBCMD=1
exec_subcommand $@
exec_subcommand "$@"
return $?
;;
esac
......@@ -189,19 +188,19 @@ EOF
}
main() {
parse_arg $@
parse_arg "$@"
local RETVAL=$?
if [ $RETVAL -gt 0 ] && [ -z "$ISSUBCMD" ]; then
# TODO: make this work with s/2/\$RETVAL/
local NEW_ARGS="${ALL_ARGS[@]:2}"
local NEW_ARGS=(${ALL_ARGS[@]:2})
if [ ${#NEW_ARGS[@]} -eq 0 ]; then
print_help
exit_cleanup
fi
parse_arg ${NEW_ARGS[@]}
parse_arg "${NEW_ARGS[@]}"
fi
}
main $@
main "$@"
......@@ -247,8 +247,8 @@ sspt_clean_config() {
"$REL_DIR/setup.py" clean -a
}
EOF
elif file_exists_glob *.sln; then
local SLN_FILE=$(ls *.sln | head -1)
elif file_exists_glob ./*.sln; then
local SLN_FILE=$(find ./*.sln -maxdepth 1 -type f | head -1)
local PROBABLE_PROJECT_NAME=${SLN_FILE%.*}
cat >> "$SSPT_FILE" <<EOF
sspt_config() {
......@@ -289,7 +289,7 @@ sspt_clean_config() {
stack clean --full
}
EOF
elif file_exists_glob *.cabal; then
elif file_exists_glob ./*.cabal; then
cat >> "$SSPT_FILE" <<EOF
sspt_config() {
cabal configure -O2 --user --prefix=\$SSPT_DEST_DIR --ghc-option=-j$(nproc)
......@@ -343,7 +343,7 @@ sspt_clean_config() {
true
}
EOF
elif file_exists_glob *.sbt; then
elif file_exists_glob ./*.sbt; then
cat >> "$SSPT_FILE" <<EOF
sspt_config() {
(>&2 echo "NOTE: sbt cannot install packages.")
......@@ -481,8 +481,8 @@ sspt_clean_config() {
rm -rf "$REL_DIR/cmake-build"
}
EOF
elif file_exists_glob *.pro; then
local PRO_FILE="$(ls *.pro | head -1)"
elif file_exists_glob ./*.pro; then
local PRO_FILE="$(find ./*.pro -maxdepth 1 -type f | head -1)"
cat >> "$SSPT_FILE" <<EOF
sspt_config() {
......@@ -519,26 +519,27 @@ sspt_clean_config() {
rm -rf "$REL_DIR/"*akefile "$REL_DIR/"config.*
}
EOF
elif [ -f "Makefile" ]; then
elif file_exists_glob ./*akefile; then # support GNUmakefile etc.
local MAKEFILE=$(find ./*akefile -maxdepth 1 -type f | head -1)
cat >> "$SSPT_FILE" <<EOF
sspt_config() {
true
}
sspt_build() {
make -j$(nproc)
make -f "$MAKEFILE" -j$(nproc)
}
sspt_install() {
make install PREFIX=\$SSPT_DEST_DIR prefix=\$SSPT_DEST_DIR
make -f "$MAKEFILE" install PREFIX=\$SSPT_DEST_DIR prefix=\$SSPT_DEST_DIR
}
sspt_clean() {
make clean
make -f "$MAKEFILE" clean
}
sspt_clean_config() {
true
}
EOF
elif file_exists_glob *.ipkg; then
local IPKG_FILE=$(ls *.ipkg | head -1) # ensure it's only 1 file
elif file_exists_glob ./*.ipkg; then
local IPKG_FILE=$(find ./*.ipkg -maxdepth 1 -type f | head -1) # ensure it's only 1 file
cat >> "$SSPT_FILE" <<EOF
sspt_config() {
(>&2 echo "NOTE: idris doesn't honor \$SSPT_DEST_DIR")
......@@ -580,15 +581,14 @@ autogen_cwd() {
}
main() {
local ESCAPED_PKG=`escape_string $1`
local ESCAPED_PKG=$(escape_string "$1")
case $1 in
-h|-?|--help)
print_help
;;
*)
exec_subcommand src-db exists "$1"
if [ $? -ne 0 ]; then
if ! exec_subcommand src-db exists "$1"; then
(>&2 echo "Package '$1' doesn't exist.")
exit 1
fi
......@@ -609,5 +609,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -20,7 +20,7 @@ EOF
}
main() {
local ESCAPED_PKG=`escape_string $2`
local ESCAPED_PKG=$(escape_string "$2")
case $1 in
-h|-?|--help)
......@@ -28,7 +28,7 @@ main() {
;;
exists)
if [ $# -lt 2 ]; then print_help; exit 1; fi
egrep -q -e "^$ESCAPED_PKG$" < "$SSPT_BIN_DB_FILE"
grep -E -q -e "^$ESCAPED_PKG$" < "$SSPT_BIN_DB_FILE"
return $?
;;
reg|register)
......@@ -36,15 +36,14 @@ main() {
if ! [ -f "$3" ]; then print_help; exit 1; fi
# check whether package exists
egrep -q -e "^$ESCAPED_PKG$" < "$SSPT_BIN_DB_FILE"
if [ $? -ne 0 ]; then
if ! grep -E -q -e "^$ESCAPED_PKG$" < "$SSPT_BIN_DB_FILE"; then
# doesn't exist
echo "$2" >> "$SSPT_BIN_DB_FILE"
cp "$3" "$SSPT_BIN_DB_DIR/$ESCAPED_PKG"
else
# update path
local TMPFILE="$(temp_filename)"
egrep -v -e "^$ESCAPED_PKG$" < "$SSPT_BIN_DB_FILE" > "$TMPFILE"
grep -E -v -e "^$ESCAPED_PKG$" < "$SSPT_BIN_DB_FILE" > "$TMPFILE"
echo "$2" >> "$TMPFILE"
mv "$TMPFILE" "$SSPT_BIN_DB_FILE"
cp "$3" "$SSPT_BIN_DB_DIR/$ESCAPED_PKG"
......@@ -53,11 +52,10 @@ main() {
unreg|unregister)
if [ $# -lt 2 ]; then print_help; exit 1; fi
egrep -q -e "^$ESCAPED_PKG$" < "$SSPT_BIN_DB_FILE"
if [ $? -eq 0 ]; then
if grep -E -q -e "^$ESCAPED_PKG$" < "$SSPT_BIN_DB_FILE"; then
# exists
local TMPFILE="$(temp_filename)"
egrep -v -e "^$ESCAPED_PKG$" < "$SSPT_BIN_DB_FILE" > "$TMPFILE"
grep -E -v -e "^$ESCAPED_PKG$" < "$SSPT_BIN_DB_FILE" > "$TMPFILE"
mv "$TMPFILE" "$SSPT_BIN_DB_FILE"
rm -f "$SSPT_BIN_DB_DIR/$ESCAPED_PKG"
else
......@@ -67,5 +65,5 @@ main() {
esac
}
main $@
main "$@"
......@@ -17,8 +17,7 @@ main() {
print_help
;;
*)
exec_subcommand src-db exists "$1"
if [ $? -ne 0 ]; then
if ! exec_subcommand src-db exists "$1"; then
# TODO: download if URL specified
(>&2 echo "This package doesn't exist.")
exit 1
......@@ -37,5 +36,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -17,8 +17,7 @@ main() {
print_help
;;
*)
exec_subcommand src-db exists "$1"
if [ $? -ne 0 ]; then
if ! exec_subcommand src-db exists "$1"; then
(>&2 echo "This package doesn't exist.")
exit 1
fi
......@@ -32,5 +31,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -17,8 +17,7 @@ main() {
print_help
;;
*)
exec_subcommand src-db exists "$1"
if [ $? -ne 0 ]; then
if ! exec_subcommand src-db exists "$1"; then
(>&2 echo "This package doesn't exist.")
exit 1
fi
......@@ -32,5 +31,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -17,8 +17,7 @@ main() {
print_help
;;
*)
exec_subcommand src-db exists "$1"
if [ $? -ne 0 ]; then
if ! exec_subcommand src-db exists "$1"; then
# TODO: download if URL specified
(>&2 echo "This package doesn't exist.")
exit 1
......@@ -33,5 +32,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -12,15 +12,14 @@ EOF
}
main() {
local ESCAPED_PKG=`escape_string $1`
local ESCAPED_PKG=$(escape_string "$1")
case $1 in
-h|-?|--help)
print_help
;;
*)
exec_subcommand src-db exists "$1"
if [ $? -ne 0 ]; then
if ! exec_subcommand src-db exists "$1"; then
(>&2 echo "Package '$1' doesn't exist.")
exit 1
fi
......@@ -41,5 +40,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -28,5 +28,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -15,29 +15,28 @@ EOF
main() {
case "$1" in
-i|--installed-only|--only-installed)
for pkg in $(cat "$SSPT_BIN_DB_FILE"); do
local ESCAPED_PKG=`escape_string "$pkg"`
local ver=$(egrep "^$ESCAPED_PKG " < "$SSPT_SRC_DB_FILE" | egrep -o "[A-Za-z0-9_\-\.]$")
while read -r pkg; do
local ESCAPED_PKG=$(escape_string "$pkg")
local ver=$(grep -E "^$ESCAPED_PKG " < "$SSPT_SRC_DB_FILE" | grep -E -o "[A-Za-z0-9_\-\.]$")
echo "$pkg $ver"
done
done < "$SSPT_BIN_DB_FILE"
;;
*)
for pkg_and_ver in $(cat "$SSPT_SRC_DB_FILE"); do
local pkg=$(echo "$pkg_and_ver" | egrep -o "^[A-Za-z0-9_\-\.]")
local ver=$(echo "$pkg_and_ver" | egrep -o "[A-Za-z0-9_\-\.]$")
while read -r pkg_and_ver; do
local pkg=$(echo "$pkg_and_ver" | grep -E -o "^[A-Za-z0-9_\-\.]")
local ver=$(echo "$pkg_and_ver" | grep -E -o "[A-Za-z0-9_\-\.]$")
local inst=" "
exec_subcommand bin-db exists "$pkg"
if [ $? -eq 0 ]; then
if exec_subcommand bin-db exists "$pkg"; then
inst="*"
fi
echo "$inst $pkg $ver"
done
done < "$SSPT_SRC_DB_FILE"
;;
esac
}
main $@
main "$@"
......@@ -12,7 +12,7 @@ EOF
}
main() {
local ESCAPED_PKG=`escape_string $2`
local ESCAPED_PKG=$(escape_string "$2")
if [ -z "$2" ]; then
print_help
......@@ -24,15 +24,13 @@ main() {
print_help
;;
*)
exec_subcommand src-db exists "$1"
if [ $? -ne 0 ]; then
if ! exec_subcommand src-db exists "$1"; then
(>&2 echo "This package doesn't exist.")
exit 1
fi
# 1. unreg in the bin db
exec_subcommand bin-db exists "$1"
if [ $? -eq 0 ]; then
if exec_subcommand bin-db exists "$1"; then
exec_subcommand bin-db unreg "$1"
else
(>&2 echo "This package isn't installed.")
......@@ -43,7 +41,7 @@ main() {
exec_subcommand src-db unreg "$1"
# 3. nuke
rm -rf "$SSPT_DL_DIR/$ESCAPED_PKG"
rm -rf "${SSPT_DL_DIR:?}/$ESCAPED_PKG"
;;
esac
}
......@@ -53,5 +51,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -19,20 +19,19 @@ main() {
exit
fi
local ESCAPED_PKG=`escape_string $2`
local ESCAPED_PKG=$(escape_string "$2")
case $1 in
-h|-?|--help)
print_help
;;
*)
exec_subcommand src-db exists "$2"
if [ $? -ne 0 ]; then
if ! exec_subcommand src-db exists "$2"; then
(>&2 echo "This package doesn't exist.")
exit 1
fi
pushd "$SSPT_DL_DIR/$ESCAPED_PKG" >/dev/null
pushd "${SSPT_DL_DIR:?}/$ESCAPED_PKG" >/dev/null
bash -c "source \".sspt\" && \"sspt_$1\""
local RETVAL=$?
popd >/dev/null
......@@ -46,5 +45,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -17,14 +17,13 @@ EOF
PKG_NAME=
pull_with_vcs() {
PKG_NAME="$(basename $2)"
PKG_NAME="$(basename "$2")"
if [ "$1" == "git" ] && [[ "$PKG_NAME" == *".git" ]]; then
PKG_NAME=${PKG_NAME%.*}
fi
exec_subcommand src-db exists "$PKG_NAME"
if [ $? -eq 0 ]; then
if exec_subcommand src-db exists "$PKG_NAME"; then
(>&2 echo "Package '$PKG_NAME' is already pulled, updating remote repository...")
exec_subcommand pkgcmd pull "$PKG_NAME"
return
......@@ -123,14 +122,14 @@ main() {
# need to register it before we can autogen the .sspt file
exec_subcommand src-db reg "$PKG_NAME" dummy_revision
if ! [ -f "$SSPT_DL_DIR/$PKG_NAME/.sspt" ]; then
exec_subcommand autogen "$PKG_NAME"
if [ $? -ne 0 ]; then # autogen failed -> resorting to manual edit
if ! exec_subcommand autogen "$PKG_NAME"; then
# autogen failed -> resorting to manual edit
exec_subcommand edit "$PKG_NAME"
fi
fi
# now set the version of the package in the DB
# (this is only possible because the .sspt file is generated)
exec_subcommand src-db reg "$PKG_NAME" $(exec_subcommand pkgcmd version "$PKG_NAME")
exec_subcommand src-db reg "$PKG_NAME" "$(exec_subcommand pkgcmd version "$PKG_NAME")"
}
if [ $# -eq 0 ]; then
......@@ -138,5 +137,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -12,22 +12,20 @@ EOF
}
main() {
local ESCAPED_PKG=`escape_string $1`
local ESCAPED_PKG=$(escape_string "$1")
case $1 in
-h|-?|--help)
print_help
;;
*)
exec_subcommand src-db exists "$1"
if [ $? -ne 0 ]; then
if ! exec_subcommand src-db exists "$1"; then
(>&2 echo "This package doesn't exist.")
exit 1
fi
# 1. uninstall (does a bin-db unreg)
exec_subcommand bin-db exists "$1"
if [ $? -eq 0 ]; then
if exec_subcommand bin-db exists "$1"; then
exec_subcommand pkgcmd uninstall "$1"
fi
......@@ -35,7 +33,7 @@ main() {
exec_subcommand src-db unreg "$1"
# 3. nuke
rm -rf "$SSPT_DL_DIR/$ESCAPED_PKG"
rm -rf "${SSPT_DL_DIR:?}/$ESCAPED_PKG"
;;
esac
}
......@@ -45,5 +43,5 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
......@@ -22,7 +22,7 @@ EOF
main() {
local ESCAPED_PKG=
if ! [ -z "$2" ]; then
ESCAPED_PKG=`escape_string $2`
ESCAPED_PKG=$(escape_string "$2")
fi
case $1 in
......@@ -31,22 +31,21 @@ main() {
;;
exists)
if [ $# -lt 2 ]; then print_help; exit 1; fi
egrep -q -e "^$ESCAPED_PKG " < "$SSPT_SRC_DB_FILE"
grep -E -q -e "^$ESCAPED_PKG " < "$SSPT_SRC_DB_FILE"
return $?
;;
reg|register)
if [ $# -lt 3 ]; then print_help; exit 1; fi
# check whether package exists
egrep -q -e "^$ESCAPED_PKG " < "$SSPT_SRC_DB_FILE"
if [ $? -ne 0 ]; then
if ! grep -E -q -e "^$ESCAPED_PKG " < "$SSPT_SRC_DB_FILE"; then
echo "$2 $3" >> "$SSPT_SRC_DB_FILE"
else
# update rev
#(>&2 echo "WARNING: Package '$2' is already registered, updating its path...")
local TMPFILE="$(temp_filename)"
# grep can bork when the intput and output files are the same
egrep -v -e "^$ESCAPED_PKG " < "$SSPT_SRC_DB_FILE" > "$TMPFILE"
grep -E -v -e "^$ESCAPED_PKG " < "$SSPT_SRC_DB_FILE" > "$TMPFILE"
echo "$2 $3" >> "$TMPFILE"
mv "$TMPFILE" "$SSPT_SRC_DB_FILE"
fi
......@@ -54,10 +53,9 @@ main() {
unreg|unregister)
if [ $# -lt 2 ]; then print_help; exit 1; fi
egrep -q -e "^$ESCAPED_PKG " < "$SSPT_SRC_DB_FILE"
if [ $? -eq 0 ]; then
if grep -E -q -e "^$ESCAPED_PKG " < "$SSPT_SRC_DB_FILE"; then
local TMPFILE="$(temp_filename)"
egrep -v -e "^$ESCAPED_PKG" < "$SSPT_SRC_DB_FILE" > "$TMPFILE"
grep -E -v -e "^$ESCAPED_PKG" < "$SSPT_SRC_DB_FILE" > "$TMPFILE"
mv "$TMPFILE" "$SSPT_SRC_DB_FILE"
else
(>&2 echo "Package '$2' doesn't exist.")
......@@ -72,6 +70,6 @@ if [ $# -eq 0 ]; then
fi
touch "$SSPT_SRC_DB_FILE"
main $@
main "$@"
exit $?
......@@ -37,7 +37,7 @@ run_strace() {
< "$STRACE_OUT" grep -e O_RDWR -e O_WRONLY \
-e creat -e link -e linkat -e mkdir -e mkdirat -e rename \
-e renameat2 -e renameat2 -e rmdir -e symlink -e symlinkat \
| egrep -v -e "\(\"\/dev\/" -e "\(\"\/tmp\/" -e "\(\"[^\/]" -e "\(\"\/home\"" \
| grep -E -v -e "\(\"\/dev\/" -e "\(\"\/tmp\/" -e "\(\"[^\/]" -e "\(\"\/home\"" \
| grep -v -e "$SSPT_DL_DIR" -e "$SSPT_DB_DIR" \
| sed -E -e 's/^[0-9]* *//' -e 's/\) *=.*/\)/' \
> "$GREP_OUT" # I told you, this is total madness
......@@ -85,14 +85,13 @@ main() {
exit
;;
*)
exec_subcommand src-db exists "$1"
if [ $? -ne 0 ]; then
if ! exec_subcommand src-db exists "$1"; then
(>&2 echo "This package doesn't exist.")
unset OUTFILE
exit 1
fi
run_strace $@
run_strace "$@"
exec_subcommand bin-db reg "$1" "$OUTFILE"
rm "$OUTFILE"
......@@ -106,6 +105,6 @@ if [ $# -eq 0 ]; then
exit
fi
main $@
main "$@"
unset OUTFILE
......@@ -13,7 +13,7 @@ EOF
remove_empty_dir_rec() {
# ensure it stops somehwere - who knows / might be empty (somehow)
if [ "$1" == "/" ]; then
if [ "$1" = "/" ]; then
return