You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
436 lines
13 KiB
436 lines
13 KiB
#!/bin/bash
|
|
# Make from ini file text file with strings like [SECTION]Name=Value
|
|
# $1 - input filename
|
|
# stdout - result file
|
|
export TEXTDOMAINDIR=/usr/share/locale
|
|
export TEXTDOMAIN=ublinux_functions
|
|
|
|
|
|
function ini2simple()
|
|
{
|
|
SECTION='[]'
|
|
cat $1 | while read a ;do
|
|
[ "$a" = "" ] && continue
|
|
if [ "${a#\[*\]}" = "" -a "$a" != "" ] ;then
|
|
SECTION=$a
|
|
else
|
|
echo "$SECTION$a"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Restore ini file from text file with strings like [SECTION]Name=Value
|
|
# $1 - input filename
|
|
# stdout - result file
|
|
function simple2ini()
|
|
{
|
|
LASTSECTION='[]'
|
|
cat $1 | while read a ;do
|
|
SECTION=${a%%\]*}']'
|
|
if [ "$SECTION" != "$LASTSECTION" ] ;then
|
|
[ "LASTSECTION" != "[]" ] && echo
|
|
echo "$SECTION"
|
|
LASTSECTION=$SECTION
|
|
fi
|
|
echo ${a#\[*\]}
|
|
done
|
|
}
|
|
|
|
# It include string from $2 file and apply to $1 file
|
|
# $1 - base file
|
|
# $2 - included file
|
|
function apply2simple()
|
|
{
|
|
cat "$2" | while read a ;do
|
|
SECTION=${a%%\]*}
|
|
SECTION=${SECTION#\[}
|
|
STR=${a#\[*\]}
|
|
PNAME=${STR%%=*}
|
|
PVAL=${a#*=}
|
|
# echo $SECTION $PNAME $PVAL
|
|
echo -ne >"$1.tmp"
|
|
echo -ne >"$1.lck"
|
|
FOUNDS=
|
|
cat "$1" | while read b ;do
|
|
BSECTION=${b%%\]*}
|
|
BSECTION=${BSECTION#\[}
|
|
BSTR=${b#\[*\]}
|
|
BPNAME=${BSTR%%=*}
|
|
BPVAL=${b#*=}
|
|
|
|
[ "$BSECTION" = "$SECTION" ] && FOUNDS=1
|
|
if [ "$BSECTION" = "$SECTION" -a "$BPNAME" = "$PNAME" ] ;then
|
|
b="$a"
|
|
rm -f "$1.lck"
|
|
fi
|
|
if [ "$BSECTION" != "$SECTION" -a "$FOUNDS" != "" -a -f "$1.lck" ] ;then
|
|
echo "$a" >> "$1.tmp"
|
|
rm -f "$1.lck"
|
|
fi
|
|
echo "$b" >> "$1.tmp"
|
|
done
|
|
[ -f "$1.lck" ] && echo "$a" >> "$1.tmp"
|
|
mv -f "$1.tmp" "$1"
|
|
rm -f "$1.lck"
|
|
done
|
|
}
|
|
|
|
# It include string from $2 ini file and apply to $1 ini file
|
|
# $1 - base file
|
|
# $2 - included file
|
|
function concatenate_ini()
|
|
{
|
|
[ -f "$1" -a -f "$2" ] || return 1
|
|
ini2simple "$1" >"$1.tmp"
|
|
ini2simple "$2" >"$2.tmp"
|
|
apply2simple "$1.tmp" "$2.tmp"
|
|
simple2ini "$1.tmp" >"$1"
|
|
rm -f "$1.tmp" "$2.tmp"
|
|
}
|
|
|
|
detectDE()
|
|
{
|
|
if [ x"$KDE_FULL_SESSION" = x"true" ]; then SESSION=kde
|
|
elif [ x"$XDG_CURRENT_DESKTOP" = x"XFCE" ]; then SESSION=xfce
|
|
elif [ x"$DESKTOP_SESSION" = x"LXDE" ]; then SESSION=lxde
|
|
elif [ x"$XDG_CURRENT_DESKTOP" = x"LXQt" ]; then SESSION=lxqt
|
|
elif [ x"$DESKTOP_SESSION" = x"i3" ]; then SESSION=i3
|
|
elif [ x"$XDG_CURRENT_DESKTOP" = x"i3" ]; then SESSION=i3
|
|
elif [ x"$DESKTOP_SESSION" = x"i3term" ]; then SESSION=i3term
|
|
elif [ x"$XDG_CURRENT_DESKTOP" = x"i3term" ]; then SESSION=i3term
|
|
elif [ x"$XDG_CURRENT_DESKTOP" = x"MATE" ]; then SESSION=mate
|
|
fi
|
|
if [ -z "$SESSION" ]; then
|
|
ps -A | grep -q "xfce4-session" && SESSION=xfce
|
|
ps -A | grep -q "kdeinit" && SESSION=kde
|
|
ps -A | grep -q " i3$" && SESSION=i3
|
|
ps -A | grep -q " i3term$" && SESSION=i3term
|
|
ps -A | grep -q "gnome-panel" && SESSION=gnome
|
|
ps -A | grep -q "gnome-shell" && SESSION=gnome-shell
|
|
fi
|
|
[ "$SESSION" = "kde" -a -f /etc/X11/wmsession.d/01Plasma ] && SESSION=plasma
|
|
[ -z "$SESSION" -a -x /usr/bin/startxfce4 ] && SESSION=xfce
|
|
[ -z "$SESSION" -a -x /usr/bin/startlxde ] && SESSION=lxde
|
|
[ -z "$SESSION" -a -x /usr/bin/startlxqt ] && SESSION=lxqt
|
|
echo $SESSION
|
|
}
|
|
|
|
# ===========================================================
|
|
# liblinuxlive functions
|
|
# ===========================================================
|
|
|
|
debug_log()
|
|
{
|
|
if grep -q "debug" /proc/cmdline ; then
|
|
echo "- debug: $*" >&2
|
|
log "- debug: $*"
|
|
fi
|
|
}
|
|
|
|
log()
|
|
{
|
|
echo "$@" 2>/dev/null >>/var/log/ublinuxlog
|
|
}
|
|
|
|
debug_mode()
|
|
{
|
|
if [ "$(cmdline_parameter debug)" -o "$DEBUGMODE" == "yes" ] ; then
|
|
name=$(basename $0)
|
|
slash="/"
|
|
[ "$(pwd)" == "/union" ] && slash=""
|
|
if ! test -f ${slash}var/log/ublinux/${name}.log ; then
|
|
echo "$0 -- debug mode enabled"
|
|
test -d ${slash}var/log/ublinux || mkdir -p ${slash}var/log/ublinux
|
|
echo $(date) > ${slash}var/log/ublinux/${name}.log || echo "can not create log file"
|
|
$0 "$@" 2>&1 | tee -a ${slash}var/log/ublinux/${name}.log
|
|
exit 0
|
|
fi
|
|
fi
|
|
}
|
|
|
|
echodebug()
|
|
{
|
|
[ "$DEBUG_IS_ENABLED" -o "$DEBUGMODE" == "yes" ] && echo "$1"
|
|
if [ -n "$2" ] ;then
|
|
command=$2
|
|
shift ; shift
|
|
if [ -z $1 ] ;then
|
|
$command
|
|
else
|
|
$command "$@"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Create module
|
|
# call mksquashfs with apropriate arguments
|
|
# $1 = directory which will be compressed to squashfs module
|
|
# $2 = output filesystem module file
|
|
# $3..$9 = optional arguments like -keep-as-directory or -b 123456789
|
|
#
|
|
create_module()
|
|
{
|
|
. /usr/lib/ublinux/os-config
|
|
. /etc/ublinux/config 2>/dev/null
|
|
echo " $@ " | egrep -q ' -comp | -noD ' && MKSQFS_OPTS=
|
|
mksquashfs "$1" "$2" $MKSQFS_OPTS $3 $4 $5 $6 $7 $8 $9 -noappend >/dev/null || return 1
|
|
chmod 444 "$2"
|
|
}
|
|
|
|
# look into cmdline and echo $1 back if $1 is set
|
|
# $1 = value name, case sensitive, for example 'debug'
|
|
#
|
|
cmdline_parameter()
|
|
{
|
|
. /etc/ublinux/config 2>/dev/null || . etc/ublinux/config 2>/dev/null
|
|
echo -n " $CMDLINE " | cat /proc/cmdline - 2>/dev/null | tr "[:cntrl:]" " " | egrep -m1 -o "(^|[[:space:]])$1([[:space:]]|\$)" | head -1 | tr -d " "
|
|
}
|
|
|
|
# look into cmdline and echo value of $1 option
|
|
# $1 = value name, case sensitive, for example 'changes'
|
|
#
|
|
cmdline_value()
|
|
{
|
|
. /etc/ublinux/config 2>/dev/null || . etc/ublinux/config 2>/dev/null
|
|
echo -n " $CMDLINE " | cat /proc/cmdline - 2>/dev/null | tr "[:cntrl:]" " " | egrep -m1 -o "(^|[[:space:]])$1=[^[:space:]]+" | head -1 | cut -d "=" -f 2-
|
|
}
|
|
|
|
# Find and run all scripts from the given module
|
|
# This function is used by the activate and deactivate script when the distro
|
|
# is already started, not during live setup
|
|
# $1 = mounted module full path
|
|
# $2..$n = optional arguments for the scripts, eg. 'start'
|
|
#
|
|
find_n_run_scripts()
|
|
{
|
|
debug_log "find_n_run_scripts" "$*"
|
|
local MOD
|
|
|
|
MOD="$1"
|
|
shift
|
|
|
|
RCPATH=/etc/rc.d/init.d
|
|
[ -d $RCPATH ] || RCPATH=/etc/init.d
|
|
RUNLEVEL=$(runlevel | awk '{print $2}')
|
|
[ -d "/etc/rc$RUNLEVEL.d" ] && RCPATH=/etc/rc$RUNLEVEL.d
|
|
[ -d "/etc/rc.d/rc$RUNLEVEL.d" ] && RCPATH=/etc/rc.d/rc$RUNLEVEL.d
|
|
RUNSCRIPTS="$MOD$RCPATH|$MOD/usr/lib/ublinux/rc.local|$MOD/usr/lib/ublinux/rc.post"
|
|
echo $@ | grep -q start || RUNSCRIPTS="$MOD$RCPATH"
|
|
|
|
find "$MOD" | egrep "$RUNSCRIPTS" | cut -b "${#MOD}"- | cut -b 2- | xargs -n 1 -r readlink -f | sort -u | \
|
|
while read SCRIPT; do
|
|
if [ "$SCRIPT" != "" -a -x "$SCRIPT" -a ! -d "$SCRIPT" ]; then
|
|
# call the script by real path, not from the module
|
|
log "starting '"$SCRIPT" $@'"
|
|
"${SCRIPT}" "$@"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# test if the script is started by root user. If not, exit
|
|
allow_only_root()
|
|
{
|
|
if [ "0$UID" -ne 0 ]; then
|
|
echo "Only root can run $(basename $0)"; exit 1
|
|
fi
|
|
}
|
|
|
|
|
|
#####################
|
|
# Hotkeys functions #
|
|
#####################
|
|
|
|
show_run()
|
|
{
|
|
DE=$(detectDE)
|
|
if [ "$DE" = "kde" -o "$DE" = "plasma" ] ; then
|
|
krunner
|
|
elif [ "$DE" = "gnome" ] ; then
|
|
gnome-panel-control --run-dialog
|
|
elif [ "$DE" = "lxqt" ] ; then
|
|
lxqt-runner
|
|
else
|
|
rofi -config /usr/share/ublinux/i3/rofi.cfg -show
|
|
fi
|
|
}
|
|
|
|
lock_session()
|
|
{
|
|
DE=$(detectDE)
|
|
# qdbus org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.Lock
|
|
xterm -geometry 0x0+1+1 -e "dbus-send --dest=org.freedesktop.ScreenSaver --print-reply /ScreenSaver org.freedesktop.ScreenSaver.Lock"
|
|
if [ "$DE" != "kde" -a "$DE" != "plasma" ] ; then
|
|
ps -U $(id -u) | grep -q xscreensaver || xscreensaver -no-splash &
|
|
sleep 0.5s
|
|
xscreensaver-command -lock
|
|
fi
|
|
}
|
|
|
|
xss_slideshow()
|
|
{
|
|
chbg -xscreensaver -randomize -R -effect 1 -interval 0.2 -mode smart -max_size 100 -R /usr/share/ublinux/screensaver/Default >/dev/null 2>&1
|
|
}
|
|
|
|
xss_heartbeat()
|
|
{
|
|
. /usr/lib/ublinux/os-config
|
|
. /etc/ublinux/config 2>/dev/null
|
|
SSAVERBLOCKAPPS="$(echo "$SSAVERBLOCKAPPS"| tr ',; ' '|' )"
|
|
bash -c "while true ;do top -bn1 -u $(id -un) | awk '{ print \$7 FS \$NF }' | grep ^[1-9] | egrep -q \"$SSAVERBLOCKAPPS\" && xscreensaver-command -deactivate >/dev/null ; sleep 20s ; done " &
|
|
}
|
|
|
|
|
|
show_hotkeys()
|
|
{
|
|
MSG1=$(gettext -s "UBLinux magic keys:")
|
|
echo "$MSG1" > /tmp/listkeys
|
|
echo " " >> /tmp/listkeys
|
|
cat $HOME/.xbindkeysrc | sed -e 's/^".*"//' -e 's/Mod4/WIN/' -e '/^#.*#/ d' -e '/^ *$/ d' -e 's/^# *//' | while read a ; do
|
|
gettext -s "$a" >> /tmp/listkeys
|
|
done
|
|
mdialog --textbox /tmp/listkeys 600 600
|
|
rm -f /tmp/listkeys
|
|
}
|
|
|
|
show_info()
|
|
{
|
|
. /etc/os-release
|
|
LIVECDNAME="$NAME"
|
|
UPTIME=$(uptime | awk '{print "time - "$1", up - "$3}')
|
|
RAM=$(free -m | grep Mem | awk '{ print "total - "$2", free - "$4}')
|
|
SWAP=$(free -m | grep Swap | awk '{ print "total - "$2", free - "$4}')
|
|
CPU="$(cat /proc/cpuinfo | sed -e '/model name/!d' -e 's/^.*://')"
|
|
CPUARCH=$(uname -p)
|
|
KERNEL=$(uname -r)
|
|
VIDEO=$(lspci | sed -e '/VGA/!d' -e 's/^.*://')
|
|
GLXINFO=$(glxinfo | sed '2,3!d')
|
|
AUDIO=$(lspci | sed -e '/Audio/!d' -e 's/^.*://')
|
|
CMDLINE=$(cat /proc/cmdline)
|
|
VERSION=$(cat /etc/ublinux-release)
|
|
if [ $(cmdline_parameter unionfs) ] ;then
|
|
PROF_SIZE=$(df -h / |grep unionfs | awk '{print " ["$5"] total - "$2", free - "$4}')
|
|
else
|
|
PROF_SIZE=$(df -h / |grep aufs | awk '{print " ["$5"] total - "$2", free - "$4}')
|
|
fi
|
|
echo "$LIVECDNAME ($VERSION)" > ~/info.txt
|
|
echo "UPTIME: $UPTIME" >> ~/info.txt
|
|
echo "KERNEL: $KERNEL" >> ~/info.txt
|
|
echo "RAM: $RAM" >> ~/info.txt
|
|
echo "SWAP: $SWAP" >> ~/info.txt
|
|
[ "$(cat /proc/cmdline | grep changes= )" ] && echo "PROFILE: $PROF_SIZE" >> ~/info.txt
|
|
echo -e "CPU: ($CPUARCH) \n$CPU" >> ~/info.txt
|
|
echo "VIDEO: $VIDEO" >> ~/info.txt
|
|
echo "$GLXINFO" >> ~/info.txt
|
|
echo "AUDIO: $AUDIO" >> ~/info.txt
|
|
echo "CMDLINE: $CMDLINE" >> ~/info.txt
|
|
echo "MODULES:" >> ~/info.txt
|
|
grep squashfs /proc/mounts | awk '{print $2}' | sort >> ~/info.txt
|
|
mdialog --textbox $HOME/info.txt
|
|
rm -f $HOME/info.txt
|
|
}
|
|
|
|
touchpad()
|
|
{
|
|
MSG2=$(gettext -s "Touchpad disabled, WIN+t to enable again")
|
|
if [ $(synclient -l | grep TouchpadOff | awk '{ print $3 }') -eq 0 ] ;then
|
|
synclient TouchpadOff=1
|
|
mdialog --passivepopup "$MSG2"
|
|
else
|
|
synclient TouchpadOff=0
|
|
fi
|
|
}
|
|
|
|
rfswitch()
|
|
{
|
|
MSG3=$(gettext -s "bluetooth, WI-FI interfaces disabled, WIN+w to enable again")
|
|
rfkill list | grep yes
|
|
if [ $? -eq 0 ] ;then
|
|
rfkill unblock all
|
|
else
|
|
rfkill block all
|
|
mdialog --passivepopup "$MSG3"
|
|
fi
|
|
}
|
|
|
|
recordvideo()
|
|
{
|
|
MSG1=$(gettext -s "Recording are stoped, please wait for encoding")
|
|
MSG2=$(gettext -s "Video are encoded and placed to your home dir")
|
|
RMDOPT=
|
|
. /etc/ublinux/config 2>/dev/null
|
|
ps -U $UID | grep -q pulseaudio && RMDOPT="$RMDOPT --device pulse"
|
|
PID=$(ps -U $UID -o pid,comm | grep recordmydesktop | awk '{print $1}')
|
|
if [ -z "$PID" ] ;then
|
|
recordmydesktop $RMDOPT &
|
|
else
|
|
kill "$PID"
|
|
mdialog --passivepopup "$MSG1"
|
|
bash -c "while true ;do ps -A -o pid | grep -q ^$PID$ || break ; sleep 1s ;done ; mdialog --passivepopup \"$MSG2\""
|
|
fi
|
|
}
|
|
|
|
show_network()
|
|
{
|
|
echo "netstat --inet" > ~/network.txt
|
|
netstat --inet >> ~/network.txt
|
|
echo -e "\nlsof -i" >> ~/network.txt
|
|
/usr/sbin/lsof -i >> ~/network.txt
|
|
mdialog --textbox $HOME/network.txt 600 250
|
|
rm -f $HOME/info.txt
|
|
}
|
|
|
|
google_search()
|
|
{
|
|
xclip -o | sed -r '2~1d;s/(^\s+|\s+$)//g;s/%/%25/g;s/#/%23/g;s/\$/%24/g;s/&/%26/g;s/\+/%2B/;s/,/%2C/g;s/:/%3A/g;s/;/%3B/g;s/=/%3D/g;s/\?/%3F/g;s/@/%40/g;s/\s/+/g' | awk '{print "http://www.google.ru/search?hl=ru&q=" $1}' | xargs firefox -new-tab
|
|
}
|
|
|
|
translate_en_rus()
|
|
{
|
|
[ "$1" == "passive" ] && mdialog --passivepopup "$(wget -U "Mozilla/5.0" -qO - "http://translate.google.com/translate_a/t?client=t&text=$(xclip -o | sed "s/[\"'<>]//g")&sl=auto&tl=ru" | sed 's/\[\[\[\"//' | cut -d \" -f 1)"
|
|
[ "$1" == "msgbox" ] && mdialog --msgbox "$(wget -U "Mozilla/5.0" -qO - "http://translate.google.com/translate_a/t?client=t&text=$(xclip -o | sed "s/[\"'<>]//g")&sl=auto&tl=ru" | sed 's/\[\[\[\"//' | cut -d \" -f 1)"
|
|
[ "$1" == "firefox" ] && xclip -o | sed -r '2~1d;s/(^\s+|\s+$)//g;s/%/%25/g;s/#/%23/g;s/\$/%24/g;s/&/%26/g;s/\+/%2B/;s/,/%2C/g;s/:/%3A/g;s/;/%3B/g;s/=/%3D/g;s/\?/%3F/g;s/@/%40/g;s/\s/+/g' | awk '{print "translate.google.com/translate_t?hl=en#en|ru|" $1}' | xargs firefox -new-tab
|
|
}
|
|
|
|
translate_rus_en()
|
|
{
|
|
[ "$1" == "passive" ] && mdialog --passivepopup "$(wget -U "Mozilla/5.0" -qO - "http://translate.google.com/translate_a/t?client=t&text=$(xclip -o | sed "s/[\"'<>]//g")&sl=auto&tl=en" | sed 's/\[\[\[\"//' | cut -d \" -f 1)"
|
|
[ "$1" == "msgbox" ] && mdialog --msgbox "$(wget -U "Mozilla/5.0" -qO - "http://translate.google.com/translate_a/t?client=t&text=$(xclip -o | sed "s/[\"'<>]//g")&sl=auto&tl=en" | sed 's/\[\[\[\"//' | cut -d \" -f 1)"
|
|
[ "$1" == "firefox" ] && xclip -o | sed -r '2~1d;s/(^\s+|\s+$)//g;s/%/%25/g;s/#/%23/g;s/\$/%24/g;s/&/%26/g;s/\+/%2B/;s/,/%2C/g;s/:/%3A/g;s/;/%3B/g;s/=/%3D/g;s/\?/%3F/g;s/@/%40/g;s/\s/+/g' | awk '{print "translate.google.com/translate_t?hl=ru#ru|en|" $1}' | xargs firefox -new-tab
|
|
}
|
|
|
|
open_url()
|
|
{
|
|
xclip -o | sed -n 1p | xargs firefox -new-tab
|
|
}
|
|
|
|
userkeys()
|
|
{
|
|
string=$(head -n $1 $HOME/.userkeys | tail -n 1)
|
|
TMPFILE=$HOME/tmp/userkey-$(id -un)
|
|
> $TMPFILE
|
|
echo "#!/bin/bash" > $TMPFILE
|
|
echo "$string" >> $TMPFILE
|
|
chmod +x $TMPFILE
|
|
$TMPFILE
|
|
rm -f $TMPFILE
|
|
}
|
|
|
|
screen_scale()
|
|
{
|
|
scale_[1]=1x1
|
|
scale_[2]=1x1.2
|
|
scale_[3]=1.2x1.2
|
|
scale_[4]=1.2x1.5
|
|
scale_[5]=1.5x1.5
|
|
scale=2
|
|
[ -f /tmp/scale ] && scale=$(cat /tmp/scale)
|
|
xrandr --output LVDS1 --scale ${scale_[$scale]}
|
|
if [ $scale == 5 ] ; then
|
|
echo 1 > /tmp/scale
|
|
else
|
|
echo $(expr $scale + 1) > /tmp/scale
|
|
fi
|
|
}
|
|
|
|
[ "$(basename $0)" = "functions" ] && $@ || true
|