#!/bin/bash
# -*- ENCODING: UTF-8 -*-

# reconfig
#
# Restore personal configuration to PicaLibre original.
#
# Copyright (c) 2013-2026: Alexis Puente Montiel   < pica (a) picalibre.org >
#
# Licensed according to GNU AGPL version 3.0.
#
# It is libre/free software; you can use, redistribute and/or modify it according to the terms of GNU AGPL as published by GNU, version 3.0, 19 November 2007.
#
# It is distributed in the hope that it will be useful, but without any warranty. Read GNU AGPL version 3.0 for additional details.
#
# A copy of GNU AGPL version 3.0 is available at /usr/share/doc/<software-package-name>/agpl-3.0.txt (additionally on Internet as text at https://www.gnu.org/licenses/agpl-3.0.txt and as HTML at https://www.gnu.org/licenses/agpl-3.0-standalone.html ).
#
# Note: Additionally to the official e-mails, picalibre.org is strictly the only official site for this software project, please consider using it to download, report bugs and contribute.
#
# Depends: bash, coreutils, findutils, grep, procps, sed


### SCRIPT VARIABLES ########################################

CNAME="reconfig"
VERSION="1.3.1"
TITLE="'reconfig'"

# Translations
if [ "$LANG" = "" ] ; then export $(cat /etc/default/locale | grep -a 'LANG=') ; fi
TEXTDOMAIN=pica-skel
TEXTDOMAINDIR=/usr/share/locale/

# Write errors to log
ERRORLOG="$HOME/.${CNAME}.log"
if [ -e "$ERRORLOG" ] ; then
	mv -f $ERRORLOG ${ERRORLOG}.ant
fi
if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi

for i in /etc/pica-global.dist /etc/pica-global.orig /etc/pica-global /etc/pica-global.local ~/.pica-global ~/pica-global ; do
	if [ -f "$i" ] ; then
	cat "$i"
	source "$i"
	source <(cat $i | sed -e "s/=\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)/=Y/g" -e "s/=\(No\|no\|n\)/=N/g" -e "s/=\"\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)\"/=Y/g" -e "s/=\"\(No\|no\|n\)\"/=N/g")
	fi
done
if [ "$DEBUG" = "Y" ] ; then
	set -xv
	DEBUG="Y"
else
	ERRORLOG="/tmp/.${CNAME}_$(id -nu).log"
	if [ -e "$ERRORLOG" ] ; then mv -f $ERRORLOG ${ERRORLOG}.ant ; fi
	if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi
fi
if [ "$DEBUG" = "" ] ; then DEBUG="N" ; fi
if [ "$DEBUG" != "N" ] ; then
exec > >(tee -a "$ERRORLOG") 2>&1
echo "$0" "$*" >> "$ERRORLOG"
echo "$CNAME" "$VERSION" >> "$ERRORLOG"
echo $(date +%Y-%m-%d_%H:%M:%S) $"Start" >> "$ERRORLOG"
echo "env:" >> "$ERRORLOG"
env >> "$ERRORLOG"
echo "set:" >> "$ERRORLOG"
set >> "$ERRORLOG"
#else
#exec 2>>"$ERRORLOG"
fi

# Description:
BDESCRIP=$"Restore personal configuration to PicaLibre original."
LDESCRIP=$"$TITLE is a tool to restore desktop and application personal configuration to PicaLibre original."

# Documentation:
docu_info () {
echo "$CNAME ($VERSION) - $BDESCRIP"
echo 
echo $"Usage:" $CNAME [$"OPTIONS"]
echo 
echo $"Options:"
echo -e "$ODESCRIP"
echo 
echo $"'man $CNAME' for more information."
echo 
}
ODESCRIP=" -x""\t"$"Show help documentation."

while getopts x OPTION ; do
	case $OPTION in
		x )   docu_info ; exit 0 ;;
	esac
done


### SCRIPT ########################################

echo $"Do you want to restore desktop and application personal configuration to PicaLibre original?"
echo $"(Desktop and application personal configuration that do not exist in the PicaLibre original will remain unchanged, unless you manually delete /home/your-username/.config before continuing.)"

if [ "$(set | grep -aEi "^XDG_|DESKTOP")" != "" ] || [ "$(env | grep -aEi "^XDG_|DESKTOP")" != "" ] || [ "$(ps aux | grep -aEi "xfce|icewm" | grep -av 'grep ')" != "" ] ; then
echo $"There appears to be a desktop session running. You should close any desktop sessions to avoid an incomplete restore."
fi

echo $"To restore desktop and application personal configuration to PicaLibre original, press the 'S' or 'Y' key followed by the 'Enter/Return' key (usually the largest key on the keyboard after the 'space' key). If you don't want to restore, simply press the 'Enter/Return' key."

read SEL

if [ "$SEL" = "Y" ] || [ "$SEL" = "y" ] || [ "$SEL" = "S" ] || [ "$SEL" = "s" ] ; then

# architecture of the running kernel:

if [ "$(uname -mr | grep -aEi 'amd64|x86_64|x86-64' )" != "" ] ; then SYSARC="amd64" ; fi
if [ "$(uname -mr | grep -aEi 'armel|iop32x|ixp4xx|kirkwood|marvell|mv78xx0|orion5x|rpi|versatile|arm' )" != "" ] ; then SYSARC="armel" ; fi
if [ "$(uname -mr | grep -aEi 'armhf|armmp|mx5|omap|vexpress|armv' )" != "" ] ; then SYSARC="armhf" ; fi
if [ "$(uname -mr | grep -aEi 'arm64|aarch64' )" != "" ] ; then SYSARC="arm64" ; fi
if [ "$(uname -mr | grep -aEi '386|486|586|686|x86_32|x86-32' )" != "" ] ; then SYSARC="i386" ; fi
if [ "$(uname -mr | grep -aEi 'itanium|mckinley|ia64' )" != "" ] ; then SYSARC="ia64" ; fi
if [ "$(uname -mr | grep -aEi 'loong64|loongarch64' )" != "" ] ; then SYSARC="loong64" ; fi
if [ "$(uname -mr | grep -aEi 'bcm91|ip22|ip32|malta|mips|octeon' )" != "" ] ; then SYSARC="mips" ; fi
if [ "$(uname -mr | grep -aEi 'loongson|malta|mips64|octeon' )" != "" ] ; then SYSARC="mips64el" ; fi
if [ "$(uname -mr | grep -aEi 'loongson|bcm91|cobalt|malta|mips32|mipsel|octeon' )" != "" ] ; then SYSARC="mipsel" ; fi
if [ "$(uname -mr | grep -aEi 'powerpc|powerpc64|ppc|ppc64' )" != "" ] ; then SYSARC="powerpc" ; fi
if [ "$(uname -mr | grep -aEi 'powerpc64le|ppc64el|ppc64le' )" != "" ] ; then SYSARC="ppc64el" ; fi
if [ "$(uname -mr | grep -aEi 'riscv64|rv64' )" != "" ] ; then SYSARC="riscv64" ; fi
if [ "$(uname -mr | grep -aEi 's390x' )" != "" ] ; then SYSARC="s390x" ; fi
if [ "$(uname -mr | grep -aEi 'sparc' )" != "" ] ; then SYSARC="sparc" ; fi
if [ "$(uname -mr | grep -aEi 'sparc64' )" != "" ] ; then SYSARC="sparc64" ; fi

#if [ "$(cat /etc/passwd | grep -aE ^$(whoami): | cut -d ':' -f 3 )" -eq 0 ] ; then
if [ "$(id -u)" -eq 0 ] ; then

cp -a --remove-destination /etc/skel/* /etc/skel/.[!.]* /etc/skel/..?* /root/
ls /home/ | grep -aiv 'lost+found' | while read i ; do cp -a --remove-destination /etc/skel/* /etc/skel/.[!.]* /etc/skel/..?* /home/"$i"/ ; done
#ls /home/ | grep -aiv 'lost+found' | while read i ; do cp -a --remove-destination /etc/skel/{,.[!.],..?}* /home/"$i"/ ; done
cp -a --remove-destination /root/.bashrcPICA /root/.bashrc
if [ "$SYSARC" = "amd64" ] || [ "$SYSARC" = "i386" ] || [ "$SYSARC" = "ia64" ] ; then sed -i -e '/presentation-mode/ s|false|true|g' -e '/dpms-enabled/ s|true|false|g' -e '/\("blank-on-ac"\|"dpms-on-ac-sleep"\|"dpms-on-ac-off"\)/ s|"[0-9]\+"|"0"|g' /root/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-power-manager.xml ; fi
ls /home/ | grep -aiv 'lost+found' | while read i ; do cd /home/"$i" ; cp -a --remove-destination .bashrcPICA .bashrc ; cd - ; done
if [ "$SYSARC" = "amd64" ] || [ "$SYSARC" = "i386" ] || [ "$SYSARC" = "ia64" ] ; then ls /home/ | grep -aiv 'lost+found' | while read i ; do cd /home/"$i" ; sed -i -e '/presentation-mode/ s|false|true|g' -e '/dpms-enabled/ s|true|false|g' -e '/\("blank-on-ac"\|"dpms-on-ac-sleep"\|"dpms-on-ac-off"\)/ s|"[0-9]\+"|"0"|g' .config/xfce4/xfconf/xfce-perchannel-xml/xfce4-power-manager.xml ; cd - ; done ; fi
### Owners and permissions
ls /home/ | grep -aiv 'lost+found' | while read i ; do chown -R $(id -nu "$i"):$(id -ng "$i") /home/"$i" ; done
find /home/*/ -type f | while read i ; do chmod 600 "$i" ; done
find /home/*/ -type d | while read i ; do chmod 700 "$i" ; done
chown -R root:root /root
find /root/ -type f | while read i ; do chmod 600 "$i" ; done
find /root/ -type d | while read i ; do chmod 700 "$i" ; done
find /root/ -maxdepth 1 | grep -a bashrc | while read i ; do sed -i '/umask 007/ s|.*|umask 002\nchmod -R o= /root/|g' "$i" ; done

else

cp -a --remove-destination /etc/skel/* /etc/skel/.[!.]* /etc/skel/..?* "$HOME"
#cp -a --remove-destination /etc/skel/{,.[!.],..?}* "$HOME"
cd "$HOME" ; cp -a --remove-destination .bashrcPICA .bashrc
if [ "$SYSARC" = "amd64" ] || [ "$SYSARC" = "i386" ] || [ "$SYSARC" = "ia64" ] ; then sed -i -e '/presentation-mode/ s|false|true|g' -e '/dpms-enabled/ s|true|false|g' -e '/\("blank-on-ac"\|"dpms-on-ac-sleep"\|"dpms-on-ac-off"\)/ s|"[0-9]\+"|"0"|g' .config/xfce4/xfconf/xfce-perchannel-xml/xfce4-power-manager.xml ; fi

fi

fi # End: if [ "$SEL" = "Y" ] || [ "$SEL" = "y" ] || [ "$SEL" = "S" ] || [ "$SEL" = "s" ]

exit 0
