#!/bin/bash
# barium helper scripts
# author: rosalinux.ru: betcher_
if [ -f $(dirname $0)/lib ] ;  then
	. $(dirname $0)/lib
else 
	. $(which lib) || exit 1
fi

unset FORCE
unset RM_BACKUP
unset RM_TOXZM
unset RM_HOMES
unset RM_ALL

HLP() {
	echo "$(basename $0) - Util to clear OS"
	echo "Usage:  $(basename $0) <parameter>"
	echo ""
	echo "$(basename $0) keys:"
	echo "-h | --help - this help"
	echo "-b | --backup - remove backups" 
	echo "-x | --toxzm  - remove persistent saves (toxzm mode)"
	echo "-h | --homes  - remove unused home directories"
	echo "-a | --all    - same as -b -x -h"
	echo "-f | --force  - non-intaractive mode"
	exit
}

ask_warning() {
	[ "$FORCE" ] && return
	echo $1
	read -p "ctrl-c to abort or ENTER to cotinue..."
}

get_MUID() {
	MUID=mac-$(cat /sys/class/net/e*/address 2>/dev/null | head -1 | tr -d :)
	[ "$MUID" = "mac-" ] && MUID=mac-$(cat /sys/class/net/*/address 2>/dev/null | head -1 | tr -d :)
	[ "$MUID" = "mac-" -o "$MUID" = "mac-000000000000" ] && MUID=vga-$(lspci -mm | grep -i vga | md5sum | cut -c 1-12)
	echo "$MUID"
}

rm_backup() {
	ask_warning "Attention! Backup of system catalogue will be removed"
	echo -e 'Remove backup system and saves...\n'
	if grep $(find_syspart) /proc/mounts |grep -wq ro ; then
		NEEDTOREMOUNT=yes
		echo -e "System partition will be temporary remount RW...\n"
		remount rw
	fi
	SYSDIR=$(find_sysdir)
	echo "$SYSDIR" |grep -qE '.*\.bak$' && \
	echo_exit "System was not reboot after last update" ${LINENO}
	[ -d ${SYSDIR}.bak ] && rm -rf "${SYSDIR}.bak"
	[ -d ${SYSDIR}.upd ] && ! lsof |grep -q ${SYSDIR}.upd && rm -rf ${SYSDIR}.upd
	find ${SYSDIR}/modules -type f -name '*.xzm.bak' -o -name '*.xzm.old' -exec rm -f {} +
	[ "$NEEDTOREMOUNT" ] && remount ro
	echo "$(dirname ${SYSDIR}):"
	ls -1 $(dirname ${SYSDIR})
	if [ -f /run/initramfs/shutdown.cfg ] ; then
		. /run/initramfs/shutdown.cfg
		if [ -f "$CHANGESMNT" ] ; then
			find $(dirname "$CHANGESMNT") -type f -name '*.bak' -exec rm -f {} +
			echo -e "\n$(dirname "$CHANGESMNT"):"
			ls -1 $(dirname "$CHANGESMNT")
		fi
	fi
	if [ -d ${SYSMNT}/layer-base/1/modules ] ; then
		find ${SYSMNT}/layer-base/1/modules -type f -name '*.xzm.bak' -o -name '*.xzm.old' -exec rm -f {} +
		echo -e "\n${SYSMNT}/layer-base/1/modules:"
		ls -1 ${SYSMNT}/layer-base/1/modules
	fi
}

rm_toxzm () {
	ask_warning "Attention!  All modules with persistent saves except those currently in use will be removed"
	[ $(echo $uird_mode |cut -f 1 -d ':') != "toxzm"  -o ! -f /run/initramfs/shutdown.cfg ] && \
		echo_exit "toxzm mode disabled" ${LINENO}
	MUID=$(get_MUID)
		[ -n "$MUID" ] || echo_exit "can not determine MUID" ${LINENO}
	. /run/initramfs/shutdown.cfg
	in_cfg=$(cat $CHANGESMNT |grep '^[[:space:]]*XZM.*=' |sed -e 's/.*=//' -e "s/'//g" -e 's/"//g')
	toskip="$(echo $in_cfg |sed 's/[[:space:]]/\|/g')|${MUID}|$(basename $uird_changes)"
	echo "Skip are: $(echo $toskip |sed 's/|/, /g')"
 	find $(dirname $CHANGESMNT) -type f | grep -vE "$toskip" |xargs rm -f 
 	echo -e "\n$(dirname "$CHANGESMNT"):"
 	ls -1 $(dirname "$CHANGESMNT")
}

rm_homes () {
	ask_warning "Attention!  All home dirs except those currently in use will be removed"
	XUSER="$(xuserrun whoami)"
	[ -n $XUSER ] || echo_exit "Can not determine current user of graphical interface"
	USER_HOME=$(grep  "$XUSER" /etc/passwd |cut -f 6 -d ':')
	[ -n $USER_HOME ] || echo_exit "Can not determine home directory of current user of graphical system"
	ls -1 /home |while read a ; do
		[ "/home/$a" != "$USER_HOME" ] && rm -rf /home/$a
	done
	echo -e "\n/home:"
	ls -1 /home
}

while [ -n "$1" ] ; do
	case $1 in
		'-f' | '--force' ) FORCE=yes ;;
		'-b' | '--backup' ) RM_BACKUP=yes ;;
		'-x' | '--toxzm' ) RM_TOXZM=yes ;;
		'-h' | '--homes' ) RM_HOMES=yes ;;
		'-a' | '--all' ) RM_ALL=yes ;;
		*) HLP ;;
	esac
	shift
done

if [ "$RM_ALL" ] ; then
	rm_backup
	rm_toxzm
	rm_homes
else
	[ $RM_BACKUP ] && rm_backup
	[ $RM_TOXZM ] && rm_toxzm
	[ $RM_HOMES ] && rm_homes
fi
