From 0950379829ade7aa1fe2ef3caab0ec344f47a488 Mon Sep 17 00:00:00 2001 From: Mateo FERON Date: Mon, 6 Apr 2020 16:01:00 +0200 Subject: [PATCH] Init --- .gitignore | 2 + README.md | 21 ++ zclick/zclick | 136 +++++++++++++ zcsf/Makefile | 37 ++++ zcsf/Makefile_cpp | 40 ++++ zcsf/main.c | 7 + zcsf/main.cpp | 7 + zcsf/shelltocstr | 30 +++ zcsf/zcsf | 461 ++++++++++++++++++++++++++++++++++++++++++++ zcsf/zcsf.bash | 25 +++ zcsf/zmake | 38 ++++ zdate/zdate | 43 +++++ zdesktop/zdesktop | 94 +++++++++ zgoc/zfanc | 132 +++++++++++++ zgoc/zgoc | 337 ++++++++++++++++++++++++++++++++ zmc/zsmc | 178 +++++++++++++++++ zmc/zsmc.conf | 7 + znotif/znotif | 77 ++++++++ zosu/zosu | 92 +++++++++ zosu/zosu-install | 152 +++++++++++++++ zosu/zosu-rater | 76 ++++++++ zosu/zosu-rater_old | 98 ++++++++++ zpac/test.sh | 15 ++ zpac/zpac | 412 +++++++++++++++++++++++++++++++++++++++ zrct2/zrct2-install | 239 +++++++++++++++++++++++ zrct2/zsrct2 | 129 +++++++++++++ ztr/zdtol | 76 ++++++++ ztr/zltod | 96 +++++++++ ztr/ztr | 147 ++++++++++++++ zumask/zumask | 15 ++ zupdate/sysupdate | 88 +++++++++ 31 files changed, 3307 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 zclick/zclick create mode 100644 zcsf/Makefile create mode 100644 zcsf/Makefile_cpp create mode 100644 zcsf/main.c create mode 100644 zcsf/main.cpp create mode 100755 zcsf/shelltocstr create mode 100755 zcsf/zcsf create mode 100644 zcsf/zcsf.bash create mode 100755 zcsf/zmake create mode 100755 zdate/zdate create mode 100755 zdesktop/zdesktop create mode 100755 zgoc/zfanc create mode 100755 zgoc/zgoc create mode 100755 zmc/zsmc create mode 100644 zmc/zsmc.conf create mode 100755 znotif/znotif create mode 100755 zosu/zosu create mode 100755 zosu/zosu-install create mode 100755 zosu/zosu-rater create mode 100755 zosu/zosu-rater_old create mode 100755 zpac/test.sh create mode 100755 zpac/zpac create mode 100755 zrct2/zrct2-install create mode 100755 zrct2/zsrct2 create mode 100755 ztr/zdtol create mode 100755 ztr/zltod create mode 100755 ztr/ztr create mode 100755 zumask/zumask create mode 100755 zupdate/sysupdate diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5d30ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Zmakefile +zmakefile diff --git a/README.md b/README.md new file mode 100644 index 0000000..69786a1 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Shell Tools + +Various CLI Shell tools + +## Index + +|----------------------|--------------------------------------------------------------| +| (zclick)[zclick] | Autoclicker | +| (zcsf)[zcsf] | C source file management | +| (zdate)[zdate] | Date operations on folders recursively | +| (zdesktop)[zdesktop] | Generate and manage desktop files | +| (zgoc)[zgoc] | Overclocking for AMD GPUs | +| zmc | Minecraft server utilities | +| znotif | Notifications on end of commands | +| zosu | Osu installer/manager for linux | +| zpac | Pulseaudio tool for volume management | +| zrct2 | OpenRCT2 install/server utilities | +| ztr | Configured text replacing | +| zumask | Do umask operations recursively | +| zupdate | Tools to go with (zupdate)[https://github.com/zawwz/zupdate] | +|----------------------|--------------------------------------------------------------| diff --git a/zclick/zclick b/zclick/zclick new file mode 100755 index 0000000..2316c89 --- /dev/null +++ b/zclick/zclick @@ -0,0 +1,136 @@ +#!/bin/sh + +usage () +{ + echo "zclick [options]" + echo "" + echo "Options:" + echo " -h Display this help" + echo " -d Run daemon" + echo " -c Click ID: 1:left 2:middle 3:right." + echo " -i Interval between clicks in ms, 0 for no clicking. Default 20." + echo " -t Toggle clicking. Sets interval to 0 if current value was != 0" +} + +error () { + printf "\033[1;31m%s\033[0m\n" "$1" >&2 +} + +arg_i=20 +block_time=100 + +if [ -z "$FOLDER" ] +then + if [-n "$XDG_DATA_HOME" ] + then + FOLDER="$XDG_DATA_HOME/zclick" + else + FOLDER="$HOME/.local/share/zclick" + fi +fi + +while getopts ":hc:i:dt" opt; +do + case $opt in + h) + usage + exit 0 + ;; + c) + if ! echo "$OPTARG" | grep -Eq '^[0-9]+$' + then + error "c argument has to be an integer" + exit 2 + fi + if [ -z "$OPTARG" ] + then + error "c needs an argument" + exit 2 + fi + if [ "$OPTARG" -lt 1 ] + then + error "c argument has to be greater than 0" + exit 2 + fi + arg_c=$OPTARG + ;; + i) + if ! echo "$OPTARG" | grep -Eq '^[0-9]+$' + then + error "i argument has to be an integer" + exit 2 + fi + if [ -z "$OPTARG" ] + then + error "i needs an argument" + exit 2 + fi + if [ "$OPTARG" -lt 1 ] + then + error "i argument has to be greater than 0" + exit 2 + fi + arg_i=$OPTARG + ;; + d) + opt_d=y + ;; + t) + opt_t=y + ;; + \?) + echo "Uknown option: $OPTARG" + usage + exit 1 + ;; + esac +done + +mkdir -p "$FOLDER" + +if [ -n "$opt_d" ] +then + ## DAEMON + # reset status of all clicks + for I in "$FOLDER"/* + do + echo "0" > "$I" + done + # DAEMON + while true + do + for I in "$FOLDER"/* + do + interval=$(cat "$I") + click_id=$(echo "$I" | rev | cut -d'/' -f1 | rev) + if [ "$interval" != "0" ] + then + xdotool click --repeat "$(echo "$block_time / $arg_i" | bc)" --delay "$interval" "$click_id" + fi + done + done + +else + ## CONTROL + + if [ -z "$arg_c" ] + then + usage + exit + fi + + if [ -n "$opt_t" ] + then + #toggle + if [ "$(cat "$FOLDER/$arg_c")" != "0" ] ; then + echo 0 > "$FOLDER/$arg_c" + else + echo "$arg_i" > "$FOLDER/$arg_c" + fi + + else + #set + echo "$arg_i" > "$FOLDER/$arg_c" + fi + +fi diff --git a/zcsf/Makefile b/zcsf/Makefile new file mode 100644 index 0000000..6756aaf --- /dev/null +++ b/zcsf/Makefile @@ -0,0 +1,37 @@ +IDIR=include +SRCDIR=src +ODIR=obj +BINDIR=. + +NAME = hello + +LDFLAGS = + +CC=gcc +CXXFLAGS= -I$(IDIR) -Wall -pedantic -std=c18 -O2 +ifeq ($(DEBUG),true) + CXXFLAGS += -g +endif + +$(shell mkdir -p $(ODIR)) +$(shell mkdir -p $(BINDIR)) + +# automatically finds .h +DEPS = $(shell find $(IDIR) -type f -regex '.*\.h') +# automatically finds .c and makes the corresponding .o rule +OBJ = $(shell find $(SRCDIR) -type f -regex '.*\.c' | sed 's|\.c$|.o|g;s|$(SRCDIR)/|$(ODIR)/|g') + +$(ODIR)/%.o: $(SRCDIR)/%.c $(DEPS) + $(CC) $(CXXFLAGS) -c -o $@ $< + +$(BINDIR)/$(NAME): $(OBJ) $(DEPS) + $(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) + +test: $(BINDIR)/$(NAME) + $(BINDIR)/$(NAME) + +clean: + rm $(ODIR)/*.o + +clear: + rm $(BINDIR)/$(NAME) diff --git a/zcsf/Makefile_cpp b/zcsf/Makefile_cpp new file mode 100644 index 0000000..8539564 --- /dev/null +++ b/zcsf/Makefile_cpp @@ -0,0 +1,40 @@ +IDIR=include +SRCDIR=src +ODIR=obj +BINDIR=. + +NAME = hello + +LDFLAGS = + +CC=g++ +CXXFLAGS= -I$(IDIR) -Wall -pedantic -std=c++17 -O2 +ifeq ($(DEBUG),true) + CXXFLAGS += -g +endif + +$(shell mkdir -p $(ODIR)) +$(shell mkdir -p $(BINDIR)) + +# automatically finds .h and .hpp +DEPS = $(shell find $(IDIR) -type f -regex '.*\.hp?p?') +# automatically finds .c and .cpp and makes the corresponding .o rule +OBJ = $(shell find $(SRCDIR) -type f -regex '.*\.cp?p?' | sed 's|\.cp?p?$|.o|g;s|$(SRCDIR)/|$(ODIR)/|g') + +$(ODIR)/%.o: $(SRCDIR)/%.c $(DEPS) + $(CC) $(CXXFLAGS) -c -o $@ $< + +$(ODIR)/%.o: $(SRCDIR)/%.cpp $(DEPS) + $(CC) $(CXXFLAGS) -c -o $@ $< + +$(BINDIR)/$(NAME): $(OBJ) $(DEPS) + $(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) + +test: $(BINDIR)/$(NAME) + $(BINDIR)/$(NAME) + +clean: + rm $(ODIR)/*.o + +clear: + rm $(BINDIR)/$(NAME) diff --git a/zcsf/main.c b/zcsf/main.c new file mode 100644 index 0000000..122d0c1 --- /dev/null +++ b/zcsf/main.c @@ -0,0 +1,7 @@ +#include + +int main(int argc, char* argv[]) +{ + printf("Hello world!\n"); + return 0; +} diff --git a/zcsf/main.cpp b/zcsf/main.cpp new file mode 100644 index 0000000..ee5ded6 --- /dev/null +++ b/zcsf/main.cpp @@ -0,0 +1,7 @@ +#include + +int main(int argc, char* argv[]) +{ + std::cout << "Hello world!\n"; + return 0; +} diff --git a/zcsf/shelltocstr b/zcsf/shelltocstr new file mode 100755 index 0000000..d03328b --- /dev/null +++ b/zcsf/shelltocstr @@ -0,0 +1,30 @@ +#!/bin/sh + +COMMENTSCRIPT="/^\s*#/d;s/\s*#[^\"']*$//" +SLASHSCRIPT='s|\\|\\\\|g;s|\"|\\\"|g' + +SEDSCRIPT="$COMMENTSCRIPT;$SLASHSCRIPT" +NEWLINE_SCRIPT=':a;N;$!ba;s/\n/\\n/g;' + +usage () +{ + echo "shelltocstr [file]" + echo "" + echo "Transform shell scripts to fit into a C string" + echo "Reads from stdin or from a file" +} + + +if [ -t 0 ] +then + # not piped + if [ -n "$1" ] + then + sed $SEDSCRIPT $1 | sed $NEWLINE_SCRIPT + else + usage + fi +else + # piped + sed $SEDSCRIPT /dev/stdin | sed $NEWLINE_SCRIPT +fi diff --git a/zcsf/zcsf b/zcsf/zcsf new file mode 100755 index 0000000..5331658 --- /dev/null +++ b/zcsf/zcsf @@ -0,0 +1,461 @@ +#!/bin/sh + +usage() +{ + echo 'zcsf + +Operations: + make : create Makefile + main : create main source file + ls : list source names + src : create source files + rm : delete source files + mv : rename source files + clear : delete all sources, headers, and binaries + import [name] : import source files from another destination + export : export source files to another destination + rclean [path] : perform a recursive '"'make clean ; make clear'"' + +Operation "src" can be abbrieviated to "s"' +} + +if [ -n "$XDG_CONFIG_HOME" ] +then + local_configpath="$XDG_CONFIG_HOME/zcsf" +else + local_configpath="$HOME/.config/zcsf" +fi + +usr_configpath="/usr/share/z/zcsf" + +if [ -n "$CONFIGPATH" ] +then + local_configpath="$CONFIGPATH" +fi + +header_ifndef() +{ + echo "#ifndef $(echo "$1"_H"$2" | tr '[:lower:]' '[:upper:]')" +} +header_define() +{ + echo "#define $(echo "$1"_H"$2" | tr '[:lower:]' '[:upper:]')" +} +header_endif() +{ + echo "#endif //$(echo "$1"_H"$2" | tr '[:lower:]' '[:upper:]')" +} + +clear_env() +{ + unset SRCDIR + unset BINDIR + unset IDIR + unset ODIR +} + +load_env() +{ + if [ ! -d "$1" ] ; then + return + fi + if [ -f "$1/Makefile" ] ; then + MAKEFILE="$1/Makefile" + elif [ -f "$1/makefile" ] ; then + MAKEFILE="$1/makefile" + fi + + if [ -f "$MAKEFILE" ] + then + SRCDIR_T=$(grep "SRCDIR=" "$MAKEFILE" | cut -d'=' -f2- | head -n1) + BINDIR_T=$(grep "BINDIR=" "$MAKEFILE" | cut -d'=' -f2- | head -n1) + IDIR_T=$(grep "IDIR=" "$MAKEFILE" | cut -d'=' -f2- | head -n1) + ODIR_T=$(grep "ODIR=" "$MAKEFILE" | cut -d'=' -f2- | head -n1) + CC=$(grep "CC=" "$MAKEFILE" | cut -d'=' -f2- | head -n1) + fi + + if [ "$CC" = "g++" ] || [ "$CPP" = "true" ] + then + PP="pp" + fi + + # overwrite + if [ -z "$SRCDIR" ] ; then SRCDIR=$SRCDIR_T ; fi + if [ -z "$BINDIR" ] ; then BINDIR=$BINDIR_T ; fi + if [ -z "$IDIR" ] ; then IDIR=$IDIR_T ; fi + if [ -z "$ODIR" ] ; then ODIR=$ODIR_T ; fi + + # if empty + if [ -z "$SRCDIR" ] ; then SRCDIR=. ; fi + if [ -z "$BINDIR" ] ; then BINDIR=. ; fi + if [ -z "$IDIR" ] ; then IDIR=. ; fi + if [ -z "$ODIR" ] ; then ODIR=. ; fi + + unset SRCDIR_T + unset BINDIR_T + unset IDIR_T + unset ODIR_T + unset MAKEFILE +} + +gen_chfiles() +{ + CFILE="$1.c$2" + HFILE="$1.h$2" + if [ ! -f "$CFILE" ] + then + echo "#include \"$HFILE\"" > "$SRCDIR/$CFILE" + fi + gen_hfile "$1" "$2" +} + +gen_cfile() +{ + CFILE="$1.c$2" + if [ ! -f "$1" ] + then + touch "$SRCDIR/$CFILE" + fi +} + +gen_hfile() +{ + HFILE="$1.h$2" + if [ ! -f "$HFILE" ] + then + { header_ifndef "$1" "$2" ; header_define "$1" "$2" ; printf "\n\n\n" ; header_endif "$1" "$2" ;} > "$IDIR/$HFILE" + fi +} + +clean_all() +{ + if [ -z "$1" ] ; then + spath=. + else + spath="$1" + fi + find "$spath" -name '[mM]akefile' -execdir sh -c 'make clean 2>/dev/null ; make clear 2>/dev/null' ';' 2>/dev/null +} + +dir_gen() +{ + + if [ ! -d "$SRCDIR" ] + then + mkdir -p "$SRCDIR" + fi + if [ ! -d "$BINDIR" ] + then + mkdir -p "$BINDIR" + fi + if [ ! -d "$IDIR" ] + then + mkdir -p "$IDIR" + fi + if [ ! -d "$ODIR" ] + then + mkdir -p "$ODIR" + fi +} + +gen_make() +{ + if [ -f "$local_configpath/Makefile$1" ] ; then + mpath="$local_configpath/Makefile$1" + else + mpath="$usr_configpath/Makefile$1" + fi + cp "$mpath" Makefile 2>/dev/null || touch Makefile +} + +gen_main() +{ + if [ -f "$local_configpath/main.c$1" ] ; then + mpath="$local_configpath/main.c$1" + else + mpath="$usr_configpath/main.c$1" + fi + cp "$mpath" "$SRCDIR" 2>/dev/null || touch "$SRCDIR/main.c$1" +} + +list_c() +{ + ( + cd "$SRCDIR" || exit + if [ -f "main.c" ] || [ -f "main.cpp" ] ; then echo "main" ; fi + find . -type f -regex ".*\.cp?p?" | cut -d '/' -f2- | sed 's/\.cpp$//g;s/\.c$//g;/main/d' + ) +} +list_h() +{ + ( + cd "$IDIR" || exit + find . -type f -regex ".*\.hp?p?" | cut -d '/' -f2- | sed 's/\.hpp$//g;s/\.h$//g;/^$/d' + ) +} + +load_env . + +if [ -z "$1" ] +then + + usage + +elif [ "$1" = "make" ] +then + + unset help + if [ -n "$2" ] + then + if [ "$2" = "c" ] + then + gen_make "" + + elif [ "$2" = "cpp" ] || [ "$2" = "c++" ] + then + gen_make "_cpp" + else + help='y' + fi + + else + help='y' + fi + + + if [ -n "$help" ] + then + echo "zcsf make " + echo "Types:" + echo " c : C automatic makefile" + echo " cpp/c++ : C++ automatic makefile" + fi + + +elif [ "$1" = "clear" ] +then + + make clean >/dev/null 2>&1 + make clear >/dev/null 2>&1 + + if [ "$SRCDIR" != "." ] ; then + rm -rd "$SRCDIR" 2> /dev/null + else + rm ./*.c ./*.cpp 2> /dev/null + fi + [ "$BINDIR" != "." ] && rm -rd "$BINDIR" 2> /dev/null + + if [ "$IDIR" != "." ] ; then + rm -rd "$IDIR" 2> /dev/null + else + rm ./*.h ./*.hpp 2> /dev/null + fi + + [ "$ODIR" != "." ] && rm -rd "$ODIR" 2> /dev/null + +elif [ "$1" = "main" ] +then + + dir_gen + gen_main $PP + +elif [ "$1" = "ls" ] +then + + (list_c && list_h;) | sort | uniq + +elif [ "$1" = "src" ] || [ "$1" = "s" ] +then + + unset help + if [ -n "$2" ] && [ -n "$3" ] + then + unset f + unset c + unset h + unset pp + + if [ "$2" = "a" ] || [ "$2" = "auto" ]; then + f='y' + pp=$PP + + elif [ "$2" = "f" ]; then + f='y' + + elif [ "$2" = "fpp" ] || [ "$2" = "f++" ]; then + f='y' + pp='pp' + + elif [ "$2" = "c" ] + then + c='y' + + elif [ "$2" = "cpp" ] || [ "$2" = "c++" ]; then + c='y' + pp='pp' + + elif [ "$2" = "h" ]; then + h='y' + + elif [ "$2" = "hpp" ] || [ "$2" = "h++" ]; then + h='y' + pp='pp' + + else + help='y' + fi + + else + help='y' + fi + + if [ -n "$help" ] + then + echo 'zcsf src +Types: + a/auto : create .c[pp] and .h[pp] files correspondingly + f : create .c and .h files + fpp/f++ : create .cpp and .hpp files + c : create .c file + cpp/c++ : create .cpp file + h : create .h file + hpp : create .hpp file' + + else + dir_gen + shift $((OPTIND+1)) + for N + do + [ -n "$c" ] && gen_cfile "$N" "$pp" + [ -n "$h" ] && gen_hfile "$N" "$pp" + [ -n "$f" ] && gen_chfiles "$N" "$pp" + done + fi + +elif [ "$1" = "rm" ] +then + + shift $((OPTIND)) + for N + do + rm "$SRCDIR/$N.c" "$SRCDIR/$N.cpp" "$IDIR/$N.h" "$IDIR/$N.hpp" 2>/dev/null + done + +elif [ "$1" = "mv" ] +then + + if [ -f "$SRCDIR/$2.c" ] ; then + CFILE="$2.c" + CFILE2="$3.c" + elif [ -f "$SRCDIR/$2.cpp" ] ; then + CFILE="$2.cpp" + CFILE2="$3.cpp" + fi + if [ -f "$IDIR/$2.h" ] ; then + HFILE="$2.h" + HFILE2="$3.h" + elif [ -f "$IDIR/$2.hpp" ] ; then + HFILE="$2.hpp" + HFILE2="$3.hpp" + fi + + [ -n "$CFILE" ] && mv "$SRCDIR/$CFILE" "$SRCDIR/$CFILE2" + + if [ -n "$HFILE" ] + then + + mv "$IDIR/$HFILE" "$IDIR/$HFILE2" || exit + + find "$SRCDIR" "$IDIR" -type f -regex ".*\.[ch]p?p?" -exec sed -i "s:#include \"$HFILE\":#include \"$HFILE2\":g" "{}" "+" + + sed -i "s:$(header_ifndef "$2"):$(header_ifndef "$3"):g;s:$(header_define "$2"):$(header_define "$3"):g;s:$(header_endif "$2"):$(header_endif "$3"):g" "$IDIR/$HFILE2" + fi + + if [ -z "$CFILE" ] && [ -z "$HFILE" ] + then + echo "'$2' not found" + exit 1 + fi + +elif [ "$1" = "import" ] +then + + SRCDIR_S=$SRCDIR + IDIR_S=$IDIR + + clear_env + + import_path="$2" + if [ ! -d "$import_path" ] ; then + echo "Cannot find '$import_path'" + exit 1 + fi + load_env "$import_path" + + if [ -n "$3" ] + then + if [ -f "$import_path/$SRCDIR/$3.c" ] ; then + cp "$import_path/$SRCDIR/$3.c" "$SRCDIR_S" + _OK=y + fi + if [ -f "$import_path/$SRCDIR/$3.cpp" ] ; then + cp "$import_path/$SRCDIR/$3.cpp" "$SRCDIR_S" + _OK=y + fi + if [ -f "$import_path/$IDIR/$3.h" ] ; then + cp "$import_path/$IDIR/$3.h" "$IDIR_S" + _OK=y + fi + if [ -f "$import_path/$IDIR/$3.hpp" ] ; then + cp "$import_path/$IDIR/$3.hpp" "$IDIR_S" + _OK=y + fi + if [ -z "$_OK" ] + then + echo "Cannot find '$3' at '$import_path'" + fi + else + + find "$import_path/$SRCDIR" -regex '.*\.cp?p?' -exec cp "{}" "$SRCDIR_S/" ";" + find "$import_path/$IDIR" -regex '.*\.hp?p?' -exec cp "{}" "$IDIR_S/" ";" + + fi + +elif [ "$1" = "export" ] +then + + if [ -f "$SRCDIR/$2.c" ] ; then + CFILE="$2.c" + CFILE2="$3.c" + elif [ -f "$SRCDIR/$2.cpp" ] ; then + CFILE="$2.cpp" + CFILE2="$3.cpp" + fi + if [ -f "$IDIR/$2.h" ] ; then + HFILE="$2.h" + HFILE2="$3.h" + elif [ -f "$IDIR/$2.hpp" ] ; then + HFILE="$2.hpp" + HFILE2="$3.hpp" + fi + + clear_env + + export_path="$3" + if [ ! -d "$export_path" ] ; then + echo "Cannot find '$import_path'" + exit 1 + fi + load_env "$export_path" + + cp "$SRCDIR_S/$CFILE" "$export_path/$SRCDIR" + cp "$IDIR_S/$HFILE" "$export_path/$IDIR" + +elif [ "$1" = "rclean" ] +then + + clean_all "$2" + +else + usage +fi diff --git a/zcsf/zcsf.bash b/zcsf/zcsf.bash new file mode 100644 index 0000000..33a02bc --- /dev/null +++ b/zcsf/zcsf.bash @@ -0,0 +1,25 @@ +#/usr/bin/env bash + +_zcsf_completion() +{ + _cw1="make main ls src rm mv clear import export rclean" + _cw1_ls="rm mv export" + _make_types="c cpp" + _src_types="auto f fpp c cpp h cpp" + + if [ "$COMP_CWORD" = "1" ] ; then # operations + _compwords=$_cw1 + elif [ "$COMP_CWORD" -gt "1" ] && echo "$_cw1_ls" | grep -qw "${COMP_WORDS[1]}" ; then # src files + _compwords=$(zcsf ls 2>/dev/null) + elif [ "$COMP_CWORD" = "2" ] && [ "${COMP_WORDS[1]}" = "make" ] ; then # make type + _compwords=$_make_types + elif [ "$COMP_CWORD" = "2" ] && [ "${COMP_WORDS[1]}" = "src" ] ; then # src type + _compwords=$_src_types + else + _compwords="" + fi + + COMPREPLY=($(compgen -W "$_compwords" "${COMP_WORDS[$COMP_CWORD]}")) +} + +complete -F _zcsf_completion -o dirnames zcsf diff --git a/zcsf/zmake b/zcsf/zmake new file mode 100755 index 0000000..289729f --- /dev/null +++ b/zcsf/zmake @@ -0,0 +1,38 @@ +#!/bin/sh + +fname=$(basename "$0") +usage() { + echo "$fname [option] [arguments] +Execute the zmakefile in the directory +If there is no zmakefile, uses the makefile instead +The zmakefile is a shell script + +Options: + -h Display help + -C Execute from this path" +} + +path=. +if [ "$1" = "-h" ] +then + usage + exit 1 +fi +if [ "$1" = "-C" ] +then + if [ $# -lt 2 ] + then + echo "Option -C needs an argument" >&2 + exit 2 + fi + path="$2" +fi + +cd "$path" + +if [ -f Zmakefile ] +then sh Zmakefile $@ +elif [ -f zmakefile ] +then sh zmakefile $@ +else make $@ +fi diff --git a/zdate/zdate b/zdate/zdate new file mode 100755 index 0000000..b32b990 --- /dev/null +++ b/zdate/zdate @@ -0,0 +1,43 @@ +#!/bin/sh + +fname="$(basename "$0")" + +usage() +{ + echo "$fname [options] [path...]" + echo 'Display latest date of modification of any item in the path' +} + +while getopts ":hs" opt; +do + _opt=y + case $opt in + h) + usage + exit 0 + ;; + s) + _opt_s=y + ;; + \?) + echo "Uknown option: $OPTARG" >&2 + usage + exit 1 + ;; + esac +done + +shift $((OPTIND-1)) + +targets="." +[ $# -gt 0 ] && targets="$*" + +ret=$(find $targets -type f -printf '%T@\n' | sort | tail -n1 | cut -d'.' -f1) + +if [ -z "$_opt_s" ] +then + ret=$(date --date="@$ret" 2>/dev/null) +fi + +[ -n "$ret" ] && echo $ret + diff --git a/zdesktop/zdesktop b/zdesktop/zdesktop new file mode 100755 index 0000000..aafedf0 --- /dev/null +++ b/zdesktop/zdesktop @@ -0,0 +1,94 @@ +#!/bin/sh + +fname="$(basename "$0")" + +ERR=0 + +usage() +{ + echo "$fname [options] " + echo ' +Operations: + gen [bin_file...] Generate a desktop file + add Add desktop file to applications + +Options: +' +} + +gen_file() +{ + unset name + + if [ -n "$1" ] + then + file="$1" + name="$(echo "$1" | sed 's|.[^.]*$||g')" + [ -z "$name" ] && name="$1" + else + name="$(basename "$(pwd)")" + file="" + fi + + desktop_file=$(echo "$name.desktop" | tr '[:upper:]' '[:lower:]') + if [ -f "$desktop_file" ] ; then + mv "$desktop_file" "$desktop_file.bak" || return $? + fi + + cat > "$desktop_file" << EOF +[Desktop Entry] +Name=$name +Comment=$name +Type=Application +Exec=$(pwd)/$file +Icon=$(pwd)/icon.png +Categories=Game +Keywords=keyword;keyword +EOF +} + +add_file() +{ + if [ -f "$1" ] + then + ln -s "$(pwd)/$1" "$HOME/.local/share/applications" + else + echo "'$1' is not a file" > /dev/stderr + return 1 + fi +} + +if [ $# -le 0 ] +then + usage + exit 1 +fi + +if [ "$1" = "gen" ] ; then + if [ $# -gt 1 ] + then + shift $((OPTIND)) + for N + do + gen_file "$N" + done + else + gen_file || ERR=$? + fi +elif [ "$1" = "add" ] ; then + + if [ $# -gt 1 ] + then + shift $((OPTIND)) + for N + do + add_file "$N" || ERR=$? + done + else + echo "$fname gen " > /dev/stderr + exit 1 + fi + +fi + +exit $ERR diff --git a/zgoc/zfanc b/zgoc/zfanc new file mode 100755 index 0000000..e908adb --- /dev/null +++ b/zgoc/zfanc @@ -0,0 +1,132 @@ +#!/bin/bash + +config_file="/usr/share/z/config/zgoc.conf" +card_name=$( grep "GPU_NAME=" "$config_file" | cut -d '=' -f2-) +pci_id=$(grep "GPU_PCI_ID=" "$config_file" | cut -d '=' -f2-) +profile_path=$(grep "FANC_PROFILE_PATH=" "$config_file" | cut -d '=' -f2-) +default_profile=$(grep "FANC_DEFAULT_PROFILE=" "$config_file" | cut -d '=' -f2-) +time_interval=$(grep "FANC_TIME_INTERVAL=" "$config_file" | cut -d '=' -f2-) + +temps=( 50 60 70 80 85 90 95 ) # in °C +fans=( 0 15 30 40 55 75 100 ) # in % + +if [ "${#temps[@]}" -ne "${#fans[@]}" ] +then + error "Amount of temperature and pwm values does not match" + exit 30 +fi + +error () { + printf "\033[1;31m%s\033[0m\n" "$1" >&2 +} + +file_report () +{ + if [ -f "$(pwd)/$1" ] + then + echo -n "$(pwd)/$1 = " + cat "$(pwd)/$1" + elif [ -f "$1" ] + then + echo -n "$1 = " + cat "$1" + else + error "File $1 from $(pwd) unaccessible" + fi +} + +file_write () +{ + if ! echo "$1" > "$2" + then + error "Error writing to $2 from $(pwd)" + exit 12 + fi + file_report "$2" +} + +_stop() { + echo "" + file_write 2 pwm1_enable + exit +} + +if [[ $(lspci | grep -c VGA) -gt 1 ]] +then + if [[ -z "$card_name" ]] && [[ -z "$pci_id" ]] ; then + error "Several GPUs are present and no GPU is specified" + exit 20 + fi + if [ -z "$pci_id" ] ; then + pci_id=$(lspci | grep VGA | grep "$card_name" | cut -d ' ' -f1) + if [ "$(echo "$pci_id" | wc -l)" -gt 1 ] ; then + error "More than one name match" + exit 21 + elif [ "$(echo "$pci_id" | wc -l)" -lt 1 ] ; then + error "No name match" + exit 22 + fi + fi + cd /sys/class/drm || exit + + if ! cd "$(ls -l card? | grep "$pci_id" | tr -s ' ' | cut -d ' ' -f9)" + then + error "Error finding pci device" + echo 23 + fi +else + cd "$(find /sys/class/drm/card? | head -n1)" || exit +fi +echo -n "Device at " +pwd +cd "device/hwmon/$(ls device/hwmon)" || exit + +if [ $UID -ne 0 ] +then + echo "Root privileges required" + exit 10 +fi + +if [ ! -f pwm1 ] +then + error "PWM not available on this device" + exit 11 +fi + +trap '_stop' SIGINT + +file_write 1 pwm1_enable + +while true +do + temp=$(zgpu -g0 -Wnt | cut -d '.' -f1) + I=1 + if [ "$temp" -lt "${temps[0]}" ] + then + fan=${fans[0]} + else + while [[ $I -lt "${#temps[@]}" ]] + do + if [ "$temp" -lt "${temps[$I]}" ] + then + LOWERTEMP=${temps[$I-1]} + HIGHERTEMP=${temps[$I]} + LOWERPWM=${fans[$I-1]} + HIGHERPWM=${fans[$I]} + fan=$(echo "( ( $temp - $LOWERTEMP ) * ( $HIGHERPWM - $LOWERPWM ) / ( $HIGHERTEMP - $LOWERTEMP ) ) + $LOWERPWM" | bc) + I=${#temps[@]} + fi + I=$((I + 1)) + done + fi + if [ -z "$fan" ] + then + fan=${fans[$((I - 1))]} + fi + fan_max=$(cat pwm1_max) + fan_min=$(cat pwm1_min) + file_write "$(echo "( ( $fan * ($fan_max - $fan_min) ) / 100 ) + $fan_min" | bc)" pwm1 + sleep "$time_interval" +done + +exit 0 diff --git a/zgoc/zgoc b/zgoc/zgoc new file mode 100755 index 0000000..78850d6 --- /dev/null +++ b/zgoc/zgoc @@ -0,0 +1,337 @@ +#!/bin/sh + +# +# This script does not support multiple GPUs +# Only radeon GPUs supported +# + +config_file="/usr/share/z/config/zgoc.conf" + +if [ ! -f "$config_file" ] +then + echo'# zgoc config file +GPU_PROFILE_PATH=/usr/share/z/zgoc/profiles/gpu +GPU_MEM_PATH=/usr/share/z/zgoc/profiles/mem +GPU_POWER_PATH=/usr/share/z/zgoc/profiles/power +GPU_NAME= +GPU_PCI_ID= +GPU_AUTOMEMFIX=true +GPU_MIN_POWER=30 +FANC_PROFILE_PATH=/usr/share/z/zgoc/profiles/fan +FANC_DEFAULT_PROFILE=default +FANC_TIME_INTERVAL=2' > "$config_file" +fi + +profile_path=$(grep "GPU_PROFILE_PATH=" "$config_file" | cut -d '=' -f2-) +mem_path=$(grep "GPU_MEM_PATH=" "$config_file" | cut -d '=' -f2-) +power_path=$(grep "GPU_POWER_PATH=" "$config_file" | cut -d '=' -f2-) +card_name=$( grep "GPU_NAME=" "$config_file" | cut -d '=' -f2-) +pci_id=$(grep "GPU_PCI_ID=" "$config_file" | cut -d '=' -f2-) +automemfix=$(grep "GPU_AUTOMEMFIX=" "$config_file" | cut -d '=' -f2-) +min_power=$(grep "GPU_MIN_POWER=" "$config_file" | cut -d '=' -f2-) +if [ ! "$automemfix" = "true" ] +then + automemfix="" +fi + +usage() { +echo "Usage: zgoc " +echo "Arguments:" +echo " print : show current state" +echo " gpu : GPU overclocking" +echo " mem : memory overclocking" +echo " apply : apply GPU and memory profiles" +echo " reset : reset GPU and memory to stock values" +echo " memfix : fix screen artifacting caused by low memory state" +echo " power : power cap" +echo "" +echo "Config file: $config_file" +} + +error () { + printf "\033[1;31m%s\033[0m\n" "$1" >&2 +} +root_check () { + if [ "$(id | cut -d'=' -f2 | cut -d'(' -f1)" -ne 0 ] + then + echo "Root privileges required" > /dev/stderr + exit 10 + fi +} + +_stop() { + echo "" + exit +} + +trap '_stop' INT + +# val_convert +# convert an integer to a lower decimal +# output = input*10^-n +# reads from input +val_convert () { + read -r in + size=${#in} + n2=$((size - $1)) + integer=$(echo "$in" | cut -c-$n2) + if [ "$2" != 0 ] ; then + decimal=$(echo "$in" | cut -c$((n2+1))-"$size" | cut -c1-"$2") + fi + if [ -z "$integer" ] ; then + integer=0 + fi + printf "%s" "$integer" + if [ -n "$decimal" ] ; then + printf ".%s" "$decimal" + fi + echo "" +} + +kernel_req () +{ + MAIN_KERNELVER=$(uname -r | cut -d'.' -f1) # Get kernel version. + SEC_KERNELVER=$(uname -r | cut -d'.' -f2) # Get kernel version. + if [ "$MAIN_KERNELVER" -le "$1" ]; then + if [ "$SEC_KERNELVER" -lt "$2" ]; then + error "Kernel $1.$2 or higher is required for this feature" + exit 13 + fi + fi +} + +# kernel version check +kernel_req 4 15 + +file_report () { + if [ -f "$(pwd)/$1" ] + then + printf "%s = " "$(pwd)/$1" + cat "$(pwd)/$1" + elif [ -f "$1" ] + then + printf "%s = " "$1" + cat "$1" + else + error "File $1 from $(pwd) unaccessible" + fi +} + +file_write () { + if ! echo "$1" > "$2" ; + then + error "Error writing to $2 from $(pwd)" + exit 14 + fi + file_report "$2" +} + +high_mem_state () { + if [ -z "$HIGH_MEM_STATE" ] + then + _t=$(grep OD_MCLK "$path/device/pp_od_clk_voltage" -A6) + I=0 + while echo "$_t" | grep -q "$I:" ; do + I=$((I + 1)) + done + HIGH_MEM_STATE=$((I - 1)) + fi + echo $HIGH_MEM_STATE +} + +set_core_state () { + root_check + echo "s$1: $2 MHz $3 mV" + echo "s$1 $2 $3" > "$path/device/pp_od_clk_voltage" +} + +set_mem_state () { + root_check + echo "m$1: $2 MHz $3 mV" + echo "m $1 $2 $3" > "$path/device/pp_od_clk_voltage" +} + +force_mem_state_high () { + root_check + echo "manual" > "$path/device/power_dpm_force_performance_level" + high_mem_state > "$path/device/pp_dpm_mclk" + echo "Forcing memory state to $(high_mem_state) (fix artifacting)" +} + +apply () { + root_check + echo "c" > "$path/device/pp_od_clk_voltage" + echo "Profile applied" + if [ -n "$automemfix" ] ; then + force_mem_state_high + fi +} + +reset_stock () { + root_check + if echo "0 1 2 3 4 5 6 7" > "$path/device/pp_dpm_sclk" && echo "r" > "$path/device/pp_od_clk_voltage" ; then + echo "Reset to default" + fi +} + +apply_power () { + root_check + if ! echo "$1" | grep -Eq '^[0-9]+$' ; then + echo "Invalid power value" >&2 + exit 2 + elif [ "$1" -lt "$min_power" ] ; then + echo "Power value too low" >&2 + exit 2 + elif [ -z "$1" ] ; then + echo "No power value given" >&2 + exit 2 + else + if echo "$1""000000" > $hwmon/power1_cap ; then + echo "Power cap set to $1 W" + fi + fi +} + + +set_profile () { + read -r _states + read -r _frequencies + read -r _voltages + I=1 + for T in $_states + do + if [ "$1" = "gpu" ] ; then + set_core_state "$(echo "$_states" | awk -v I=$I '{print $I}')" "$(echo "$_frequencies" | awk -v I=$I '{print $I}')" "$(echo "$_voltages" | awk -v I=$I '{print $I}')" + elif [ "$1" = "mem" ] ; then + set_mem_state "$(echo "$_states" | awk -v I=$I '{print $I}')" "$(echo "$_frequencies" | awk -v I=$I '{print $I}')" "$(echo "$_voltages" | awk -v I=$I '{print $I}')" + fi + I=$((I + 1)) + done +} + +## locate device path +if [ "$(lspci | grep -c VGA)" -gt 1 ] +then + if [ -z "$card_name" ] && [ -z "$pci_id" ] ; then + error "Several GPUs are present and no GPU is specified" + error "Edit GPU_NAME or GPU_PCI_ID in '$config_file'" + exit 20 + fi + if [ -z "$pci_id" ] ; then + pci_id=$(lspci | grep VGA | grep "$card_name" | cut -d ' ' -f1) + if [ "$(echo "$pci_id" | wc -l)" -gt 1 ] ; then + error "More than one name match for GPU" + exit 21 + elif [ "$(echo "$pci_id" | wc -l)" -lt 1 ] ; then + error "No name match for GPU" + exit 22 + fi + fi + if ! path=$(ls -l /sys/class/drm/card? | grep "$pci_id" | tr -s ' ' | cut -d ' ' -f9) ; then + error "Error finding pci device" + echo 23 + fi +else + path=/sys/class/drm/card0 +fi + +hwmon=$path/device/hwmon/$(ls $path/device/hwmon) + +if [ "$( printf "0x%08x\n" "$(cat /sys/module/amdgpu/parameters/ppfeaturemask)" )" != "0xffffffff" ] ; then + error "PP feature not enabled" + echo "Append 'amdgpu.ppfeaturemask=0xffffffff' to boot parameters to enable it" >&2 + exit 11 +fi + +if [ ! -f "$path/device/pp_od_clk_voltage" ] +then + error "FATAL: overclocking file not found" + error "Either feature is not properly enabled or this isn't a Radeon GPU" + exit 12 +fi + +# read arguments +shift $((OPTIND-1)) + +if [ "$#" -gt 0 ] +then + + if [ "$1" = "reset" ]; then + reset_stock + + elif [ "$1" = "gpu" ]; then + + if [ "$2" = "profile" ]; then + if [ -f "$profile_path/$3" ] ; then + set_profile gpu < "$profile_path/$3" + else + if ! cd "$profile_path" 2>/dev/null ; then + exit + fi + ls -p | grep -v / + fi + elif [ "$2" = "file" ]; then + set_profile gpu < "$3" + apply + else + echo "zgoc gpu " + fi + + elif [ "$1" = "mem" ]; then + + if [ "$2" = "profile" ]; then + if [ -f "$mem_path/$3" ] ; then + set_profile mem < "$mem_path/$3" + else + if ! cd "$mem_path" 2>/dev/null ; then + exit + fi + ls -p | grep -v / + fi + elif [ "$2" = "file" ]; then + set_profile mem < "$3" + apply + else + echo "zgoc mem " + fi + + elif [ "$1" = "power" ] ; then + + kernel_req 4 20 + + if [ "$2" = "profile" ]; then + if [ -f "$power_path/$3" ] ; then + apply_power "$(cat "$power_path/$3")" + else + if ! cd "$power_path" 2>/dev/null ; then + exit + fi + ls -p | grep -v / + fi + elif [ "$2" = "max" ]; then + apply_power "$(val_convert 6 0 < $hwmon/power1_cap_max)" + elif [ "$2" = "set" ]; then + apply_power "$3" + elif [ "$2" = "print" ]; then + echo $(val_convert 6 0 < $hwmon/power1_cap) W + else + echo "zgoc power " + fi + + elif [ "$1" = "apply" ]; then + apply + + elif [ "$1" = "memfix" ]; then + force_mem_state_high + + elif [ "$1" = "print" ]; then + cat $path/device/pp_od_clk_voltage + + else + usage + fi +else + usage +fi + +exit 0 diff --git a/zmc/zsmc b/zmc/zsmc new file mode 100755 index 0000000..146f302 --- /dev/null +++ b/zmc/zsmc @@ -0,0 +1,178 @@ +#!/bin/sh + +config_file="zsmc.conf" +if [ ! -f "$config_file" ] +then + echo "Generating default config file" + cat > "$config_file" << EOF +# zsmc config file +SCREEN_NAME=Minecraft +JAR_FILE=server.jar +JAVA_COMMAND=java +JAVA_ARGUMENTS=-Xmx2G +SERVER_ARGUMENTS=--nogui +BACKUP_PATH=backup +EOF + exit 100 +fi + +# quit if screen isn't installed +which screen >/dev/null || { echo "screen not installed, screen is required for this script to work" >&2; exit 1 ; } + +# load config +. "$(pwd)/$config_file" + +fname="$(basename "$0")" +usage() { +echo "$fname " +echo 'Manage minecraft servers +Backups do not work with bukkit and spigot + +Arguments: + start : Start server in background with screen + startLocal : Start server on the current console + console : Bring up the screen instance of the server (C^a then d to leave) + eraseCache : Upgrade world and clear cached data + backup [file] : Make a world backup. If no file is specifed the current date is used as name + restore [file] : Restore this world backup. If no file is specified, will take the latest file + log [date...] : Display entire log. No date means latest + login [date...] : Display login/logout log. No date means latest + +--DATE FORMAT -- + l for latest + t for today + yyyy-mm-dd for given day + - for x days before today +' +} + + +# Functions + +start_server () +{ + if screen -ls | cut -sd'.' -f2 | awk '{print $1}' | grep -qw "$SCREEN_NAME" # abort if server running + then + echo "A screen under the name '$SCREEN_NAME' is already running" >&2 + return 1 + else + screen -dmS "$SCREEN_NAME" $JAVA_COMMAND $JAVA_ARGUMENTS -jar "$JAR_FILE" $SERVER_ARGUMENTS + fi +} + +attach () { + screen -r "$SCREEN_NAME" +} + +stop_server () { + screen -S "$SCREEN_NAME" -X stuff "^Mstop^M" +} + +eraseCache () +{ + if screen -ls | cut -sd'.' -f2 | awk '{print $1}' | grep -qw "$SCREEN_NAME" # abort if server running + then + echo "A screen under the name '$SCREEN_NAME' is already running" >&2 + return 1 + else + screen -dmS "$SCREEN_NAME" $JAVA_COMMAND $JAVA_ARGUMENTS -jar "$JAR_FILE" $SERVER_ARGUMENTS --forceUpgrade --eraseCache && stop_server && attach + fi +} + +world_backup () +{ + # no server.properties + [ ! -f server.properties ] && echo "server.properties not found" >&2 && return 1 + # extract save name + world="$(grep "level-name=" server.properties | cut -d '=' -f2-)" + + # create backup path + [ ! -d "$BACKUP_PATH" ] && mkdir -p "$BACKUP_PATH" + + # get filename + FILE="$1" + echo "$1" | grep -q '.tar.gz$' || FILE="$FILE.tar.gz" # add .tar.gz extension if not present + + echo "Backing up world to $FILE" + if [ -n "$(command -v pv)" ] # fancy progress bar + then tar -cf - "$world" -P | pv -s "$(du -sb "$world" | awk '{print $1}')" | gzip > "$BACKUP_PATH/$FILE" + else tar -cvzf "$BACKUP_PATH/$FILE" "$world" + fi +} + +world_restore () { + tar -xvf "$BACKUP_PATH/$1" +} + +show_log () +{ + # no arg = latest + [ -z "$1" ] && show_log l + + # latest + if [ "$1" = "l" ] || [ "$1" = "latest" ] + then + cat logs/latest.log 2>/dev/null + + # latest + all current date + elif [ "$1" = "t" ] || [ "$1" = "today" ] ; then + show_log l + show_log "$(date -u "+%F")" + + # -X : translate to date + elif [ "$(echo "$1" | cut -c1)" = "-" ] ; then + show_log "$(date -u --date=@$(( $(date -u +%s) - $((86400 * $(echo "$1" | cut -c2-) )) )) +%F)" + + # date format iterate files + else + LIST=$(find logs -maxdepth 1 -type f -name "$1*") + for I in $LIST + do + gzip -dc "$I" 2>/dev/null || cat $I + done + + fi +} + + +# Main process + +case $1 in + start) start_server ;; + startLocal) $JAVA_COMMAND $JAVA_ARGUMENTS -jar "$JAR_FILE" $SERVER_ARGUMENTS ;; + console) attach ;; + stop) stop_server ;; + eraseCache) eraseCache ;; + backup) + shift 1 + if [ $# -gt 0 ] + then world_backup "$1" + else world_backup "$(date -u "+%F_%H%M%S")" + fi + ;; + log) + shift 1 + if [ $# -gt 0 ] + then for N ; do show_log "$N" ; done + else show_log l + fi + ;; + login) + shift 1 + if [ $# -gt 0 ] + then for N ; do show_log "$N" | grep -E '] logged in with entity id | left the game' ; done + else show_log l | grep -E '] logged in with entity id | left the game' + fi + ;; + restore) + shift 1 + if [ "$#" -gt 0 ] + then world_restore "$1" + else world_restore "$(ls -lt "$BACKUP_PATH" | head -n 2 | tail -n 1 | awk '{print $9}')" + fi + ;; + *) + usage + exit 1 + ;; +esac diff --git a/zmc/zsmc.conf b/zmc/zsmc.conf new file mode 100644 index 0000000..c14ba5d --- /dev/null +++ b/zmc/zsmc.conf @@ -0,0 +1,7 @@ +# zsmc config file +SCREEN_NAME=Minecraft +JAR_FILE=server.jar +JAVA_COMMAND=java +JAVA_ARGUMENTS=-Xmx2G +SERVER_ARGUMENTS=--nogui +BACKUP_PATH=backup diff --git a/znotif/znotif b/znotif/znotif new file mode 100755 index 0000000..c219a0c --- /dev/null +++ b/znotif/znotif @@ -0,0 +1,77 @@ +#!/bin/bash + +_arg_t=2 + +fname=$(basename "$0") +usage () { + echo "$fname [options] [command] +Sends notification when the given command finished executing + Options: + -h Show this message then exit + -t Time the notification stays. By default 2 + -T Notification title + -m <string> Displays this message when finished. Variable resolution on + > Default message is '<command> finished'" +} + +warning () { + if [ ! -n "$_opt_w" ] ; then + printf "\033[0;33m$1\033[0m\n" >&2 + fi +} +error () { + printf "\033[1;31m$1\033[0m\n" >&2 +} + +_args="" + +# $1 = message , $2 = time , $3 = title +notify () { + if which kdialog >/dev/null + then + kdialog --passivepopup "$1" $2 --title "$3" + elif which notify-send >/dev/null + then + notify-send -t $2 "$3" "$1" + else echo "No supported notification" >&2 && return 1 + fi +} + +# read options +while getopts ":hm:T:t:" opt; +do + case $opt in + h) + usage + exit 0 + ;; + m) + if [ ! -n "$OPTARG" ] + then + error "m needs an argument" + exit + fi + message=$OPTARG + _opt_m=y + ;; + \?) + echo "Uknown option: $OPTARG" + usage + exit 1 + ;; + esac +done + +shift $((OPTIND-1)) + +if [ $# -gt 0 ] +then + [ ! -n "$_opt_m" ] && message="'$*' finished" + [ -z "$title" ] && title=$* + $@ +else + [ ! -n "$_opt_m" ] && message="Ping" + [ -z "$title" ] && title="Ping" +fi + +notify "$message" $_arg_t "$title" diff --git a/zosu/zosu b/zosu/zosu new file mode 100755 index 0000000..95dadbe --- /dev/null +++ b/zosu/zosu @@ -0,0 +1,92 @@ +#!/bin/sh + +bin=$(basename "$0") + +config_file="zosu.conf" + +usage() +{ + echo "$bin [options] <operation> [arguments]" + echo 'Perform operations on a wined osu instance + +Operations: + start Start the wined osu + kill Kill the wine server + sound Change the sound driver + +Options: + -h Display this help + -c Path to conf file (default zosu.conf) +' +} + +while getopts ":hc:" opt; +do + case $opt in + h) + usage + exit 0 + ;; + c) + config_file="$OPTARG" + ;; + \?) + echo "Uknown option: $OPTARG" + usage + exit 1 + ;; + esac +done + +# resolve config file path +[ "$(echo $config_file | cut -c1)" != "/" ] && config_file="$(pwd)/$config_file" + +# load config file +[ -f "$config_file" ] && . "$config_file" + +# default environment +[ -z "$OSUDIR" ] && export OSUDIR=osu +[ -z "$WINEPREFIX" ] && export WINEPREFIX="$(pwd)/.wine" +export vblank_mode=0 + + +shift $((OPTIND-1)) + +if [ -n "$1" ] +then + + if [ "$1" = "start" ]; then + shift $((OPTIND)) + wine $OSUDIR/osu\!.exe $@ + + elif [ "$1" = "kill" ]; then + wineserver -k + + elif [ "$1" = "sound" ]; then + + case $2 in + "pulse") + winetricks sound=pulse + ;; + "stable") + winetricks sound=pulse + ;; + "alsa") + winetricks sound=alsa + ;; + "lowlatency") + winetricks sound=alsa + ;; + *) + echo "$bin sound <pulse/alsa>" + esac + + else + usage + exit 1 + fi + +else + usage + exit 1 +fi diff --git a/zosu/zosu-install b/zosu/zosu-install new file mode 100755 index 0000000..e79597d --- /dev/null +++ b/zosu/zosu-install @@ -0,0 +1,152 @@ +#!/bin/sh + +if [ "$OSUDIR" == "" ] ; then + OSUDIR='osu' +fi +if [ "$WINEDIR" == "" ] ; then + WINEDIR='$(pwd)/.wine' +fi +export WINEARCH=win32 +export WINEPREFIX=$(echo "echo $WINEDIR" | sh) + +silentmk() +{ + if [ ! -d "$1" ] ; then + mkdir "$1" + fi +} + +pacman_packages() +{ + sudo pacman -S --needed --noconfirm lib32-alsa-lib lib32-libpulse lib32-gnutls lib32-libxcomposite +} + +echo_red() { + printf "\033[1;31m$1\033[0m\n" +} +echo_blue() { + printf "\033[1;34m$1\033[0m\n" +} +echo_yellow() { + printf "\033[1;33m$1\033[0m\n" +} +echo_white() { + printf "\033[1;37m$1\033[0m\n" +} +_done() { + printf "\033[1;32mDone\033[0m\n\n" +} + +abort() +{ + echo_red "ERROR: ABORTING" + exit $1 +} + +if [ ! -n "$(command -v wine)" ] +then + echo_red "wine not installed" + exit +fi +if [ ! -n "$(command -v winetricks)" ] +then + echo_yellow "winetricks not installed" + read -p "Manually install winetricks? [Y/n]: " yn + case $yn in + [Nn]* ) + echo_red "Aborting" + exit + ;; + * ) + wget https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks + chmod +x winetricks + sudo mv -v winetricks /usr/local/bin + _done + ;; + esac +fi +if [ -n "$(command -v pacman)" ] +then + pacman_packages +fi + +echo_white "Wine environment will be setup. This can take a very long time and may fail" +read -p "Proceed? [Y/n]: " yn +case $yn in + [Nn]* ) + echo_red "Aborting" + exit + ;; + * ) + ;; +esac + +# The big one +winetricks -q dotnet472 cjkfonts gdiplus > /dev/null || abort $? +_done + +# sound buffers +echo_white "Optimizing sound buffer" +cat > dsound.reg << "EOF" +Windows Registry Editor Version 5.00 + +[HKEY_CURRENT_USER\Software\Wine\DirectSound] +"HelBuflen"="512" +"SndQueueMax"="3" +EOF +wine regedit dsound.reg > /dev/null 2>&1 || abort $? +rm dsound.reg +_done + +# ALSA audio +read -p "Experimental audio lag lowering? [Y/n]: " yn +case $yn in + [Nn]* ) + ;; + * ) + winetricks sound=alsa + echo_yellow "If you encounter audio distortion, run 'zosu sound pulse' to revert" + _done + ;; +esac + +# install osu +read -p "Insall fresh osu? [Y/n]: " yn +case $yn in + [Nn]* ) + echo_blue "Provide a link '$OSUDIR' to the desired osu instance" + ;; + * ) + wget https://m1.ppy.sh/r/osu\!install.exe + echo_white "Starting osu installer. Don't change the install directory" + wine osu\!install.exe > /dev/null 2>&1 + sleep 5 + ln -sf $WINEPREFIX/drive_c/users/$(whoami)/Local\ Settings/Application\ Data/osu\! $OSUDIR + rm osu\!install.exe + _done + ;; +esac + + +echo_white "Creating zosu.conf" +cat > zosu.conf << "EOF" +OSUPATH=$OSUDIR +export WINEPREFIX=$WINEDIR +EOF + +_done + +echo_white "Creating osu.sh" +cat > osu.sh << "EOF" +#!/bin/sh +DIR=$(dirname "\$0") +cd "$DIR" + +. "$(pwd)/zosu.conf" +export vblank_mode=0 + +wine "$OSUDIR/osu!.exe" $@ +EOF +chmod +x osu.sh + +_done diff --git a/zosu/zosu-rater b/zosu/zosu-rater new file mode 100755 index 0000000..b3ec21c --- /dev/null +++ b/zosu/zosu-rater @@ -0,0 +1,76 @@ +#!/bin/sh + +if [ $# -lt 2 ] +then + echo "$0 <rate> <file>" +fi + +RATE="$1" +FILE="$2" + +OUTPUTFILE="$(echo "$2" | sed 's/\.osu//g' | cut -d']' -f1) x$RATE].osu" + +AUDIO=$(grep "AudioFilename:" "$FILE" | cut -d' ' -f2- | tr -d '\n\r') +AUDIORATED=$(echo $AUDIO | cut -d'.' -f1)-x$RATE".mp3" + +echo "" > $OUTPUTFILE +rm "$OUTPUTFILE" + +output_line () { + printf "%s\r\n" "$1" >> $OUTPUTFILE +} + +audioRate () { + ffmpeg -i "$AUDIO" -filter:a "asetrate=$(ffprobe -v error -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 "$AUDIO")*$RATE" -y "$AUDIORATED" +} + +LINES=$(wc -l "$FILE" | cut -d' ' -f1) +FFILE=$(cat "$FILE") + +step_skip () +{ + printf "$(echo "$FFILE" | grep -B $LINES "$1" | head -n -1)" >> $OUTPUTFILE + FFILE=$(echo "$FFILE" | grep -A $LINES "$1" | tail -n +2) +} + +step_skip "AudioFilename:" +output_line "AudioFilename: $AUDIORATED" + +PREVIEW=$(echo "$FFILE" | grep "PreviewTime:" | cut -d' ' -f2 | tr -d '\r\n') +step_skip "PreviewTime:" +output_line "PreviewTime: $(echo "$PREVIEW / $RATE" | bc | cut -d '.' -f1)" +VERSION=$(echo "$FFILE" | grep "Version:" | cut -d':' -f2- | tr -d '\n\r') +step_skip "Version:" +output_line "Version:$VERSION x$RATE" + +step_skip "BeatmapID:" +output_line "BeatmapID:0" + +step_skip "\[TimingPoints\]" +TIMINGPOINTS=$(echo "$FFILE" | grep -B $LINES "\[HitObjects\]" | head -n -1) +OBJECTS=$(echo "$FFILE" | grep -A $LINES "\[HitObjects\]" | tail -n +2) + +output_line "[TimingPoints]" + +N=$(echo "$TIMINGPOINTS" | wc -l) +I=0 + +echo "$TIMINGPOINTS" | awk -F ',' "{ RATE=$RATE"' + if ($7 == 0 || $7 == 1) + { + if ($7 == 1) + {print int($1/RATE)","$2/RATE","$3","$4","$5","$6","$7","$8} + else + {print int($1/RATE)","$2","$3","$4","$5","$6","$7","$8} + } +}' >> "$OUTPUTFILE" + +output_line "[HitObjects]" +N=$(echo "$OBJECTS" | wc -l) +echo "$OBJECTS" | awk -F "[,:]" "{RATE=$RATE"' + if ($1) + {print $1","$2","int($3/RATE)","$4","$5","int($6/RATE)":"$7":"$8":"$9":"$10} +} +' >> "$OUTPUTFILE" + +audioRate diff --git a/zosu/zosu-rater_old b/zosu/zosu-rater_old new file mode 100755 index 0000000..c59276e --- /dev/null +++ b/zosu/zosu-rater_old @@ -0,0 +1,98 @@ +#!/bin/sh + +if [ $# -lt 2 ] +then + echo "$0 <rate> <file>" +fi + +RATE="$1" +FILE="$2" + +OUTPUTFILE="$(echo "$2" | sed 's/\.osu//g' | cut -d']' -f1) x$RATE].osu" + +AUDIO=$(grep "AudioFilename:" "$FILE" | cut -d' ' -f2- | tr -d '\n\r') +AUDIORATED=$(echo $AUDIO | cut -d'.' -f1)-x$RATE".mp3" + +echo "" > $OUTPUTFILE +rm "$OUTPUTFILE" + +output_line () { + printf "%s\r\n" "$1" >> $OUTPUTFILE +} + +audioRate () { + ffmpeg -i "$AUDIO" -filter:a "asetrate=$(ffprobe -v error -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 "$AUDIO")*$RATE" -y "$AUDIORATED" +} + +LINES=$(wc -l "$FILE" | cut -d' ' -f1) +FFILE=$(cat "$FILE") + +step_skip () +{ + printf "$(echo "$FFILE" | grep -B $LINES "$1" | head -n -1)" >> $OUTPUTFILE + FFILE=$(echo "$FFILE" | grep -A $LINES "$1" | tail -n +2) +} + +step_skip "AudioFilename:" +output_line "AudioFilename: $AUDIORATED" + +PREVIEW=$(echo "$FFILE" | grep "PreviewTime:" | cut -d' ' -f2 | tr -d '\r\n') +step_skip "PreviewTime:" +output_line "PreviewTime: $(echo "$PREVIEW / $RATE" | bc | cut -d '.' -f1)" +VERSION=$(echo "$FFILE" | grep "Version:" | cut -d':' -f2- | tr -d '\n\r') +step_skip "Version:" +output_line "Version:$VERSION x$RATE" + +step_skip "BeatmapID:" +output_line "BeatmapID:0" + +step_skip "\[TimingPoints\]" +TIMINGPOINTS=$(echo "$FFILE" | grep -B $LINES "\[HitObjects\]" | head -n -1) +OBJECTS=$(echo "$FFILE" | grep -A $LINES "\[HitObjects\]" | tail -n +2) + +output_line "[TimingPoints]" + +N=$(echo "$TIMINGPOINTS" | wc -l) +I=0 +for LINE in $TIMINGPOINTS +do + # TODO: ignore colors + I=$((I + 1)) + printf "Timing: %s%%\r" $(echo "$I * 100 / $N" | bc) + if [ -n "$(echo $LINE | grep ",")" ] + then + VAL=$(echo "$(echo "$LINE" | cut -d ',' -f1) / $RATE" | bc | cut -d '.' -f1) + BPM=$(echo "$LINE" | cut -d',' -f7) + if [ $BPM -eq 1 ] ; then + VAL2=$(echo "$(echo "$LINE" | cut -d ',' -f2) / $RATE" | bc -l) + output_line "$VAL,$VAL2,$(echo "$LINE" | cut -d ',' -f3-)" + else + output_line "$VAL,$(echo "$LINE" | cut -d ',' -f2-)" + fi + else + output_line "$LINE" + fi +done +echo "" + +output_line "[HitObjects]" +N=$(echo "$OBJECTS" | wc -l) +I=0 +for LINE in $OBJECTS +do + I=$((I + 1)) + printf "Objects: %s%%\r" $(echo "$I * 100 / $N" | bc) + if [ -n "$(echo $LINE | grep ",")" ] + then + V12=$(echo $LINE | cut -d',' -f-2) + V3=$(echo "$(echo "$LINE" | cut -d ',' -f3) / $RATE" | bc | cut -d '.' -f1) + V45=$(echo $LINE | cut -d',' -f4-5) + V6=$(echo "$(echo $LINE | cut -d',' -f6 | cut -d':' -f1) / $RATE" | bc | cut -d '.' -f1) + CPM=$(echo $LINE | cut -d':' -f2-) + output_line "$V12,$V3,$V45,$V6:$CPM" + else + output_line $LINE + fi +done +echo "" +audioRate diff --git a/zpac/test.sh b/zpac/test.sh new file mode 100755 index 0000000..395a78f --- /dev/null +++ b/zpac/test.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +VAR="" +_date() +{ + if [ -z "$VAR" ] + then + VAR=$(date) + echo "date" >&2 + fi + echo $VAR +} + +_date +_date diff --git a/zpac/zpac b/zpac/zpac new file mode 100755 index 0000000..ad2f09d --- /dev/null +++ b/zpac/zpac @@ -0,0 +1,412 @@ +#!/bin/sh + +usage() +{ + echo "Usage: zpac <operation> [sub-operation]" + echo "Operations:" + echo " master : Master volume operation" + echo " app : Application volume operation" + echo " mic : Mic operation" + echo " fx : FX operation" + echo " unload-hdmi : Unload HDMI sound modules" + echo " tsched : Turn on or off scheduling" +} + +value() +{ + if [ "$(printf "%s" "$1" | tail -c 1)" = "%" ] + then + echo "$(echo "$1" | cut -d'%' -f1) * 65535 / 100" | bc + else + echo "$1" + fi +} + + +tsched_0() { + sed -i 's|^load-module module-udev-detect.*|load-module module-udev-detect tsched=0|' /etc/pulse/default.pa +} +tsched_1() { + sed -i 's|^load-module module-udev-detect.*|load-module module-udev-detect|' /etc/pulse/default.pa +} + + +FXon() +{ + screen -dmS "pulseeffects" pulseeffects --gapplication-service > /dev/null 2>&1 + echo "FX on" +} +FXoff() +{ + pulseeffects -q + echo "FX off" +} +FXtoggle() +{ + if [ "$(pgrep -c pulseeffects)" -lt 1 ] + then FXon + else FXoff + fi +} +FXstate() +{ + if [ "$(pgrep -c pulseeffects)" -ge 1 ] + then echo "FX on" + else echo "FX off" + fi +} + +get_list_sinks() +{ + if [ -z "$LIST_SINKS" ] + then + LIST_SINKS="$(pacmd list-sinks)" + fi +} +list_sinks() { echo "$LIST_SINKS"; } + +get_list_sink_inputs() +{ + if [ -z "$LIST_SINK_INPUTS" ] + then + LIST_SINK_INPUTS="$(pacmd list-sink-inputs)" + fi +} +list_sink_inputs() { echo "$LIST_SINK_INPUTS"; } + +get_list_sources() +{ + if [ -z "$LIST_SOURCES" ] + then + LIST_SOURCES="$(pacmd list-sources)" + fi +} +list_sources() { echo "$LIST_SOURCES"; } + +master_index() { + list_sinks | grep "* index:" | awk '{print $3}' +} + +############## +# SINK OPERATIONS +sinkvolume() +{ + list_sinks | grep -E "(index: )|(volume: )" | grep "index: $1" -A1 | tail -n1 | awk '{print $3}' +} +sinkvolumep() +{ + list_sinks | grep -E "(index: )|(volume: )" | grep "index: $1" -A1 | tail -n1 | awk '{print $5}' +} +sinkvolumeset() +{ + R="$(pacmd set-sink-volume "$1" "$(value $2)")" + if [ -z "$R" ] ; then + echo "$3 @ $2" + else + echo "$R" + fi +} +sinkmutestate() +{ + if [ "$(list_sinks | grep -E "(index: $1)|(muted: )" | grep "index: $1" -A1 | tail -n1 | awk '{print $2}')" = "yes" ] + then echo "$2 mute on" + else echo "$2 mute off" + fi +} +sinkmuteset() +{ + R="$(pacmd set-sink-mute "$1" "$(value $2)")" + if [ -z "$R" ] ; then + if [ "$2" -eq 1 ] + then echo "$3 mute on" + else echo "$3 mute off" + fi + else + echo "$R" + fi +} +sinkmutetoggle() +{ + if sinkmutestate $1 | grep -q "mute on"; then + sinkmuteset $1 0 $2 + else + sinkmuteset $1 1 $2 + fi +} + +################ +# SINK INPUT OPERATIONS +sinkinvolume() +{ + list_sink_inputs | grep -E "(index: )|(volume: )" | grep "index: $1" -A1 | tail -n1 | awk '{print $3}' +} +sinkinvolumep() +{ + list_sink_inputs | grep -E "(index: )|(volume: )" | grep "index: $1" -A1 | tail -n1 | awk '{print $5}' +} +sinkinlist() +{ + list_sink_inputs | grep application.process.binary | cut -d '"' -f2 +} +sinkinvolumeset() +{ + sinkid=$(list_sink_inputs | grep -E "(index: $1)|(sink: )" | grep "index: $1" -A1 | tail -n1 | awk '{print $2}') + sinkvol=$(list_sinks | grep -E "(index: )|(volume: )" | grep "index: $sinkid" -A1 | tail -n1 | awk '{print $3}') + val=$(echo "$(value $2)"'*'"$sinkvol / 65536" | bc) + R="$(pacmd set-sink-input-volume "$1" "$val")" + if [ -z "$R" ] ; then + echo "app \"$3\" @ $2" + else + echo "$R" + fi +} +sinkin_get_indexes() +{ + list_sink_inputs | grep -E "(index: )|(application.process.binary = )" | grep "application.process.binary = \"$1\"" -B1 | head -n1 | awk '{print $2}' +} + +############# +# SOURCE OPERATIONS +sourcelist() +{ + list_sources | grep device.product.name | cut -d '"' -f2 +} +sourcevolume() +{ + list_sources | grep -E "(index: )|(volume: )" | grep "index: $1" -A1 | tail -n1 | awk '{print $3}' +} +sourcevolumep() +{ + list_sources | grep -E "(index: )|(volume: )" | grep "index: $1" -A1 | tail -n1 | awk '{print $5}' +} +sourcevolumeset() +{ + R="$(pacmd set-source-volume "$1" "$(value $2)")" + if [ -z "$R" ] ; then + echo "mic \"$3\" @ $2" + else + echo "$R" + fi +} +source_get_default_name() +{ + list_sources | grep -E '(* index: )|(device.product.name = )' | grep -A1 "* index: " | tail -n1 | cut -d '"' -f2 +} +source_get_index() +{ + if [ "$1" = "default" ] + then + list_sources | grep '* index: ' | awk '{print $3}' + elif [ -n "$1" ] + then + list_sources | grep -E "(index: )|(device.product.name = )" | tr '*' ' ' | grep "device.product.name = \"$1"\" -B1 | grep "index:" | awk '{print $2}' + fi +} +source_name() +{ + if [ "$1" = "default" ] + then + source_get_default_name + else + echo "$1" + fi +} +sourcemutestate() +{ + if [ "$(list_sources | grep -E "(index: $1)|(muted: )" | grep "index: $1" -A1 | tail -n1 | awk '{print $2}')" = "yes" ] + then echo "mic \"$2\" mute on" + else echo "mic \"$2\" mute off" + fi +} +sourcemuteset() +{ + R="$(pacmd set-source-mute "$1" "$(value $2)")" + if [ -z "$R" ] ; then + if [ "$2" -eq 1 ] + then echo "mic \"$3\" mute on" + else echo "mic \"$3\" mute off" + fi + else + echo "$R" + fi +} +sourcemutetoggle() +{ + if sourcemutestate $1 | grep -q "mute on"; then + sourcemuteset $1 0 "$2" + else + sourcemuteset $1 1 "$2" + fi +} + +if [ -z "$1" ] +then + usage + exit 1 +elif [ "$1" = "master" ] +then + get_list_sinks + case $2 in + setvolume) sinkvolumeset $(master_index) $3 "master" ;; + volume) sinkvolumep $(master_index) $3 "master" ;; + mute) + case $3 in + on) sinkmuteset $(master_index) 1 master ;; + off) sinkmuteset $(master_index) 0 master ;; + toggle) sinkmutetoggle $(master_index) master ;; + state) sinkmutestate $(master_index) master ;; + *) echo "zpac master mute <on/off/toggle/state>" && exit 1 ;; + esac + ;; + *) echo "zpac master <volume/setvolume/mute>" && exit 1 ;; + esac + +elif [ "$1" = "app" ] +then + + if [ "$2" = "list" ] + then + get_list_sink_inputs + sinkinlist + + elif [ "$2" = "volume" ] + then + get_list_sink_inputs + shift $((OPTIND+1)) + for N + do + for I in $(sinkin_get_indexes "$N") + do + sinkinvolumep $I + done + done + + elif [ "$2" = "setvolume" ] + then + + if [ -n "$3" ] + then + vol=$3 + shift $((OPTIND+2)) + get_list_sink_inputs + get_list_sinks + for N + do + for I in $(sinkin_get_indexes "$N") + do + sinkinvolumeset $I $vol "$N" + done + done + else + echo "zpac app setvolume <volume> <binary...>" + fi + + else + echo "zpac app <list/setvolume>" + fi + +elif [ "$1" = "mic" ] +then + + if [ "$2" = "list" ] + then + + get_list_sources + sourcelist + + elif [ "$2" = "volume" ] + then + get_list_sources + shift $((OPTIND+1)) + for N + do + sourcevolumep $(sinkin_get_indexes "$N") + done + + elif [ "$2" = "setvolume" ] + then + + if [ -n "$3" ] + then + get_list_sources + vol=$3 + shift $((OPTIND+2)) + for N + do + sourcevolumeset $(source_get_index "$N") $vol "$(source_name "$N")" + done + else + echo "zpac mic setvolume <volume> <mic...>" + fi + + elif [ "$2" = "mute" ] + then + get_list_sources + case $3 in + on|off|toggle|state) s=$3 ;; + *) echo "zpac mic mute <on/off/toggle/state> <mic...>" && exit 1 ;; + esac + + shift $((OPTIND+2)) + for N + do + case $s in + "on") sourcemuteset $(source_get_index "$N") 1 "$(source_name "$N")" ;; + "off") sourcemuteset $(source_get_index "$N") 0 "$(source_name "$N")" ;; + "toggle") sourcemutetoggle $(source_get_index "$N") "$(source_name "$N")" ;; + "state") sourcemutestate $(source_get_index "$N") "$(source_name "$N")" ;; + esac + done + + else + echo "zpac mic <list/volume/setvolume/mute>" + fi + + +elif [ "$1" = "fx" ] +then + [ -z "$(which pulseeffects)" ] && echo "No FX available" >&2 && exit 1 + + case $2 in + on|off|toggle|state) MODE=$2 ;; + *) echo "zpac fx <on/off/toggle/state>" && exit 1 ;; + esac + + if [ -z "$3" ] #all fx + then + case $MODE in + on) FXon ;; + off) FXoff ;; + toggle) FXtoggle ;; + state) FXstate ;; + esac + else + echo "FX '$3' not found" + fi + +elif [ "$1" = "unload-hdmi" ] +then + + for I in $(lspci | grep HDMI | awk '{print $1}' | tr ':' '_') + do + pacmd unload-module $(pacmd list-modules | grep "pci-0000_$I" -B2 | head -n 1 | awk '{print $2}') > /dev/null 2>&1 + done + +elif [ "$1" = "tsched" ] +then + + if [ "$2" = "0" ] + then + tsched_0 + + elif [ "$2" = "1" ] + then + tsched_1 + + else + echo "zpac tsched <0/1>" + fi + +else + usage +fi diff --git a/zrct2/zrct2-install b/zrct2/zrct2-install new file mode 100755 index 0000000..bdf6f62 --- /dev/null +++ b/zrct2/zrct2-install @@ -0,0 +1,239 @@ +#!/bin/sh + +# values: true/false +# parameters: +# DISCORD_RPC +# DESKTOP_ENTRY +# CLEAN_SRC + +echo_red () { + printf "\033[1;31m%s\033[0m\n" "$1" +} +echo_blue () { + printf "\033[1;34m%s\033[0m\n" "$1" +} +echo_yellow () { + printf "\033[1;33m%s\033[0m\n" "$1" +} +echo_white () { + printf "\033[1;37m%s\033[0m\n" "$1" +} +_done () { + printf "\033[1;32mDone\033[0m\n\n" +} +warning () { + printf "\033[0;33m%s\033[0m\n" "$1" > /dev/stderr +} + +abort() +{ + echo_red "ERROR: ABORTING" > /dev/stderr + exit "$1" +} + +APT_PACKAGES="gcc g++ git make cmake libsdl2-dev libicu-dev pkg-config libjansson-dev libspeex-dev libspeexdsp-dev libcurl4-openssl-dev libcrypto++-dev libfontconfig1-dev libfreetype6-dev libpng-dev libssl-dev libzip-dev" +DNF_PACKAGES="gcc gcc-c++ jansson-devel openssl-devel SDL2-devel libicu-devel speexdsp-devel libcurl-devel cmake fontconfig-devel freetype-devel libpng-devel libzip-devel mesa-libGL-devel" +PACMAN_PACKAGES="gcc gcc-libs git cmake sdl2 fontconfig libzip libpng curl jansson speexdsp openssl icu" + +echo_white "Installing packages" +if [ -n "$(command -v apt)" ] +then + sudo apt install --no-install-recommends -y $APT_PACKAGES || abort $? +elif [ -n "$(command -v dnf)" ] +then + sudo dnf install $DNF_PACKAGES || abort $? +elif [ -n "$(command -v pacman)" ] +then + sudo pacman -S --needed $PACMAN_PACKAGES || abort $? +else + warning "Unsupported packaging system. Skipping package install and attempting to continue" +fi +_done + +if [ "$DESTDIR" = "" ] ; then + DESTDIR='OpenRCT2' +fi +if [ "$SRCDIR" = "" ] ; then + SRCDIR='openrct2-src' +fi + +mkdir -p "$SRCDIR" +cd "$SRCDIR" || abort $? + +## PULLING SOURCE ## ./$SRCDIR +echo_white "Getting source code" +if [ -d "OpenRCT2" ] +then + echo_blue "Source code already present" + cd OpenRCT2 || abort $? + git pull || abort $? +else + git clone https://github.com/OpenRCT2/OpenRCT2.git || abort $? + cd OpenRCT2 || abort $? +fi +_done + +unset DISCORDRPC +add_discord_rpc() +{ + DISCORDRPC=true + echo_white "Getting source code" + if [ -d "discord-rpc" ] + then + echo_blue "Discord rpc already present" + git -C discord-rpc pull || abort $? + else + git clone https://github.com/discordapp/discord-rpc.git || abort $? + fi + echo_white "Getting rapidjson fix" + if [ -d "rapidjson" ] + then + echo_blue "rapidjson already present" + git -C rapidjson pull || abort $? + else + git clone https://github.com/janisozaur/rapidjson.git || abort $? + fi + cp -r rapidjson/include/rapidjson discord-rpc/src || abort $? + _done +} +remove_discord_rpc() +{ + unset DISCORDRPC + if [ -d "discord-rpc" ] + then + echo_yellow "Discord rpc present, deleting" + rm -rf discord-rpc || abort $? + fi + if [ -d "rapidjson" ] + then + echo_yellow "rapidjson present, deleting" + rm -rf rapidjson || abort $? + fi +} + +## DISCORD-RPC ## ./$SCRDIR/OpenRCT2 +if [ -n "$DISCORD_RPC" ] +then + if [ "$DISCORD_RPC" = "true" ] + then + add_discord_rpc + else + remove_discord_rpc + fi +else + printf "Add discord rich presence?(may cause compilation errors)[y/N]: " + read -r discord + case $discord in + [Yy]* ) + add_discord_rpc + ;; + * ) + remove_discord_rpc + ;; + esac +fi + +## CMAKE ## ./$SCRDIR/OpenRCT2 +echo_white "Preparing build files" +if [ -d "build" ] +then + echo_yellow "Build files already present. Deleting and rebuilding" + rm -rd build +fi +mkdir -p build || abort $? +cd build || abort $? +cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. || abort $? +_done + +## BUILD ## ./$SCRDIR/OpenRCT2/build +echo_white "Building binary" + +if ! make -j "$(nproc --all)" +then + if [ -n "$DISCORDRPC" ] + then + echo_red "Compliation with discord-rpc failed. Retry without." >/dev/stderr + abort 1 + else + abort 1 + fi +fi +_done +echo_white "Building g2.dat" +make g2 -j "$(nproc --all)" || abort $? +_done + +## DOWNLOAD DATA ## ./$SCRDIR/OpenRCT2/build +echo_white "Downloading game data" +mkdir -p temp_install || abort $? +DESTDIR=./temp_install make install || abort $? +_done + +## MAKE FOLDERS ## ./$SCRDIR/OpenRCT2/build +echo_white "Making game folders" +mkdir -p "../../../$DESTDIR" || abort $? +mkdir -p "../../../$DESTDIR/data" || abort $? +cp -r temp_install/usr/local/share/openrct2/* "../../../$DESTDIR/data" || abort $? +cp "temp_install/usr/local/share/icons/hicolor/256x256/apps/openrct2.png" "../../../$DESTDIR/icon.png" || abort $? +cp openrct2 openrct2-cli "../../../$DESTDIR" || abort $? +_done + +cd "../../.." || abort $? +cd "$DESTDIR" || abort $? + +add_desktop_entry() +{ + { + echo "[Desktop Entry]" + echo "Name=OpenRCT2" + echo "Comment=RCT2 but Open!" + echo "Type=Application" + echo "Exec=$(pwd)/openrct2" + echo "Icon=$(pwd)/icon.png" + echo "Categories=Game" + echo "Keywords=Roller;Coaster;Tycoon" + } > openrct2.desktop + sudo ln -sf "$(pwd)/openrct2.desktop" "/usr/share/applications" +} + +## DESKTOP ENTRY ## ./$DESTDIR +if [ -n "$DESKTOP_ENTRY" ] +then + if [ "$DESKTOP_ENTRY" = "true" ] + then + add_desktop_entry + fi +else + printf "Do you want to add a desktop entry (/usr/share)?[Y/n]: " + read -r yn + case $yn in + [Nn]* ) + ;; + * ) + add_desktop_entry + ;; + esac +fi + +cd .. || abort $? + +## CLEAN ## ./ +if [ -n "$CLEAN_SRC" ] +then + if [ "$CLEAN_SRC" = "true" ] + then + rm -rf "$SRCDIR" + fi +else + printf "Clean source files?[Y/n]: " + read -r yn + case $yn in + [Nn]* ) + ;; + * ) + rm -rf "$SRCDIR" + ;; + esac +fi + +exit 0 diff --git a/zrct2/zsrct2 b/zrct2/zsrct2 new file mode 100755 index 0000000..4800df6 --- /dev/null +++ b/zrct2/zsrct2 @@ -0,0 +1,129 @@ +#!/bin/sh + +usage() { +echo "Usage: zrct [options] <operation>" +echo 'Set of operations for OpenRCT2 server management. +By default reads zsrct2.conf for config + +Operations: + start start in screen instance in background + startLocal start the server in the current console + console prompt the screen instance (C-a + d to leave) + backup make a backup of current save + getautosave get latest autosave and sets it as active save + -> auto backup of current save + | alias: get + update recompile from latest source. Will overwrite previous server + +Options: + -h display help + -c <file> use this file for config + -s <save> uses provided save file + -S <server> uses this server instance (folder) +' +} + +save_backup () { + echo "Backing up '$save_file' to $1.sv6.gz" + cp "$save_file" "$backup_path/$1.sv6" + gzip "$backup_path/$1.sv6" +} + +unset save_file + +config_file="zsrct2.conf" +while getopts ":hS:s:c:" opt; +do + case $opt in + h) + usage + exit 0 + ;; + S) + server_path=${OPTARG} + ;; + s) + save_file=${OPTARG} + ;; + c) + config_file=${OPTARG} + ;; + *) + echo "Incorrect option: $OPTARG" + exit + ;; + esac +done + +if [ ! -f "$config_file" ] +then + echo "Generating default config file" + echo '# zsrct2 1.0 config file +SAVE_NAME= +SERVER_NAME=server +PORT=11753 +SCREEN_NAME=OpenRCT2 +SAVE_FOLDER=saves +BACKUP_FOLDER=saves/backup +DATA_FOLDER=data +SRC_FOLDER=src +' >> "$config_file" || exit 1 +fi + +save_name=$(grep "SAVE_NAME=" "$config_file" | cut -d '=' -f2-) +server_path=$(grep "SERVER_PATH=" "$config_file" | cut -d '=' -f2-) +port=$(grep "PORT=" "$config_file" | cut -d '=' -f2-) +screen_name=$(grep "SCREEN_NAME=" "$config_file" | cut -d '=' -f2-) +save_path=$(grep "SAVE_PATH=" "$config_file" | cut -d '=' -f2-) +backup_path=$(grep "BACKUP_PATH=" "$config_file" | cut -d '=' -f2-) +data_path=$(grep "DATA_PATH=" "$config_file" | cut -d '=' -f2-) +src_path=$(grep "SRC_PATH=" "$config_file" | cut -d '=' -f2-) + +[ -z "$save_file" ] && save_file="$save_path/$save_name.sv6" + +[ ! -d "$backup_path" ] && mkdir -p "$backup_path" +[ ! -d "$save_path" ] && mkdir -p "$save_path" +[ ! -d "$data_path" ] && mkdir -p "$data_path" + +shift $((OPTIND-1)) + +if [ "$1" ] + then + if [ "$1" = "start" ] + then + screen -dmS "$screen_name" "$server_path/openrct2" host "$save_file" --user-data-path "$data_path" --port "$port" --headless + + elif [ "$1" = "startLocal" ] + then + "$server_path/openrct2" host "$save_file" --user-data-path "$data_path" --port "$port" --headless + + elif [ "$1" = "console" ] + then + screen -r "$screen_name" + + elif [ "$1" = "backup" ] + then + + if [ -n "$2" ] + then + save_backup "$2" + else + save_backup "$save_name"_"$(date -u "+20%y-%m-%d_%H%M%S")" + fi + + elif [ "$1" = "getautosave" ] || [ "$1" = "get" ] + then + autosave=$(ls -l "$data_path/save/autosave/" | grep autosave | tail -1 | tr -s ' ' | cut -d ' ' -f9-) + mv "$save_file" "$backup_path/$(basename "$save_file")"_"$(date -u "+20%y-%m-%d_%H%M%S")" + mv "$data_path/save/autosave/$autosave" "$save_file" + + elif [ "$1" = "update" ] + then + DISCORD_RPC=false DESKTOP_ENTRY=false CLEAN_SRC=false SRCDIR="$src_path" DESTDIR="$server_path" zrct2-install + + else + usage + fi +else + usage +fi diff --git a/ztr/zdtol b/ztr/zdtol new file mode 100755 index 0000000..780419b --- /dev/null +++ b/ztr/zdtol @@ -0,0 +1,76 @@ +#!/bin/sh + +usage () { + echo "zdtol [options] <name>" + echo " Searches a label for partition or disk /dev/<name>" + echo " Options: " + echo " -h Show this message then exit" + echo " -d Discard prefix (/dev/)" + echo " -n <num> Show only nth label" +} + +error () { + printf "\033[1;31m%s\033[0m\n" "$1" >&2 +} + +while getopts ":hdn:" opt; +do + case $opt in + h) + usage + exit 0 + ;; + n) + if ! echo "$OPTARG" | grep -Eq '^[0-9]+$' + then + error "n argument has to be an integer" + exit 2 + fi + if [ -z "$OPTARG" ] + then + error "n needs an argument" + exit 2 + fi + if [ "$OPTARG" -lt 1 ] + then + error "argument has to be greater than 0" + exit 2 + fi + _arg_n=$OPTARG + _opt_n=y + ;; + d) + _opt_d=y + ;; + \?) + echo "Uknown option: $OPTARG" >&2 + usage + exit 1 + ;; + esac +done + +shift $((OPTIND-1)) + +if [ $# -le 0 ] +then + usage + exit 2 +fi + +_LIST=$(lsblk -lnio NAME,LABEL | tr -s ' ') + +for _I in "$@" +do + if [ -n "$_opt_d" ] ; then + _I=$(echo "$_I" | cut -d'/' -f3) + fi + _ret=$(echo "$_LIST" | grep "$_I" | tr -s ' ' | cut -d' ' -f2- | sed '/^[[:space:]]*$/d') + if [ -n "$_opt_n" ] ; then + echo "$_ret" | cut -d'\n' -f"$_arg_n" + else + echo "$_ret" + fi +done + +exit 0 diff --git a/ztr/zltod b/ztr/zltod new file mode 100755 index 0000000..afe42ed --- /dev/null +++ b/ztr/zltod @@ -0,0 +1,96 @@ +#!/bin/sh + +usage () { + echo "zltod [options] <name>" + echo " Options: " + echo " -h Show this message then exit" + echo " -w Search whole words only" + echo " -i Ignore case" + echo " -d Show device name instead of partition" + echo " -f Give full name (/dev/)" +} + +while getopts ":hwidf" opt; +do + case $opt in + h) + usage + exit 0 + ;; + w) + _opt_w=y + ;; + i) + _opt_i=y + ;; + d) + _opt_d=y + ;; + f) + _opt_f=y + ;; + \?) + echo "Uknown option: $OPTARG" >&2 + usage + exit 1 + ;; + esac +done + +if [ -n "$_opt_w" ] +then + if [ -z "$GREPARGS" ] + then + GREPARGS="-" + fi + GREPARGS=$GREPARGS"w" +fi + +if [ -n "$_opt_i" ] +then + if [ -z "$GREPARGS" ] + then + GREPARGS="-" + fi + GREPARGS=$GREPARGS"i" +fi + +shift $((OPTIND-1)) + +if [ $# -le 0 ] +then + usage + exit 2 +fi + +_LIST=$(lsblk -lnio NAME,LABEL | tr -s ' ') +_SEARCH=$(echo "$_LIST" | grep $GREPARGS "$*" ) + +if [ -z "$_SEARCH" ] ## no result +then + exit 0 +elif [ "$(echo "$_SEARCH" | wc -l)" -gt 1 ] ## more than 1 result +then + echo "More than one result" >&2 + exit 3 +fi + +_partition=$(echo "$_SEARCH" | awk '{print $1}') + +if [ -n "$_partition" ] && [ -n "$_opt_f" ] +then + printf "/dev/" +fi +if [ -n "$_opt_d" ] +then + if echo "$_SEARCH" | grep -q nvme + then + echo "$_partition" | cut -c1-7 + else + echo "$_partition" | cut -c1-3 + fi +else + echo "$_partition" +fi + +exit 0 diff --git a/ztr/ztr b/ztr/ztr new file mode 100755 index 0000000..28d2818 --- /dev/null +++ b/ztr/ztr @@ -0,0 +1,147 @@ +#!/bin/sh + +usage() +{ + echo "command | ztr [options]" + echo "" + echo "Options:" + echo " -h Display help" + echo " -l Load files in data dir" + echo " -p Generate partitions" + echo " -d Generate disks" + echo " -P Print lines" + echo " -s Print in sed script format" +} + +gen_tr_lines_disk () +{ + for I in $(ls /sys/block) + do + _t=$(zdtol $I | head -n1) + if [ -n "$_t" ] + then + printf "/dev/%s:%s\n" "$I" "$(zdtol $I | head -n1)" + fi + done +} + +gen_tr_lines_part () +{ + lsblk -lnio NAME,LABEL | tr -s ' ' | while read in + do + in="$in " + if [ -n "$(echo "$in" | cut -d' ' -f2-)" ] + then + printf "/dev/%s:%s\n" "$(echo "$in" | cut -d' ' -f1)" "$(echo "$in" | cut -d' ' -f2- | sed -e 's/ *$//g' )" + fi + done +} + +gen_sed_script () +{ + while read in + do + printf "s|%s|%s|g;" "$(echo "$in" | cut -d':' -f1)" "$(echo "$in" | cut -d':' -f2-)" + done +} + +if [ -n "$XDG_CONFIG_HOME" ] +then + CONFIG_PATH="$XDG_CONFIG_HOME/ztr" +else + CONFIG_PATH="$HOME/.config/ztr" +fi + +DATA_PATH=$CONFIG_PATH/data/ + +FILE=/dev/stdin + +while getopts ":hPsf:pdl" opt; +do + _opt=y + case $opt in + h) + usage + exit 0 + ;; + P) + _opt_P=y + ;; + s) + _opt_s=y + ;; + f) + _opt_f=y + _arg_f=$OPTARG + _opt_data=y + ;; + d) + _opt_d=y + _opt_data=y + ;; + p) + _opt_p=y + _opt_data=y + ;; + l) + _opt_l=y + _opt_data=y + ;; + \?) + echo "Uknown option: $OPTARG" >&2 + usage + exit 1 + ;; + esac +done + +shift $((OPTIND-1)) + +if [ -z "$_opt_data" ] +then + _opt_p=y + _opt_d=y + _opt_l=y + _opt_L=y +fi + +mkdir -p "$DATA_PATH" + +LINES="" + +if [ -n "$_opt_f" ] +then + LINES="$(printf "%s\n%s" "$LINES" "$(cat "$_arg_f")" )" +fi +if [ -n "$_opt_l" ] +then + LINES="$(printf "%s\n%s" "$LINES" "$(cat "$DATA_PATH"/* 2>/dev/null)" )" +fi +if [ -n "$_opt_p" ] +then + LINES="$(printf "%s\n%s" "$LINES" "$(gen_tr_lines_part)" )" +fi +if [ -n "$_opt_d" ] +then + LINES="$(printf "%s\n%s" "$LINES" "$(gen_tr_lines_disk)" )" +fi + +LINES=$(echo "$LINES" | sed '/^\s*$/d') + + +if [ -n "$1" ] ; then + FILE="$1" +fi +if [ -t 0 ] || [ -n "$_opt_P" ] && [ -z "$1" ] +then + + if [ -n "$_opt_s" ] + then + echo "$LINES" | gen_sed_script | rev | cut -c2- | rev + else + echo "$LINES" + fi + +else + sed "$(echo "$LINES" | gen_sed_script | rev | cut -c2- | rev)" < "$FILE" +fi diff --git a/zumask/zumask b/zumask/zumask new file mode 100755 index 0000000..acdb0b9 --- /dev/null +++ b/zumask/zumask @@ -0,0 +1,15 @@ +#!/bin/sh + +shift $((OPTIND-1)) + +if [ $# -gt 0 ] +then + _path=$* +else + _path=$(pwd) +fi + +#find "$_path" -type d -exec chmod $(umask -S) {} \; +#find "$_path" -type f -exec chmod $(umask -S | tr -d 'x') {} \; +chmod "$(umask -S)" $(find "$_path" -type d) +chmod "$(umask -S | tr -d 'x')" $(find "$_path" -type f) diff --git a/zupdate/sysupdate b/zupdate/sysupdate new file mode 100755 index 0000000..308cbec --- /dev/null +++ b/zupdate/sysupdate @@ -0,0 +1,88 @@ +#!/bin/sh + +linuxpkgnames="" + +lock_file=/tmp/update.lock + +get_running_programs() +{ + echo "$1" | tr '-' '_' | grep -qwE "$(echo "$linuxpkgnames" | tr '-' '_')" && echo linux + for I in $1 + do + pgrep -xi "$I" >/dev/null && echo $I + done +} + +stop() +{ + rm "$lock_file" + exit $1 +} + +fetch_error() +{ + echo "Error: Could not fetch packages" > /dev/stderr + kdialog --passivepopup "Error during system updates: could not fetch packages" 60 --title "System Updates" + stop 3 +} + +update_error() +{ + echo "Error: Could not upgrade system" > /dev/stderr + kdialog --passivepopup "Error during system updates: could not upgrade" 60 --title "System Updates" + stop 3 +} + + +# Check if another process is running +if [ -n "$(pgrep zupdate)" ] || [ -f "$lock_file" ] +then + echo "Updates are already running" > /dev/stderr + exit 1 +fi +touch "$lock_file" + +which zupdate >/dev/null || { echo "zupdate not installed" >&2; stop 4; } + +# package manager specific variables +if which apt >/dev/null 2>&1 +then + linuxpkgnames="linux-image-*" +elif which pacman > /dev/null 2>&1 +then + linuxpkgnames="linux|linux-hardened|linux-lts|linux-zen|linux-mainline|linux-rt|linux-git|linux-next" +else + echo "Unsupported package manager" > /dev/stderr + stop 2 +fi + +#fetch updates + +size=$(zupdate -Mkd) || fetch_error + +packages=$(zupdate -L | cut -d' ' -f1) || fetch_error + +if [ -z "$packages" ] +then + echo "No updates" + stop 0 +fi + +# find running updates +running_programs=$(get_running_programs "$packages") +if [ -n "$running_programs" ] +then + if ! kdialog --yesno "The following running programs have recieved updates: \n$(echo "$running_programs" | sed 's|^| - |g')" --yes-label "Continue" --no-label "Cancel" --title "Updates" + then + echo "Update cancelled" > /dev/stderr + stop 3 + fi +fi + +kdialog --passivepopup "Installing updates for $size download" 10 --title "System Updates" +#update +zupdate -yu || update_error +#end update +kdialog --passivepopup "Updates finished" 5 --title "System Updates" + +stop 0