#!/bin/bash

echo "This is the preparation script for new snapshots."
echo "It will delete some files, logs, shutdown server, clean sd card for better compression"
echo "rename computer and set wlan to AP and shutdown linux. Then you can make a image"
echo "from the sd card for release."
echo "Are you sure you want this? [enter y]:"
read sure
echo "Read $sure"
if [ "$sure" != "y" ]; then
  echo "Aborting preparation."
  exit 1
fi;
echo "Preparing for snapshot ..."
service RepetierServer stop
apt-get clean
rm /var/lib/Repetier-Server/logs/*
rm /var/lib/Repetier-Server/database/key*
rm /var/lib/Repetier-Server/database/user*
rm /var/lib/Repetier-Server/database/work*
rm /var/lib/Repetier-Server/database/repetier-network*
rm -r /tmp/*
rm -r /home/pi/.cache/*
rm /home/pi/Repetier-Server*
rm /root/.bash_history
rm /home/pi/.bash_history
rm -r /home/pi/.ssh
rm -r /home/pi/.cache/chromium
rm -r /var/log/daemon*
rm -r /var/log/user*
rm -r /var/log/messages*
rm -r /var/log/auth*
rm -r /var/log/debug*
rm -r /var/log/altern*
rm -r /var/log/syslog*
rm -r /usr/local/Repetier-Setup/etc/webcam_ports
rm /etc/NetworkManager/system-connections/*
touch /usr/local/Repetier-Setup/etc/needExpandFS
cp ../etc/server.conf.orig ../etc/server.conf
# Remove special country settings
iw reg set 00

echo "Delete printer configurations [n=skip]"
read sure
if [ "$sure" == "y" ]; then
  echo "Deleting printer configurations and special customizations ...."
#  rm /var/lib/Repetier-Server/configs/*
#  rm -r /var/lib/Repetier-Server/printer/*
  rm /var/lib/Repetier-Server/database/repetier-network*.xml
fi;

echo "Skip image cleanup? [y=skip]"
read sure
if [ "$sure" == "y" ]; then
  echo "Skipping cleanup...."
  shutdown -h now
  exit 1
fi;


echo "Optimizing sd card, can take a while ..."
dd bs=4M if=/dev/zero of=/root/junk
sync
rm /root/junk
echo "Shutdown ..."
shutdown -h now

