#!/bin/bash
# Cleans up 001-core files before publishing
# Run it from inside the 001-core folder

pver=porteus-rc2

# Make sure no kwrite backup files are still around
find . -name "*~"|xargs rm

# Set permissions that may have gone astray

chmod u+s usr/bin
chmod -R 4777 tmp
chmod u=rws,g=x,o=x usr/bin/chage
chmod u=rws,g=x,o=x usr/bin/chfn
chmod u=rws,g=x,o=x usr/bin/chsh
chmod u=rws,g=x,o=x usr/bin/crontab
chmod u=rws,g=x,o=x usr/bin/expiry
chmod u=rws,g=x,o=x usr/bin/gpasswd
chmod u=rws,g=x,o=x usr/bin/newgrp
chmod u=rws,g=x,o=x usr/bin/passwd
chmod 4755 bin/su
chmod u=rws,g=rx,o=rx usr/bin/fusermount
chmod u=rws,g=x,o=x usr/bin/ping 001/usr/bin/ping6
chmod u=rws,g=x,o=x usr/sbin/faillog
chmod u=rws,g=x,o=x usr/sbin/lastlog
chmod -x etc/rc.d/rc.sshd
chmod -x etc/rc.d/rc.pcmcia
#chmod +x etc/rc.d/rc.bluetooth >/dev/null 2>&1
# Set correct permissions
chown -R 1000:1000 home/guest
chown -R root:root root
chmod 4777 tmp/.ICE-unix

# Make synaptic touchpad scroll by default
psm=`grep "proto=auto" etc/modprobe.d/psmouse.conf`
if [ -z "$psm" ]; then
sed -i etc/modprobe.d/psmouse.conf -e 's/#options/options/g'
sed -i etc/modprobe.d/psmouse.conf -e s/proto=imps/proto=auto/g
fi
#sed -i etc/shadow -e 's@root::9804:0:::::@root:$1$MKy0eqPM$auerQwMpGYcqgBqVddkfO/:9804:0:::::@g'

# Stop annoying mail check by default
ml=`grep MAIL_CHECK_ENAB etc/login.defs|awk '{print$NF}'`
if [ "$ml" == "yes" ]; then
sed -i etc/login.defs -e 's@MAIL_CHECK_ENAB\t*yes@MAIL_CHECK_ENAB\t*no@g'
sed -i etc/login.defs -e s/*//g
fi

# add /opt/porteus-scripts to default path
sed -i etc/profile -e 's@games@games:/opt/porteus-scripts@g'

rm bash
rm etc/motd
rm boot/*
rm etc/ld.so.cache
rm -r etc/init.d
rm -r mnt/*
rm -r media/*
#rm -r usr/doc/*   # don't delete licences in /usr/doc
rm -r usr/share/doc
rm -r usr/share/kbd/keymaps/amiga usr/share/kbd/keymaps/atari usr/share/kbd/keymaps/mac usr/share/kbd/keymaps/ppc usr/share/kbd/keymaps/sun
rm -r usr/src/*
rm usr/info/*
rm usr/man/man1/perl*
rm usr/man/man2/*
rm usr/man/man3/*
rm usr/man/man4/*
rm usr/man/man6/*
rm usr/man/man9/*
rm usr/man/mann/*
rm -r usr/share/lynx
rm -r usr/share/sounds
rm -r var/log/setup
rm var/spool/mail/*
rm usr/bin/todos

echo "Changing permissions..."
find ./ -type d | xargs chmod -v 755 >/dev/null 2>&1
echo "Extracting All manpages..."
find . | grep .gz | xargs gunzip >/dev/null 2>&1
echo "stripping Unneeded Symbols..."
find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded >/dev/null 2>&1
chmod 777 tmp/.*


echo "clean a.out, include and locale in /mnt/sda7/porteus-master/001/changes ? y/n"
read ans
if [ $ans = "y" ]; then
    rm -r /mnt/sda7/porteus32/$pver/export/a.out/* /mnt/sda7/porteus32/$pver/export/devel/* /mnt/sda7/porteus32/$pver/export/togo/locale/* /mnt/sda7/porteus32/$pver/export/togo/man/*
fi

echo "moving a.out, include and locale"
pack=`find . -name "*.a"`
target=/mnt/sda7/porteus32/porteus-v1/export/a.out
[ ! -d $target ] && mkdir $target
for a in $pack; do
    cp -d --parents $a $target
    rm $a
done

headers=/mnt/sda7/porteus32/$pver/export/devel
locales=/mnt/sda7/porteus32/$pver/export/togo
cp -r usr/include $headers && rm -r usr/include/*
cp -r usr/share/locale $locales && rm -r usr/share/locale/*

# Decompress all man files - xzm will do a better job of compression
cd usr/man
for man in `ls | grep man`; do
    cd $man
    for x in `find . -type f -name '*.bz2'`; do bunzip2 $x; done
    for x in `find . -type l`; do
        if [ ! -e $x ]; then
        name=`echo $x | sed 's/.bz2//' | sed 's@./@@'`
        dest=`file $x | cut -d " " -f 6 | sed 's/.\(.*\)./\1/' | sed 's@/usr/share/man/@@' | sed 's/.bz2//'`
        ln -sf $dest $name
        rm $x
        fi
    done
    cd ..
done
cd ../../

# move man files for export to LST
[ -d /mnt/sda7/porteus32/$pver/export/togo/man ] || mkdir -p /mnt/sda7/porteus32/$pver/export/togo/man
for x in `ls usr/man | egrep -v "man|cat"`; do
    cp -r usr/man/$x /mnt/sda7/porteus32/$pver/export/togo/man
    rm -rf usr/man/$x
done

# Create symlinks for opt/porteus-scripts in /usr/bin
for x in `find opt/porteus-scripts/ -type f -executable -maxdepth 1`; do
    name=`echo $x | rev | cut -d/ -f1 | rev`
    ln -sf /opt/porteus-scripts/$name usr/bin/$name
done

echo "001 has been processed! All done."
echo
exit
