500 lines
14 KiB
Makefile
500 lines
14 KiB
Makefile
#! /usr/bin/make
|
|
# Shell configuration for Zsh, works almost completely for Bash as well
|
|
|
|
export LESS=-R
|
|
alias info='info --vi-keys'
|
|
export DIST=$(lsb_release --id | cut -d' ' -f2)
|
|
|
|
# Some aliases
|
|
alias println='printf "\n"'
|
|
|
|
# find pruning avoids permission denied errors - see https://unix.stackexchange.com/a/121020/272449
|
|
alias conf='find ~/.config/nvim ~/.config ~/.gradle/gradle.properties /etc -type d ! \( -readable \) -prune -o -type f -print | fzf --tiebreak=end,index --preview "bat --color=always --style=numbers --line-range :500 {}" --bind "enter:execute(test -O {} && $EDITOR {} || sudoedit {}),del:execute(gio trash {})"'
|
|
alias editgrub='sudoedit /etc/default/grub && sudo update-grub'
|
|
alias editenv='sudoedit /etc/environment'
|
|
editshell() {
|
|
nvim $(test $# -lt 1 && echo "$SHELL_FUNCTIONS" || (results=$(find "$dropbox_path/tools/setup" -maxdepth 1 -name "$1.sh") && test "$results" && echo "$results") || echo "$(dirname $SHELL_FUNCTIONS)/$1") && r
|
|
}
|
|
|
|
alias graphics='lspci -vnn | grep VGA --color=never'
|
|
|
|
alias startMinecraftServer='curl https://ipinfo.io/ip | xclip -sel clip && cd ~/daten/games/sharedgames/minecraft/server && java -jar forge-1.12.2-14.23.5.2768-universal.jar -mx 8G'
|
|
|
|
# Fast shortcuts
|
|
alias t='task'
|
|
alias c='clear'
|
|
alias q='exit'
|
|
alias v='nvim'
|
|
alias grp='grep -iIn'
|
|
alias grpr='grp -r'
|
|
b() { bat "$@" 2>/dev/null || ls -lah "$@" }
|
|
cdd() { cd "$@" 2>/dev/null || cd "$(dirname "$1")" "${@:2}" }
|
|
tr() { tree -C -a -L 2 "$@" | less }
|
|
|
|
SHELL_FUNCTIONS="${BASH_SOURCE[0]:-${(%):-%x}}"
|
|
alias r="reset && source $SHELL_FUNCTIONS"
|
|
|
|
dropbox_path="$(cat ~/.dropbox/info.json | grep -Po '"'"path"'"\s*:\s*"\K([^"]*)')"
|
|
|
|
# Add an "alert" alias for long running commands. Use like so: sleep 10; alert
|
|
#if [ command -v notify-send >/dev/null 2>&1 ]; then
|
|
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(echo "$history[$HISTCMD]" | sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
|
|
#fi
|
|
|
|
# Fix broken mime messages
|
|
alias fixmime='sudo rm /usr/share/mime/packages/kde.xml && sudo update-mime-database /usr/share/mime'
|
|
|
|
# Find zmv in man because I need to look it up all the time ^^
|
|
man() {
|
|
(test "$1" = "zmv" && LESS="$LESS+/^ *zmv *\\[" /usr/bin/man zshcontrib) || /usr/bin/man "$@"
|
|
}
|
|
|
|
sedcomment() { sed -i 's/$1/#\0/' "${@:2}" }
|
|
seduncomment() { sed -i 's/#\($1\)/\0/' "${@:2}" }
|
|
|
|
mozedit() (
|
|
set -eo pipefail
|
|
file="$1"
|
|
tmpfile="/tmp/$(basename $1).tmp"
|
|
unsetopt multios # https://stackoverflow.com/a/58261480
|
|
# ( [[ -t 0 ]] && echo 'STDIN is attached to TTY' ) || cat
|
|
{ echo -en '\x02\x21\x4C\x18TT\x0\x0' && dd if="$file" bs=1 skip=12 } 2>&1 >"$tmpfile" | head -1 | cut -d+ -f1 | xargs printf "%04x\n" | read len
|
|
tmpfile2="${tmpfile}2"
|
|
cat "$tmpfile" | sed "s/TT/\\x${len:2:2}\\x${len:0:2}/" | unlz4 >"$tmpfile2"
|
|
cat "$tmpfile2" | sed "$2" | lz4 -l | dd bs=1 skip=8 seek=12 of="$file" conv=notrunc
|
|
)
|
|
|
|
theme_save="$(xdg-user-dir CACHE)/theme"
|
|
theme_default="dark"
|
|
theme() {
|
|
theme="$1"
|
|
if test "$2" = "kde" || test "$theme" != "$THEME" -a "$2" != "q"
|
|
then kcmshell5 kcm_lookandfeel &
|
|
fi
|
|
export THEME="$theme"
|
|
echo "$theme">"$theme_save"
|
|
|
|
#mozfile=$(find $HOME/.mozilla/firefox/ -maxdepth 2 -name addonStartup.json.lz4 | grep ".test")
|
|
case "$theme" in
|
|
(light)
|
|
export BAT_THEME="OneHalfLight"
|
|
konsoleprofile colors="Light"
|
|
sed -i 's/#\(include.*\/light-256.theme\)/\1/' "$(xdg-user-dir CONFIG)/taskrc"
|
|
#mozedit $mozfile '/light@mozilla/,/"enabled"/ s/\("enabled":.*\)false/\1true/; /dark@mozilla/,/"enabled"/ s/\("enabled":.*\)true/\1false/'
|
|
;;
|
|
(dark)
|
|
export BAT_THEME="OneHalfDark"
|
|
konsoleprofile colors="Breeze"
|
|
sed -i 's/^include.*light-256.theme/#\0/' "$(xdg-user-dir CONFIG)/taskrc"
|
|
#mozedit $mozfile '/light@mozilla/,/"enabled"/ s/\("enabled":.*\)true/\1false/; /dark@mozilla/,/"enabled"/ s/\("enabled":.*\)false/\1true/'
|
|
;;
|
|
esac
|
|
}
|
|
export THEME=$(cat "$theme_save" 2> /dev/null)
|
|
export THEME=${THEME:-$theme_default}
|
|
theme $THEME
|
|
|
|
updateDeps() {
|
|
name="$1"
|
|
pattern="$2"
|
|
depth="4"
|
|
case $name in
|
|
gradle-wrapper.properties) depth=6;;
|
|
esac
|
|
shift 2
|
|
oldversion="[0-9.]\+"
|
|
while test $# -gt 1; do
|
|
case "$1" in
|
|
--pattern) oldversion="$2";;
|
|
--version) version="$2";;
|
|
esac
|
|
shift 2
|
|
done
|
|
echo name $name depth $depth pattern $oldversion version $version
|
|
find -maxdepth $depth -type f -name $name | while read f
|
|
do echo $f
|
|
sed -i "s/\($pattern\)$oldversion/\1$version/gw /dev/stdout" $f
|
|
done
|
|
}
|
|
alias updateKotlin='updateDeps build.gradle.kts '\''kotlin(\"jvm\") version \"'\'' --version'
|
|
alias updateGradle='updateDeps gradle-wrapper.properties "services.gradle.org\/distributions\/gradle-" --version'
|
|
alias updateUtils='updateDeps build.gradle.kts '\''\"com.github.Xerus2000.util\", \".*\", \"'\'' --pattern '\''[^\"]\+'\'' --version'
|
|
|
|
# Kill all shell background processes
|
|
alias killbg='kill ${${(v)jobstates##*:*:}%=*}'
|
|
|
|
# Kil all Java processes except IDEA
|
|
# Pass "-9" to force-kill and "-q" to not output what has been killed
|
|
killJava() {
|
|
pgrep -f 'java' | while read id
|
|
do if [[ $(ps --no-headers -o cmd $id) != *"idea"* ]]; then
|
|
[[ "$@" == "-q" ]] || echo "killing $(ps -p $id -o pid,cmd --width 350 --no-headers)"
|
|
if [[ "$@" == "-9" ]]
|
|
then kill -9 $id
|
|
else kill $id
|
|
fi
|
|
fi; done
|
|
}
|
|
|
|
clearNpmCache() {
|
|
rm -rf $TMPDIR/react-*
|
|
rm -rf node_modules/
|
|
npm cache verify
|
|
npm install
|
|
}
|
|
|
|
# NAVIGATION & DISK
|
|
|
|
up() {
|
|
if [[ $# < 1 ]] ; then
|
|
cd ..
|
|
else
|
|
CDSTR=""
|
|
for i in {1..$1} ; do
|
|
CDSTR="../$CDSTR"
|
|
done
|
|
cd $CDSTR
|
|
fi
|
|
}
|
|
|
|
# Switch directory & ls
|
|
cl() {
|
|
cd $1
|
|
ls -A
|
|
}
|
|
|
|
# shows size usage for disk or folder
|
|
s() {
|
|
if test "-d" = "$1"
|
|
then df -h
|
|
else sudo du --max-depth ${1:-1} -xh | sort -h | grep -v "^0"
|
|
fi
|
|
}
|
|
|
|
alias f='find -not -path "*.sync*" -and -not \( -name daten -prune \)'
|
|
alias f1='find -mindepth 1 -maxdepth 1'
|
|
|
|
# FILES
|
|
|
|
alias lowercase='rename "y/A-Z/a-z/"'
|
|
|
|
mkcd() {
|
|
mkdir -p $1 && cd $1
|
|
}
|
|
|
|
# Creates directory $2 and moves $1 into it
|
|
mvk() {
|
|
mkdir -p $(dirname "$2")
|
|
mv "$1" "$2"
|
|
}
|
|
|
|
# Moves from $1 to $2 and creates a symlink in place of $2
|
|
mvln() {
|
|
file=$(test -f "$1" && echo 1 || echo 0)
|
|
if test -d $1; then
|
|
mkdir -p "$2"
|
|
mv -T $1 $2
|
|
else
|
|
mv -v $1 $2
|
|
fi
|
|
[ $file -gt 0 -a -d $2 ] && 2="$2/$(basename $1)"
|
|
ln -vsr $2 $1
|
|
}
|
|
|
|
# OTHER
|
|
|
|
pathadd() {
|
|
local IFS=":"
|
|
local result="$@"
|
|
unset IFS
|
|
cat /etc/environment | head -1 | cut -d'"' -f2 | tr ":" "\n" | while read v; do
|
|
[[ " $@ " =~ " $v " ]] || result+=":$v"
|
|
done
|
|
echo PATH=\"${result}\"\\n$(cat /etc/environment | tail -n +2) | sudo tee /etc/environment
|
|
}
|
|
|
|
writecompletion() {
|
|
echo "#compdef $1" > "_$1"
|
|
nvim "_$1"
|
|
}
|
|
|
|
resetdocker() {
|
|
#aptremove docker-ce
|
|
kill $(ps -e | grep docker | cut -d' ' -f2)
|
|
sudo rm -rf /var/run/docker /var/lib/docker
|
|
sudo rm /var/run/docker.*
|
|
#aptinstall docker-ce
|
|
}
|
|
|
|
# MUSIC
|
|
|
|
music_folder="/home/janek/daten/musik"
|
|
audio_folder="/home/janek/daten/audio"
|
|
music_original_folder="$audio_folder/songs/_raw"
|
|
|
|
makePl() { echo ${2:-empty} >$1.m3u8 }
|
|
makePls() {
|
|
i=1; while test $i -le ${1:-9}; do makePl "${2:-Ω}$i" "empty$i"; ((i++)); done
|
|
}
|
|
|
|
findsongs() {
|
|
find -regextype posix-extended -maxdepth 1 -type f -regex "${1:-.*\.(mp3|flac|wav|m4a)}" -printf "%P\n"
|
|
}
|
|
|
|
addmix() {
|
|
mv -v "$1.cue" "$audio_folder/recordings/cues"
|
|
addsong "$1.flac" Mixes
|
|
}
|
|
|
|
addsong() {
|
|
test "$1" = "-q" && shift && quick="true"
|
|
|
|
local raw="${1%.*}"
|
|
local isflac=$(case "$1" in *.flac) echo "true";; esac)
|
|
test "$isflac" || (echo "Converting to flac..." && flac "$1" --totally-silent)
|
|
|
|
test "$quick" || (echo "Press ENTER when the metadata of the flac file is correct..." && read)
|
|
|
|
echo "Converting to mp3..."
|
|
lame -V0 "$1" --silent
|
|
|
|
mkdir -p "$music_folder/$2"
|
|
mkdir -p "$music_original_folder/$2"
|
|
|
|
local destination="$music_folder/$2/$raw.mp3"
|
|
echo "Moving mp3 with metadata to $destination"
|
|
ffmpeg -i "$raw.mp3" -i "$raw.flac" -c copy -map_metadata 1 "$destination" -v warning "${@:3}" && rm "$raw.mp3"
|
|
test ! "$PWD" -ef "$music_original_folder/$2" && mv -v "$raw.flac" "$music_original_folder/$2" && (test "$isflac" || rm -v "$1")
|
|
}
|
|
|
|
addalbum() {
|
|
mkdir -p "$music_folder/$1"
|
|
mkdir -p "$music_original_folder/$1"
|
|
f1 -regextype posix-extended -type f -regex "\./(cover|folder)\.(png|jpg|jpeg)" | while read cover; do
|
|
foundcover="true"
|
|
cp -v "$cover" "$music_folder/$1" &&
|
|
mv -v "$cover" "$music_original_folder/$1"
|
|
done
|
|
test "$foundcover" || f1 -regextype posix-extended -type f -regex ".*\.(png|jpg|jpeg)" | while read cover; do
|
|
cp -v "$cover" "$music_folder/$1" &&
|
|
mv -v "$cover" "$music_original_folder/$1"
|
|
done
|
|
|
|
IFS=$'\n'
|
|
for s in $(findsongs ".*\.(flac|wav)"); do
|
|
highlight "$s"
|
|
addsong -q "$s" "$1" "${@:2}"
|
|
done
|
|
unset IFS
|
|
}
|
|
|
|
formatsongs() {
|
|
_pwd="$PWD"
|
|
find "$PWD" "$@" -type d | while read d; do cd $d; findsongs | while read song; do
|
|
new="$(formatsong "$song")"
|
|
test "$new" != "$song" && mv -v "$song" "$new"
|
|
done; done
|
|
cd "$_pwd"
|
|
IFS=$'\n'
|
|
for f in $(find "$@" -name "*.wav"); do
|
|
echo "Converting $f to ${f%.*}.flac"
|
|
ffmpeg -i "$f" "${f%.*}.flac" -v warning && rm -v $f
|
|
done
|
|
unset IFS
|
|
}
|
|
|
|
formatsong() {
|
|
echo "$1" | sed -e 's/\([ ([]\)\(ft\|Ft\|Feat\)\([ .]\)/\1feat\3/;s/\([ ([]\)feat /\1feat. /;s/ \((Original Mix)\|(Original)\|(Full Stream)\|.NCS Release.\)//i;s/ \(\..\{2,4\}\)$/\1/;s/---/ - /;s/^[0-9]\{6\}_//;s/ / /g;s/\(\w\)+\(\w\)/\1 \2/g' -
|
|
}
|
|
|
|
# INSTALL
|
|
|
|
snapinstall() {
|
|
sudo snap install "$@" && refreshSetup
|
|
}
|
|
|
|
alias aptupdate='sudo apt-get update'
|
|
alias aptremove='sudo apt-get remove'
|
|
alias aptpurge='sudo apt-get purge'
|
|
alias aptclean='sh -c "sudo apt-get clean && sudo apt-get autoremove"'
|
|
upgrade() {
|
|
if test $DIST = "neon"
|
|
then sudo pkcon update
|
|
else sudo apt update && sudo apt upgrade
|
|
fi
|
|
sudo snap refresh
|
|
flatpak update -y
|
|
sudo npm update -g
|
|
# npm -g outdated --parseable=true | cut -d : -f 4 | xargs -n 1 npm -g install
|
|
}
|
|
|
|
uma() {
|
|
if test "$1"
|
|
then printf 'y\nj\na' | umake "$1" "$2" "/opt/${3:-$2}"
|
|
else
|
|
uma ide idea-ultimate idea
|
|
uma android android-studio android/android-studio && yes | /opt/android/sdk/tools/bin/sdkmanager --licenses && pathadd /opt/android/sdk/tools/bin /opt/android/sdk/platform-tools && sudo apt install qemu-kvm && sudo adduser janek kvm
|
|
test -d "/opt/vscode" && uma ide visual-studio-code vscode
|
|
fi
|
|
}
|
|
|
|
alias aptrepo='sudo add-apt-repository'
|
|
_aptrepo_n="$(test "$OSTYPE" != "cygwin" && add-apt-repository --help | grep -- "-n")"
|
|
aptrepon() {
|
|
test $_aptrepo_n && aptrepo -y -n "$@" || aptrepo -y "$@"
|
|
}
|
|
aptrepos() {
|
|
for repo in "$@"
|
|
do aptrepon $repo
|
|
done
|
|
aptupdate
|
|
}
|
|
alias aptinstall='sudo apt-get install'
|
|
aptrepoinstall() {
|
|
aptrepo -yu $1
|
|
if (( $# > 1 ))
|
|
then aptinstall "${@:2}"
|
|
else aptinstall "${1##*/}"
|
|
fi
|
|
}
|
|
|
|
# Gets the download url for the latest release of a package provided via GitHub Releases
|
|
# Usage: ghrelease USER REPO [PATTERN]
|
|
ghrelease() {
|
|
result=$(curl -s "https://api.github.com/repos/$1/$2/releases/latest" | grep -o "http.*${3:-deb}" | awk '{ print length(), $0}' | sort -n | cut -d' ' -f2-)
|
|
echo "$result" | grep amd64 || echo "$result"
|
|
}
|
|
|
|
# Installs a local or remote(http/https) deb package and removes it after installation
|
|
installdeb() {
|
|
set -e
|
|
loc="/tmp/install.deb"
|
|
case $1 in
|
|
http*) sudo wget -O "$loc" $1;;
|
|
*) loc="$1"
|
|
esac
|
|
sudo dpkg -i "$loc"
|
|
sudo apt -f install
|
|
sudo rm -f "$loc"
|
|
}
|
|
|
|
installgh() {
|
|
installdeb $(ghrelease "$@")
|
|
}
|
|
|
|
# Downloads all dependencies of a package into the current directory
|
|
download-debs() {
|
|
apt-get download $1 && apt-cache depends -i $1 | awk '/Depends:/ {print $2}' | xargs apt-get download
|
|
}
|
|
|
|
# RANDOM
|
|
|
|
typeset -A _clean_map
|
|
if test "$BASH_VERSION"
|
|
then _clean_map=([m]=~/.mozilla [c]=~/.cache)
|
|
else _clean_map=(m ~/.mozilla c ~/.cache)
|
|
fi
|
|
_clean_folders=(~/.oracle_jre_usage ~/.gradle-kotlin-dsl ~/.thumbnails)
|
|
clean() {
|
|
|
|
for f in $_clean_folders
|
|
do test -d $f && rm -rv $f
|
|
done
|
|
|
|
highlight "g to clean gradle"
|
|
if [[ $1 =~ "g" ]]; then
|
|
find ~/.gradle/* -maxdepth 0 -not -name "gradle.properties" -exec rm -r {} \; -print
|
|
find ~/daten/projects/ -name .gradle -print -exec rm -r {} \;
|
|
fi
|
|
|
|
if test "$BASH_VERSION"; then
|
|
for k in "${!_clean_map[@]}"; do
|
|
highlight "$k to delete $_clean_map[$k]
|
|
echo "key : $k"
|
|
echo "value: ${_clean_map[$k]}"
|
|
done
|
|
else
|
|
for k in "${(@k)_clean_map}"; do
|
|
highlight "$k to delete $_clean_map[$k]
|
|
[[ $1 =~ "$k" ]] && rm -rfv "$_clean_map[$k]"
|
|
done
|
|
fi
|
|
|
|
aptclean && echo "Cleaned apt"
|
|
|
|
highlight "e to delete empty folders"
|
|
[[ $1 =~ "e" ]] && find -empty -type d -delete -print
|
|
}
|
|
|
|
function zipdiff() {
|
|
diff -W200 -y <(unzip -vql $1 | sort -k8) <(unzip -vql $2 | sort -k8) #--suppress-common-lines
|
|
}
|
|
|
|
# PROJECTS
|
|
|
|
source "$(dirname $SHELL_FUNCTIONS)/git"
|
|
source "$(dirname $SHELL_FUNCTIONS)/projects"
|
|
|
|
pdiff() {
|
|
diff -r $1 $2 -x .git -x .idea -x .gradle
|
|
}
|
|
|
|
genssh() {
|
|
mkdir -p ~/.ssh
|
|
test -f ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -q -N "" -C ${1:-$(hostname)}
|
|
if test "$OSTYPE" = "cygwin"
|
|
then cat ~/.ssh/id_rsa.pub>/dev/clipboard
|
|
else xclip -sel clip < ~/.ssh/id_rsa.pub
|
|
fi && echo "SSH Public key copied to clipboard"
|
|
}
|
|
|
|
createproject() {
|
|
mkcd $1
|
|
|
|
echo 'plugins {
|
|
kotlin("jvm") version "1.3.41"
|
|
application
|
|
id("com.github.johnrengelman.shadow") version "5.1.0"
|
|
id("com.github.ben-manes.versions") version "0.21.0"
|
|
}
|
|
|
|
sourceSets {
|
|
main {
|
|
java.srcDir("src/main")
|
|
resources.srcDir("src/resources")
|
|
}
|
|
test {
|
|
java.srcDir("src/test")
|
|
}
|
|
}' > build.gradle.kts
|
|
|
|
}
|
|
|
|
# SWAP
|
|
|
|
mem_stat() {
|
|
mem_total="$(free | head -2 | tail -1 | awk '{print $2}')"
|
|
mem_free="$(free | head -2 | tail -1 | awk '{print $4}')"
|
|
mem_percentage=$((mem_free * 100 / $mem_total))
|
|
swap_total="$(free | tail -1 | awk '{print $2}')"
|
|
swap_used="$(free | tail -1 | awk '{print $3}')"
|
|
swap_percentage=$(($swap_used * 100 / $swap_total))
|
|
|
|
echo -e "Free memory:\t$((mem_free / 1024))/$((mem_total / 1024)) MB\t($mem_percentage%)"
|
|
echo -e "Used swap:\t$((swap_used / 1024))/$((swap_total / 1024)) MB\t($swap_percentage%)"
|
|
}
|
|
|
|
stopswap() {
|
|
mem_stat
|
|
if (( $swap_used == 0 ))
|
|
then echo "No swap is in use."
|
|
elif (( $swap_used + 100000 < mem_free ))
|
|
then echo "Freeing swap..."
|
|
sudo swapoff -a
|
|
sudo swapon -a
|
|
mem_stat
|
|
else
|
|
echo "Not enough free memory!"
|
|
fi
|
|
}
|