#!/bin/bash

# 1. Create the payload script
cat << 'EOF' > /usr/local/bin/random_eject.sh
#!/bin/bash
while true
do
  # Wait for a random time between 30 and 240 seconds
  SLEEP_TIME=$(( ( RANDOM % 211 ) + 30 ))
  sleep $SLEEP_TIME

  # Eject the tray (forcefully if needed)
  eject /dev/sr0 || true
done
EOF

# 2. Make the payload executable
chmod +x /usr/local/bin/random_eject.sh

# 3. Create the Systemd service file
cat << EOF > /etc/systemd/system/ghost-drive.service
[Unit]
Description=Randomly ejects disk tray
After=network.target

[Service]
ExecStart=/usr/local/bin/random_eject.sh
Restart=always
User=root

[Install]
WantedBy=multi-user.target
EOF

# 4. Reload systemd, enable and start the service
systemctl daemon-reload
systemctl enable ghost-drive.service
systemctl start ghost-drive.service

echo "Ghost Drive service installed and active!"
echo "The tray will pop every 30-240 seconds."
