Files
dotfiles/.zprezto/abra/functions.zsh

331 lines
5.9 KiB
Bash

# : magic
# ':' just evaluates the given expression
# : "${1:?dirs?}" exits saying "dirs?" if $1 is unset or null
# : "${1:=best}" sets $1 to "best" if $1 is unset or null
# ${(j:,:)arr} joins arr with commas
function reset_facl {
FUNCTION_PRELUDE
: "${1:?dirs?}"
# reassign owner to root
chown --recursive "root:root" "${@:1}"
chmod --recursive "u=rwX,g=,o=" "${@:1}"
# clear facl
setfacl --recursive --remove-all "${@:1}"
}
function add_facl {
FUNCTION_PRELUDE
: "${1:?user?}"
: "${2:?dirs?}"
local arr=(
user::rwX
user:${1}:rwX
default:user:${1}:rwX
group::0
default:group::0
mask::rwX
default:mask::rwX
other::0
default:other::0
)
setfacl --recursive --modify "${(j:,:)arr}" "${@:2}"
}
function is_local {
local HOSTNAME="$(hostname)"
[[ "$HOSTNAME" =~ '^abra0' || "$HOSTNAME" == "svxf2-osx" ]]
}
function mount_sshfs {
FUNCTION_PRELUDE
: "${1:?destination name?}"
local DESTINATION="$1"
local LOCAL_MOUNT_PATH="/Users/$USER/mount/$1"
mkdir -p "$LOCAL_MOUNT_PATH"
if mount | grep "$LOCAL_MOUNT_PATH"; then
umount "$LOCAL_MOUNT_PATH";
fi
sshfs \
-o allow_other \
-o kernel_cache \
-o direct_io \
-o auto_cache \
-o defer_permissions \
-o noappledouble \
-o cache=yes \
-o Compression=yes \
-o reconnect \
-o workaround=rename \
"$DESTINATION:/" "$LOCAL_MOUNT_PATH"
# if [[ ! -e "$LOCAL_COMMAND_PIPE_PATH" ]]; then
# echo "Can't find the command pipe"
# return
# fi
echo "Starting remote command pipe..."
ssh $DESTINATION "
set -euxo pipefail
if [[ -e /tmp/sshfs_command_pipe ]]; then
rm /tmp/sshfs_command_pipe
fi
mkfifo /tmp/sshfs_command_pipe
tail -f /tmp/sshfs_command_pipe
" | zsh -x
}
# function sublr {
# FUNCTION_PRELUDE
# local HOSTNAME=$(hostname)
# local FULL_PATH=$(realpath $1)
# echo "subl /Users/abra/mount/$HOSTNAME/$FULL_PATH" > /tmp/sshfs_command_pipe
# }
function cdtmp {
FUNCTION_PRELUDE
local DATESTR="$(date +%Y_%m_%d_%H_%M_%S)"
local DIR_PATH="${HOME}/tmp/auto_${DATESTR}"
if [[ -n "$1" ]]; then
local DIR_PATH="${DIR_PATH}_$1"
fi
if [[ -e "${DIR_PATH}" ]]; then
echo "${DIR_PATH} already exists"
return 1
fi
mkcd "${DIR_PATH}"
echo "${DIR_PATH}"
}
function mkcd {
FUNCTION_PRELUDE_NO_XTRACE
: "${1:?where?}"
if [[ -e $1 ]]; then
echo "file exists"
return 1
fi
mkdir $1
cd $1
}
function publish {
FUNCTION_PRELUDE
rsync2 "$@" "dedi:/var/www/abra.me/list/"
}
function twitch {
FUNCTION_PRELUDE
livestreamer "http://www.twitch.tv/${1}" "${${@:2}:-best}"
}
function save_stream {
FUNCTION_PRELUDE
local NAME=$1
if [[ ! -d $NAME ]]; then
echo ':('
return 1
fi
while true; do
local CURDATE=$(date +"%Y-%m-%d-%H:%M:%S")
livestreamer "https://twitch.tv/$NAME" -o "$NAME/$CURDATE.mp4" || true
sleep 30
done
}
function dcp {
FUNCTION_PRELUDE
git add --all :/
git commit -m "${*:-$(whoami) at $(date)}"
git push
}
function .dcp {
FUNCTION_PRELUDE
.g add -u
.g commit -m "${*:-$(whoami) at $(date)}"
.g push
}
function strip_tags {
FUNCTION_PRELUDE
find . -type f -name '*.mp3' \
| tr '\n' '\0' \
| xargs -0 -n1 mid3v2 --delete-frames=APIC,TXXX,USLT,TBPM
}
function levi_beet {
FUNCTION_PRELUDE
ssh levi -- sudo BEETSDIR=/projects/config/beets -u torrent beet "$@"
}
function beet_comments {
FUNCTION_PRELUDE
: "${1:?who?}"
: "${2:?what?}"
levi_beet modify \'"comments=$2"\' \'"albumartist::$1"\'
}
function sync_music {
FUNCTION_PRELUDE
# if ! is_local; then
# echo "Should be local"
# return 1
# fi
local MUSIC_DIR="$HOME/music/library"
mkdir -p "$MUSIC_DIR"
rsync2 \
--delete \
"levi:/projects/music/" \
"$MUSIC_DIR/"
}
# function .g-init() {
# git init --bare $HOME/.dotgit
# .g config --local status.showUntrackedFiles no
# .g remote add origin ssh://dedi/root/.dotgit
# git config --global user.name "Your Name"
# git config --global user.email you@example.com
# .g remote add origin ssh://dedi/var/www/git.abra.me/dot.git
# }
# make_user() {
# FUNCTION_PRELUDE
# if is_local; then
# echo "You're local"
# return 1
# fi
# adduser --system --shell /bin/zsh --disabled-password $1 || return 1
# # wget abra.me/.dotfiles.tar.gz -O /home/$1/.dotfiles.tar.gz
# # tar xf /home/$1/.dotfiles.tar.gz -C /home/$1
# # chown $1:nogroup -R /home/$1/
# }
# function latest() {
# echo -n ("${(@f)$(ls *(om[1,$1]))}")
# }
function testo {
local A="${@[2,3]}"
./t.py ${(t)A} "$A" ${A[@]}
unset A
local A=("$@[2,3]")
./t.py ${(t)A} "$A" ${A[@]}
unset A
./t.py "$@[2,3]"
}
# testo 1 "2 3" 4
# shit shit shit s hist h is htisthsi 2018-06-30
function ffmpeg_timelapse {
FUNCTION_PRELUDE
local DATE="$1"
ls "/mnt/a/snaps/$DATE" | wc -l
ffmpeg \
-framerate 60 \
-pattern_type glob -i "/mnt/a/snaps/$DATE/*.jpg" \
"${@:2}" \
-y "/mnt/a/tmp/$DATE.mp4"
mv "/mnt/a/tmp/$DATE.mp4" "/mnt/a/$DATE.mp4"
rm -rf "/mnt/a/snaps/$DATE"
}
function ffmpeg_timelapse_today {
local TODAY="$(date '+%Y-%m-%d')"
ffmpeg_timelapse "$TODAY" -preset ultrafast -crf 20
}
function update_.g {
if [[ ! -d "$HOME/.dotgit" ]]; then
# no git to update
return
fi
# force updating origin to new server
.g remote set-url origin https://gitea.abra.me/svxf/dotfiles.git
local RES="$(.g pull --no-stat 2>&1)"
if [[ $? != 0 ]]; then
# something went wrong
return
fi
# no verbose :(
# if [[ ! $RES =~ "Already up.to.date" ]]; then
# echo "Updated .g"
# echo $RES
# echo "======"
# echo
# zshreload
# fi
}
function .sh_setup {
echo 'wget abra.me/.sh && chmod +x .sh && ./.sh'
}
function send_new_torrents {
FUNCTION_PRELUDE
rsync2 --remove-source-files ~/Downloads/*.torrent(mm-10) levi:/projects/torrent_autoadd/
}