This commit is contained in:
Pieds-Nus 2023-06-17 18:01:20 +00:00
parent f7665eeb14
commit d32fbc1205
5 changed files with 556 additions and 13 deletions

View File

@ -1,13 +1,20 @@
#!/bin/bash
#mod-settings.dat
# Dependencies : you'll need a dmenu-like to run this script.
# On debian and debian-like distros (ubuntu, mint...), simply run:
# sudo apt install dmenu
# If you use something else than dmenu, edit the following line accordingly:
dmenu=dmenu
#Verbosity log
verb=true
# Edit this line to meet your installation
factorio_dir="/DATA/Games/factorio"
factorio_dir="$HOME/Games/factorio"
test "$verb" && echo "Directory: $factorio_dir"
# If you have installed new mods, this script will revert to previously saved
# mods before launching factorio. To update the modlist for a save, select the
@ -15,36 +22,59 @@ factorio_dir="/DATA/Games/factorio"
# factorimods -r
# and select that save. This will replace the previously saved mods for that
# save with the active list from factorio.
cd $factorio_dir/saves
cd "$factorio_dir/saves"
#Ask the user to select a save
save=$(ls -t *.zip | grep -v autosave | rev | cut -d "." -f 2- | rev | $dmenu -i -l 5 -p "Chose a save file: ")
save=$(ls -t *.zip | rev | cut -d "." -f 2- | rev | $dmenu -l 5 -p "Chose a save file: ") || ( echo "No save selected" ; exit 1)
test $? -ne 0 && echo "No save selected" && exit 1
test "$verb" && echo "Save selected : $save"
# If -r option is passed, replace the modlist of hte save with the latest modlist from factorio
# If -r option is passed, replace the modlist of the save with the latest modlist from factorio
if test "$1" = "-r" ; then
cp ../mods/mod-list.json mod-list.json_$save
modlist=$save
cp ../mods/mod-list.json "mod-list.json_$save"
cp ../mods/mod-list.json "mod-settings.dat_$save"
modlist="$save"
test "$verb" && echo "Modlist updated"
# Check if a modlist exists for that save
elif test -s "mod-list.json_$save" ; then
modlist=$save
modlist="$save"
test "$verb" && echo "Modilist found: mod-list.json_$save"
else
# Otherwise, prompt to load another modlist
modlist=$(ls -t mod-list.json_* | cut -d "_" -f 2- | $dmenu -p "No mod list found for savegame \"$save\". Select mod list from available: ")
test $? -ne 0 && echo "No modlist selected" && exit 1
if test $? -ne 0 ; then
echo "No modlist selected"
# And prompt to save it for later
echo -e "yes\nno" | $dmenu -p "Save selected modlist for savegame $save ?" | grep -q "yes" && cp mod-list.json_$modlist mod-list.json_$save && modlist=$save
elif echo -e "yes\nno" | $dmenu -p "Save selected modlist for savegame $save ?" | grep -q "yes" ; then
test "$verb" && echo "Modlist selected: $modlist"
cp "mod-list.json_$modlist" "mod-list.json_$save"
cp "mod-settings.dat_$modlist" "mod-settings.dat_$save"
modlist="$save"
fi
fi
# Link that modlist to the file read by factorio
ln -fs ../saves/mod-list.json_$modlist ../mods/mod-list.json
ln -fs "../saves/mod-list.json_$modlist" ../mods/mod-list.json
test -s "../saves/mod-settings.dat_$modlist" && ln -fs "../saves/mod-settings.dat_$modlist" ../mods/mod-settings.dat
# Launch the game and the requested save
cd ..
echo "LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libhugetlbfs-2.23.so HUGETLB_MORECORE=thp HUGETLB_RESTRICT_EXE=factorio"
echo "$factorio_dir/bin/x64/factorio --load-game $factorio_dir/saves/$save.zip"
$factorio_dir/bin/x64/factorio --load-game $factorio_dir/saves/$save.zip
# hugepage memory allocation
# source : https://www.reddit.com/r/factorio/comments/dr72zx/8_ups_gain_on_linux_with_huge_pages/
# comment that "LD_PRELOAD..." line to prevent hugepage and go back to normal glibc allocating
#LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libhugetlbfs-2.23.so HUGETLB_MORECORE=thp HUGETLB_RESTRICT_EXE=factorio "$factorio_dir/bin/x64/factorio" --load-game "$factorio_dir/saves/$save.zip"
# Factorio launcher that does some memory alloc optimisations and shit : [ $HOME/Games/factorio/launchers.sh ]
"$factorio_dir/launcher.sh" --load-game \"$factorio_dir/saves/$save.zip\"
# $factorio_dir/bin/x64/factorio --load-game $factorio_dir/saves/$save.zip
diff -q "saves/mod-list.json_$save" mods/mod-list.json && echo -e "save\nforget" | dmenu -p "Mod list updated." | grep -q "save" && cp mods/mod-list.json "saves/mod-list.json_$save"
diff -q "saves/mod-settings.dat_$save" mods/mod-settings.dat && echo -e "save\nforget" | dmenu -p "Mod settings updated." | grep -q "save" && cp mods/mod-settings.dat "saves/mod-settings.dat_$save"

5
mpv-suspend Normal file
View File

@ -0,0 +1,5 @@
#!/bin/sh
/usr/bin/mpv $@ & wait
/usr/bin/sleep 3
/usr/bin/test $(/usr/bin/xprintidle) -gt 2500 && /usr/bin/doas /usr/local/bin/suspend-then-hibernate

156
pnstatus Normal file
View File

@ -0,0 +1,156 @@
#!/bin/bash
# Dependencies
#acpitools
#nmcli
#awk
#bc
remode() {
echo $((($(tail -n 1 /tmp/pnstatus_mode) $1 1 + 3 ) % 3)) >> /tmp/pnstatus_mode
kill $waitpid
}
trap "remode +" SIGUSR1
trap "remode -" SIGUSR2
echo -e "\
Mode control for pnstatus script\n\
Write the desired mode as last line of this file\n\
\t0|min :\tminimal mode :\t| 23:25\n\
\t1|some :\tintermediate mode :\t| Wifi | CPU:RAM:SWAP | BATTERY | DA/TE TI:ME\n\
\t2|all :\t complete mode :\t| Network | CPU:RAM:SWAP | TEMP | BATTERY | DA/TE TI:ME\n\
0" \
> /tmp/pnstatus_mode
command=echo
if test "$1" = "-x" ; then
# echo "-x"
command="xsetroot -name"
shift 1
fi
if test "$1" = "-c" ; then
# echo "-c"
command="$2"
shift 2
fi
# echo $command
echo ${1:-0} >> /tmp/pnstatus_mode
while true ; do
res=""
case $(tail -n 1 /tmp/pnstatus_mode) in
0|"min")
res="$(date '+%H:%M')"
sleep 30 &
waitpid=$!
;;
1|"some")
connect=""
#Check ethernet interface status
grep "up" /sys/class/net/eth0/operstate > /dev/null && connect=$connect\E
#Check wifi interface status
grep "up" /sys/class/net/wlan0/operstate > /dev/null && connect=$connect\W
# CPU load (divided by 4 cause 4-core i5 cpu.
# Dirty way of dealing with bash and non-integer calculations : )
# | tr -d "." removes the decimal point read in /proc/loadavg
# If it is always written with three significative numbers, should work...
cpu="$(echo "($(acpitool -c | head -n 3 | tail -n 1 | rev | cut -d " " -f 2 | rev)-800)/26" | bc)"
# ram usage
ram=$(free | head -n 2 | tail -n 1 | awk '{printf( "%.0f", $3/$2*100 )}')
# swap usage
swap=$(head -n 16 /proc/meminfo | tail -n 2 | tr -s " " | cut -d " " -f 2 | tr "\n" " " | awk '{printf( "%.0f", ($1-$2)*100/$1)}')
# battery state
bat=$(((($(cat /sys/class/power_supply/BAT0/capacity)+$(cat /sys/class/power_supply/BAT1/capacity))/2)))
case "$(cat /sys/class/power_supply/BAT0/status)" in
"Full") batstate="" ;;
"Discharging") batstate="-" ;;
"Charging") batstate="+" ;;
"Not charging") batstate="." ;;
*) batstate="?" ;;
esac
case "$(cat /sys/class/power_supply/BAT1/status)" in
"Full") batstate="$batstate" ;;
"Discharging") batstate="$batstate-" ;;
"Charging") batstate="$batstate+" ;;
"Not charging") batstate="$batstate." ;;
*) batstate="$batstate?" ;;
esac
res="| $connect | $cpu:$ram:$swap | B:$batstate$bat% | $(date '+%a %d/%m %H:%M')"
sleep 2 &
waitpid=$!
;;
2|"all")
# Network
#Should look into iw dev wls3p0 scan dump to get that info quicker
wifi=$(timeout 0.5 nmcli -f IN-USE,SSID,BARS d wifi list | grep ^\* | cut -c 9- | tr -s " ")
grep -q "up" /sys/class/net/eth0/operstate && eth=eth
test -n "$wifi" && test -n "$eth" && eth=" - $eth"
res=$res"| $wifi$eth"
#CPU
# cpu1=$(cut -d " " -f 1 /proc/loadavg)
# cpu2=$(cut -d " " -f 2 /proc/loadavg)
# cpu=$(awk '{printf( "%.0f", $cpu1*25 "% - " $cpu2*25 "%" )}')
#cpu=$(cut -d " " -f 1,2 /proc/loadavg | awk '{print $1*25 "%-" $2*25 "%" }')
#cpu freq
res=$res" | C:$(echo "($(acpitool -c | head -n3 | tail -n 1 | rev | cut -d " " -f 2 | rev)-800)/26" | bc)"
#cpu load avg (1 minute) (4 thread-processor)
res=$res" L:$(echo "scale=0 ; $(cut -d " " -f 1 /proc/loadavg) * 25/1" | bc)"
#RAM
mems=($(head -n 3 /proc/meminfo | grep -v MemFree | tr -s " " | cut -d " " -f 2))
res="$res | R:$(((${mems[0]}-${mems[1]})*100/${mems[0]}))"
#SWAP
res="$res S:$(head -n 16 /proc/meminfo | tail -n 2 | tr -s " " | cut -d " " -f 2 | tr "\n" " " | awk '{printf( "%.0f", ($1-$2)*100/$1)}')"
#TEMP °C
#Generic way :
# $(( ( $(cat /sys/class/thermal/thermal_zone*/temp | rev | cut -c 4- | rev | tr "\n" "+") 0 ) / $(ls -d /sys/class/thermal/thermal_zone* | wc -w) ))
#Specific for only 2 thermal_zones :
res="$res | T:$(( ( $(cat /sys/class/thermal/thermal_zone0/temp | rev | cut -c 4- | rev) + $(cat /sys/class/thermal/thermal_zone1/temp | rev | cut -c 4- | rev) ) / 2 ))°C"
#BATTERY
bat=$(cat /sys/class/power_supply/BAT1/status)
case "$bat" in
"Full") bat="" ;;
"Discharging") bat="-" ;;
"Charging") bat="+" ;;
"Not charging") bat="." ;;
*) bat="?" ;;
esac
bat=$(echo "( $(cat /sys/class/power_supply/BAT*/capacity | tr "\n" +) 0 ) / $(ls -l /sys/class/power_supply/BAT* | wc -l)" | bc)%$bat
res="$res | B:$bat"
#DATE
res="$res | $(date "+%a %d/%m %H:%M")"
sleep 2 &
waitpid=$!
;;
*)
echo $usage
exit 1
;;
esac
$command "$res"
wait $waitpid
done

143
sss Normal file
View File

@ -0,0 +1,143 @@
#!/bin/bash
usage="\
Usage: $0 [-p PREFIX] [-s SUFFIX] [-e EXTENSION] [-g \"GEOMETRY\"] [-q QUALITY] [-v] image1 image2 image3 ...\n
Use ImageMagick's convert tool to reduce the size of a selection of image files.\n
\n
Options:\n
-p PREFIX\tAdd a prefix to output file name. Can be used to provide a \n
\t\tdifferent output folder.\n
-s SUFFIX\tAppend a suffix to the file name, before the extension.\n
-t\t\tReplace the filename with creation time from exif\n
-g \"GEOMETRY\"\tSpecify a desired geometry. Default : \"1920x1080>\" \n
\t\t(see https://www.imagemagick.org/Magick++/Geometry.html\n
\t\tfor detailled doc)\n
-e EXTENSION\tSpecify a file extension (recognized by Magick).\n
\t\tIf not specified, reuse orinal file's extension. \n
-q QUALITY\tSet the jpeg compression rate in %. Default : 85\n
-x\t\tStrip exif metadata. Default off\n
-t\t\tUse exif creation time instead of original filename when renaming.\n
\t\tCan be used with -p and -s\n
-S\t\tDeactivate JPEG sampling factor. First level of compression, very little \n
\t\tloss in quality. Default on (4:2:0)\n
-m\t\tDeactivate median blurring of the image. This filter can smooth some of\n
\t\tthe noise with minimal loss in details. \n
\t\tBetter compression, with some loss of quality. Default on.\n
-M SIZE\tSpecify the size of the median blurr (useful for fine-tuning text scans).\n
\t\tDefault: 2\n
-G\t\tApply gaussian blurr to the image. Smooths noise in background or low-light\n
\t\timages, but creates a loss of detail. \n
\t\tBest compression, most loss of quality. Default off\n
-v\t\tMake verbose\n
-P\t\tPrint progress (for use with zenity --progress)\n
-h\t\tPrint this help and exit.\n"
if [[ $# == 0 ]] ; then echo -e $usage ; exit 0 ; fi
#Preset defaults
nopt=1
geom="1920x1080>"
qual="85"
ext="jpg"
verbose=false
sampling="-sampling-factor 4:2:0"
median="2x2-1-1"
while getopts p:s:g:q:M:e:xGmhSvPt option ; do
case "${option}"
in
p) ((nopt+=2)) ;
prefix=${OPTARG}
;;
s) ((nopt+=2)) ;
suffix=${OPTARG}
;;
g) ((nopt+=2)) ;
geom=${OPTARG}
;;
q) ((nopt+=2)) ;
qual=${OPTARG}
;;
t) ((nopt+=1)) ;
time=true;
;;
e) ((nopt+=2)) ;
EXT=${OPTARG}
;;
v) ((nopt+=1)) ;
verbose=true;
;;
P) ((nopt+=1)) ;
progress=0;
;;
x) ((nopt+=1)) ;
exif=true;
;;
S) ((nopt+=1)) ;
unset sampling;
;;
G) ((nopt+=1)) ;
gaussian=true;
;;
m) ((nopt+=1)) ;
unset median
;;
M) ((nopt+=2)) ;
a=${OPTARG}
b=$((a/2))
median=$a\x$a-$b-$b
echo $median
unset a
unset b
;;
h | -help)
echo -e $usage ;
exit 0
;;
*)
echo -e $usage ;
echo "Unreconized option: ${option} ${OPTARG}"
exit 0
;;
esac
done
test $verbose == true && echo -e "Old file \t Old size \t Old res \t New res \t New size \t New file \t Compression"
for f in "${@:$nopt}" ; do
test "$verbose" == true && status="$f \t $(ls -lh $f | cut -d " " -f 5) \t $(identify $f | rev | cut -d " " -f 7 | rev) \t " ; size=$(ls -l "$f" | cut -d " " -f 5)
name="$(echo $f | rev | cut -d '.' -f 2- | rev)"
ext="${EXT:-$(echo $f | rev | cut -d '.' -f 1 | rev)}"
test "$time" && exif -m -t "DateTime" "$f" > /dev/null && name="$(exif -m -t "DateTime" $f | tr ": " ".-")" || echo "$f : no DateTime found in exif data. Using filename instead."
nf="$prefix""$name""$suffix"."$ext"
while test -e $nf ; do nf="$prefix""$name""$suffix"_$(((++i)))."$ext" ; done
unset i
((insize+=$(ls -l "$f" | cut -d " " -f 5)))
convert \
-auto-orient \
-define jpeg:dct-method=float \
$sampling \
${median:+\-median $median} \
-interlace Plane \
${gaussian:+-gaussian-blur 0.1} \
-colorspace RGB \
-resize "$geom" \
-quality "$qual" "$f" "$nf";
test "$exif" = true && exif --remove --no-fixup -o $nf $nf > /dev/null
((outsize+=$(ls -l "$nf" | cut -d " " -f 5)))
test $verbose == true && echo -e $status"$(identify "$nf" | rev | cut -d " " -f 7 | rev) \t $(ls -lh "$nf" | cut -d " " -f 5) \t "$nf" \t $((100*$(ls -l "$nf" | cut -d " " -f 5)/$size))% "
test $progress && echo $(((++progress * 100 / $#)))
done
test $verbose == true && echo -e "Average compression : $((100*outsize/insize))%"

209
swapctl Normal file
View File

@ -0,0 +1,209 @@
#!/bin/bash
# Warning : this script calls heavily on sudo for swapfile manipulation.
# This is not clean.
# But it is easier in my workflow.
# Please consider removing all "sudo" from this file,
# and calling it with administrative rights
# if you want to use it in a proper way.
usage="$0 -d DIR -n NAME -s SIZE -p PRIO add|remove
Add or remove swapfiles to micro-manage disk usage.
-i : Interactively prompt for parameters.
(other options are NOT ignored, but set as preselection)
-d DIR : Set DIRectory to place swapfile in. Default: /mnt
-n NAME : Set filename for the swapfile. Default: swapfileN
-s SIZE : SIZE in MB (only when creating a swapfile). Default: 1024
-p PRIO : Priority to use with swapon. Range from 0 to 1000. Default: 500
-f : Do not edit fstab with the changes. Default: edit fstab
-h : Display this help and exit."
if test $# -eq 0 ; then
echo -e "$usage"
echo "You must provide either 'add' or 'remove' !"
exit 1
fi
SWAP_FOLDER="/mnt"
SWAP_FILENAME="swapfile"
SWAP_SIZE="1024" #in MB
SWAP_PRIO_MAX=750 #range 0..1000
SWAP_PRIO_INC=50
FSTAB=true
sep="\n\t#############\n"
while getopts ifhp:s:n:d: arg ; do
case $arg in
h) echo -e "$usage" ; exit 0 ;;
i) INT=true ;;
f) FSTAB=false ;;
d) SWAP_FOLDER="$OPTARG" ;;
n) SWAP_FILENAME="$OPTARG" ;;
s)
if test "$OPTARG" -gt 0 2> /dev/null ; then
SWAP_SIZE="$OPTARG"
else
echo -e "invalid argument: -s $OPTARG\t (size expected in MB without suffix)"
exit 1
fi
;;
p) SWAP_PRIO_MAX="$OPTARG" ;;
*) echo -e "$usage" ; echo "Invalid option: $arg"; exit 1 ;;
esac
done
shift $((( $OPTIND - 1 )))
if test "$1" = "add" ; then
MODE="add"
elif test "$1" = "remove" ; then
MODE="remove"
elif test "$INT" != true ; then
echo -e "$usage"
echo "You must provide either add or remove !"
exit 1
fi
if test "$INT" = true ; then
if test "$MODE" != "add" -a "$MODE" != "remove" ; then
until test "$REPLY" = "add" -o "$REPLY" = "remove" -o "$REPLY" = "quit"
do
read -p "Mode [add|remove|quit]: "
done
test "$REPLY" = "quit" && exit 0
MODE=$REPLY
unset REPLY
fi
until test -d "$REPLY"
do
read -e -i "$SWAP_FOLDER" -p "Directory : "
done
SWAP_FOLDER=$REPLY
unset REPLY
if test "$MODE" = "add" ; then
read -e -i "$SWAP_FILENAME" -p "Filename : "
SWAP_FILENAME=$REPLY
unset REPLY
until test "$REPLY" -gt 0 2> /dev/null ;
do
read -e -i "$SWAP_SIZE" -p "Swapfile size (MiB) : "
done
SWAP_SIZE="$REPLY"
unset REPLY
until test "$REPLY" -gt 0 2> /dev/null ;
do
read -e -i "$SWAP_PRIO_MAX" -p "Swap priority {0-1000} : "
done
SWAP_PRIO_MAX="$REPLY"
unset REPLY
fi
until test "$REPLY" = "y" -o "$REPLY" = "n"
do
read -e -i "$(test "$FSTAB" = true && echo y || echo n)" -p "Update fstab ? [y|n]: "
done
test "$REPLY" = "n" && FSTAB=false
unset REPLY
echo ; echo " About to run the following command:"
echo $0 -d "$SWAP_FOLDER" -n "$SWAP_FILENAME" -s "$SWAP_SIZE" -p "$SWAP_PRIO_MAX" -f "$MODE"
until test "$REPLY" = "y" -o "$REPLY" = "n"
do
read -p "Proceed ? [y|n]: "
done
test "$REPLY" != "y" && exit 1
fi
if test "$MODE" = "add" ; then
N=$(ls $SWAP_FOLDER/$SWAP_FILENAME* | wc -l)
swapfile=$SWAP_FOLDER/$SWAP_FILENAME$N
prio=$((($SWAP_PRIO_MAX - $N * $SWAP_PRIO_INC)))
echo -e "$sep Creating swapfile: $swapfile$sep"
dd if=/dev/urandom of=$swapfile bs=1M count=$SWAP_SIZE status=progress
chmod 0600 $swapfile
mkswap -c -L $SWAP_FILENAME$N $swapfile
swapon -p $prio $swapfile
echo -e "\n\n"
swapon
elif test "$MODE" = "remove" ; then
N=$(ls $SWAP_FOLDER/$SWAP_FILENAME* | wc -l)
swapfile=$SWAP_FOLDER/$SWAP_FILENAME$((($N - 1)))
echo -e "$sep Removing swapfile: $swapfile$sep"
swapoff $swapfile && sudo rm $swapfile
echo -e "\n\n"
swapon
fi
if test "$FSTAB" = true ; then
echo -e "$sep Editing /etc/fstab accordingly$sep"
tmpfile=/tmp/swapctl.fstab
if test "$MODE" = "add" ; then
cat /etc/fstab > $tmpfile
echo -e "$swapfile\t\t\t\t\t\t\t\tnone\t\t\tswap\tsw,pri=$prio\t\t\t0\t\t0" >> $tmpfile
else
cat /etc/fstab | grep -v $swapfile > $tmpfile
fi
echo -e "# The lines below this one will be the new fstab file : \n\n$(cat $tmpfile)" | more
while true ; do
echo -e "$sep What to do with this fstab ?"
echo -e "[ \e[4ma\\e[0mccept changes | \e[4me\e[0mdit file | dis\e[4mc\e[0mard changes | show \e[4md\e[0miff ]"
read -p "[ a | e | c | d ]: " prompt
case $prompt in
a|accept)
mv /etc/fstab.2 /etc/fstab.3
mv /etc/fstab.1 /etc/fstab.2
mv /etc/fstab /etc/fstab.1
mv $tmpfile /etc/fstab
echo -e "$sep Changes accepted. New contents of /etc/fstab :$sep"
cat /etc/fstab | more
break
;;
e|edit)
${EDITOR:-nano} $tmpfile
;;
c|discard)
echo -e "$sep Changes discarded. /etc/fstab remains unchanged.$sep"
break
;;
d|diff)
echo -e "\n\n\tdiff\t\t/etc/fstab\t\t\t\t\t\t/tmp/swapctl.fstab\n"
diff --tabsize=4 -E -Z -b -t -y /etc/fstab /tmp/swapctl.fstab | more
;;
esac
done
fi