#!/bin/sh
#170811 sfs 64
l=ru
m="Удаление doc, gtk-doc, info, licenses, man и пр. 
в указанном каталоге,
отделение dev составляющей (нужна только разработчикам)
Домашние директории изменены не будут."

D(){
y="`yad --title "trim" --window-icon gtk-cut --separator= \
    --image-on-top --image="gtk-cut" \
    --center --text "$m" --form --field="Укажите каталог":DIR`"
[ "$y" ] || exit
}

HLP(){
echo "Usage $0 [-g] dir
$m
-g - GUI" && exit
}

[ "$1" = "--help" ] && HLP
 
if [ "$1" = "-g" ] ;then
[ "$2" ] && y="$2" || D
g=1 && shift && o1="`cd "$y" && du -sh`" && ntf -q "trim - оптимизация размера </b><small>$y</small><b>" "
$m

Текущий размер: <b>$o1</b>

<b>Оптимизировать?</b>"
[ "$?" = "1" ] && exit
r="$y"
else [ ! "$1" ] && HLP && exit || r="$1"
fi

[ "$r" = "`basename "$r"`" ] && r="`pwd`/$r"
#echo $r ;exit
rd=$r\_DEV

X() {
#echo del:"$x"
#[ -e "$1" ] && rm -R ${1}
[ "$1" ] && rm -R ${1}
}

X $r/etc/udev/hwdb.bin
X "`find $r/usr/share/terminfo/* -maxdepth 0 -type d -name "*" -print |egrep -v 'l$|r$|v$|x$'`" 
X "`find $r/usr/share/kbd/locale/* -maxdepth 0 -type d -name "*" -print |egrep -v ''$l'$'`" 
X "`find $r/usr/share/kbd/consolefonts   -type f -name "*" -print |egrep -v  'Cyr_a8x|LatArCyrHeb|cyr-sun|sun12x'`" 
X "`find $r/usr/share/i18n/charmaps  -type f -name "*" -print |egrep -v '/CP1250|/CP1251|/CP1252|ISO-8859-1.gz|KOI-8|KOI8-|UTF-8.gz'`" 
X "`find $r/usr/share/X11/locale/* -maxdepth 0 -type d -name "*" -print |egrep -v '\/locale\/C$|en_US.UTF-8|ru_RU.UTF-8'`" 
#rm -R `find  $r/usr/share/X11/locale/* -maxdepth 0  -type d -name "*" -print |egrep -v '\/locale\/C|koi8-r|ru_|en_|cp1251|iso8859-1$'`
X "`find $r/usr/lib/gconv/ -type f -name "*" -print |egrep -v 'CP1251.so|CP1252.so|IBM866.so|ISO8859-1.so|ISO8859-5.so|KOI|UNICODE.so|UTF-|gconv-modules$'`" 
X "`ls -1 $r/usr/share/gnupg/help.*.txt |grep -v $l`"
X "$r/usr/share/zoneinfo"
X "$r/usr/share/mime"
X "$r/usr/share/xxkb"
X "$r/usr/share/fonts"
X "$r/usr/share/sounds/alsa"
X "`ls -1 $r/usr/lib/libgfortran.so*`"

#dconf
X "$r/pra/mk/-/dconf-editor"
X "$r/usr/share/icons/HighContrast"
X "$r/usr/share/icons/Adwaita"

[ -d $r/usr/share/i18n/localescd ] && $r/usr/share/i18n/locales && rm `ls |egrep -v 'POSIX|en_US|i18n|ru_RU'` 

#mkdir -p "$rd"
#mkdir -p $rd/usr/lib/pkgconfig
M() {
if [ "$x" ] ;then
    mkdir -p "$rd$x2" && mv "$x" "$rd$x2"
fi
}

x2=/usr/bin 
x=`find $r$x2 -type f -name "*" -print |egrep '\-config$'` ; M

x2=/usr/lib/gawk
x=`find $r$x2 -type f -name "*" -print |egrep '.la$'`; M

mvdir(){ 
if [ -d "$r$1" ] ;then
    d2="$rd`dirname $1`" ; [ -d "$d2" ] || mkdir -p "$d2"
    mv "$r$1" "$d2"
fi
}

mvdir /usr/lib/glib-2.0/include 
mvdir /usr/lib/glib-2.0/libffi-3.0.13
mvdir /usr/lib/gtk-2.0/include
mvdir /usr/include
mvdir /usr/lib/pkgconfig
mvdir /usr/lib/gdbus-2.0
mvdir /usr/lib/girepository-1.0
mvdir /usr/lib/dbus-1.0/include
mvdir /usr/lib/perl5
mvdir /usr/bin/core_perl
mvdir /usr/lib/gcc
mvdir /usr/share/perl5
mvdir /usr/share/pkgconfig
mvdir /usr/share/aclocal
mvdir /usr/share/automake-1.15
mvdir /usr/share/autoconf
mvdir /usr/lib/cmake
mvdir /usr/share/cmake
mvdir /usr/share/vala
mvdir /usr/share/common-lisp
mvdir /usr/share/pygobject
mvdir /usr/share/pygtk
#mvdir /usr/share/glib-2.0/codegen
mvdir /usr/share/readline
mvdir /usr/share/texinfo
mvdir /usr/share/texinfo
mvdir /var/lib/pacman/local
mvdir /usr/share/gir-1.0

if [ "`ls -1 $r/usr/lib/ |egrep '\.o|a|la$'`" ] ;then
mkdir -p $rd/usr/lib
mv $r/usr/lib/*.o $rd/usr/lib/
mv $r/usr/lib/*.a $rd/usr/lib/
mv $r/usr/lib/*.la $rd/usr/lib/
fi

mv $r/usr/lib/libpython2* . $rd/usr/lib/

if [ "`find $r/usr/lib/python*`" ] ;then
    [ -d "$rd/usr/lib/" ] || mkdir -p $rd/usr/lib/
    mv $r/usr/lib/python* $rd/usr/lib/
fi


X "`ls -d $r/usr/share/locale/* |egrep -v '/'$l'$'`" 
X $r/usr/share/gtk-2.0/demo 
#x="`ls -d $r/usr/share/locale/* |egrep -v ''$l'$'`" ; [ "$x" ] && rm -R "$x"
#x="`find $r/usr/share/locale/* -maxdepth 0 -type d -name "*" -printf " %p" |egrep -v '/'$l'$'`" ; [ "$x" ] && rm -R $x
#\( -name "firefox*.s*fs" -o  -name "chrome*.s*fs" -o  -name "opera*.s*fs" \)
#find $r/usr/share/locale/* -maxdepth 0 -type d -name "*" 

X "$r/usr/local/share/applications/mimeinfo.cache" 
#rm $r/usr/share/fonts/misc/*
X "$r/usr/bin/gtk-demo" 
X "$r/etc/udev/hwdb.bin" 

if [ -f $r/etc/pacman.conf ];then
#rm $r/usr/lib/locale/locale-archive 
    x="`ls -1 $r/usr/share/gnupg/help.*.txt |grep -v ".$l."`" ;X
    printf "" > /var/log/pacman.log
    rm $r/.arch-chroot
#x="`ls -1d $r/usr/share/zoneinfo/* |egrep -v 'Asia|Etc|Europe|zone.tab'`" ; [ "$x" ] && rm -R $x
fi

###optim Pro
X "`find $r/usr/share/kbd/keymaps/* -maxdepth 0 -type d -name "*" -print |egrep -v '/i386|/include'`"
X "`find $r/usr/share/kbd/keymaps/i386/* -maxdepth 0 -type d -name "*" -print |egrep -v '/qwerty|/include'`"
X "`find $r/usr/share/kbd/locale/* -maxdepth 0 -type d -name "*" -print |egrep -v '/'$l''`"
X "`find $r/usr/share/ROX-Filer/Messages/* -maxdepth 0 -type d -name "*" -print |egrep -v '/'$l''`"

#rm -R $r/usr/share/zoneinfo $r/usr/share/terminfo
#x=`find $r/usr/bin -type f -name "*-config" -print` #|egrep -v  'Cyr_a8x|LatArCyrHeb|cyr-sun|sun12x'`

X "`find $rd/var/lib/pacman/local  -type f \( -name "mtree" \)`"
#X "`find $r/usr/share/pixmaps/*.svg  -print`" 
X "`find $r/usr/share/icons/hicolor/256x256/* -maxdepth 0 -type d -name "*" -print`" 
X "`find $r/usr/share/icons/hicolor/128x128/* -maxdepth 0 -type d -name "*" -print`" 
X "`find $r/usr/share/icons/hicolor/96x96/* -maxdepth 0 -type d -name "*" -print`" 
#X "`find $r/usr/share/icons/hicolor/scalable/* -maxdepth 0 -type d -name "*" -print`" 
X "/usr/share/icons/hicolor/icon-theme.cache"
#X "$r/usr/share/icons/hicolor/index.theme"
X "$r/usr/share/themes/bubble" 

X "`find $r/usr/share/mc/help/ $r/usr/share/mc/hints/  -type f |grep -v .$l`" 

[ "`find $rd -type f`" ] || rm -r "$rd" #del empty
#find "$r" "$rd" -type d -empty -delete
find "$rd" -type d -empty -delete
for d in /usr/share/help /usr/share/doc /usr/share/gtk-doc /usr/share/info /usr/share/licenses /usr/share/man \
    /var/cache/fontconfig /var/cache/ldconfig /var/lib/pacman/sync \
    /var/cache/pacman/pkg ;do
	[ -d "$r$d" ] &&  X "$r$d" 
        [ -f "$r/usr/bin/init" ] && mkdir -p "$r$d" #only in 001-base
done

mim=/usr/share/mime/packages/freedesktop.org.xml ; cp $mim "$mim"1
awk '{ if ($0~/<comment xml:lang="/ && $0!~/<comment xml:lang="ru">/) print ""; else print $0}' "$mim"1 >$mim && rm "$mim"1

[ "$g" ] && o2="`cd "$1" && du -sh`" && ntf -w "trim - оптимизация размера </b><small>$1</small><b> завершена" "

Исходный размер: <b>$o1</b>
Оптимизированный размер: <b>$o2</b>"
