Use functions for everything

Original message: https://t.me/TelegramDesktopTalk/95813
This commit is contained in:
Michael De Roover 2023-02-10 10:21:00 +01:00
parent b04d23dfcd
commit ff62441bc8
Signed by: vim
GPG Key ID: 075496E232CE04CB

View File

@ -1,31 +1,71 @@
#!/bin/sh #!/bin/sh
permerr(){ # Check whether script is executed by root
f_perm_err(){
printf "This script has been executed as the root user.\n" printf "This script has been executed as the root user.\n"
printf "Please run it as a regular user instead.\n" printf "Please run it as a regular user instead.\n"
exit 1 exit 1
} }
[ $(id -u) == "0" ] && permerr f_mount_storage(){
printf "Mounting $1 storage...\n"
# Environment variables [ ! -d $2 ] && mkdir $2
user_id=$(id -u) sudo mount -t tmpfs -o size=256M,mode=1777 tg-$1 $2
user_name=$(whoami)
tmp="/tmp/tg-setup"
data="/home/${user_name}/.local/share/TelegramDesktop"
# Establish the tmpfs for Telegram
mkdir $tmp
cp -rv $data/* $tmp
sudo mount -t tmpfs -o size=256M,mode=1777 telegram $data
cp -rv $tmp/* $data/
telegram
read -p "Do you want to remove the cached application data? (y/n) " remove_app_data
f_remove_app_data(){
sudo umount telegram
rm -r $tmp
} }
[ "$remove_app_data" != "n" ] && f_remove_app_data f_remove_cache(){
printf "Removing unnecessary data from $1 storage...\n"
rm -r "$2/tdata/user_data/cache/"* 2>/dev/null
rm -r "$2/tdata/user_data/media_cache/"* 2>/dev/null
rm -r "$2/tdata/user_data#?/cache/"* 2>/dev/null
rm -r "$2/tdata/user_data#?/media_cache/"* 2>/dev/null
rm -r "$2/tdata/emoji/cache"* 2>/dev/null
rm -r "$2/DebugLogs/"* 2>/dev/null
}
f_copy_data(){
printf "Copying data from $1 to $2 storage...\n"
cp -r $3/* $4
}
f_rsync_data(){
printf "Copying data from $1 to $2 storage...\n"
rsync -a --delete $3/ $4
}
f_umount_storage(){
printf "Removing $1 storage...\n"
sudo umount tg-$1
[ ! -z $2 ] && \
printf "Deleting directory...\n" && \
rm -r $2
}
[ $(id -u) == "0" ] && f_perm_err
# Environment variables
tmp="/tmp/tg-temporary"
data="/home/$USER/.local/share/TelegramDesktop"
# Reduce permissions for written files
umask 0077
# Establish the tmpfs for Telegram
f_remove_cache internal $data
f_mount_storage transitory $tmp
f_copy_data internal transitory $data $tmp
f_mount_storage temporary $data
f_copy_data transitory temporary $tmp $data
f_umount_storage transitory $tmp
# Launch Telegram application
printf "Launching Telegram...\n"
telegram 2>/dev/null
# Tear down the tmpfs for Telegram
f_remove_cache temporary $data
f_mount_storage transitory $tmp
f_copy_data temporary transitory $data $tmp
f_umount_storage temporary
f_rsync_data transitory internal $tmp $data
f_umount_storage transitory $tmp