#!/bin/bash
led oxxx
clear
echo "Blink is a tool that allows you to change the rate at which your Xbox's"
echo "LED changes color.  Think of the LED as having 4 colors per cycle like"
echo "1234.  Each number must be assigned a color.  You can have no more than 4"
echo "Colors per cycle"
echo "You can choose from:"
echo -e "\033[1;31mred     (r)"
led rrrr
sleep 2
echo -e "\033[1;33morange  (o)"
led oooo
sleep 2
echo -e "\033[1;32mgreen   (g)"
led gggg
sleep 2
echo -e "\033[1;37moff     (x)\033[0;00m"
led xxxx
sleep 2
led oxxx
echo "Each color lasts for about 1/5 of a second"
echo ""
echo -e "If you wanted to make the led flash \033[1;31mred\033[0;00m, \033[1;33morange\033[0;00m, \033[1;32mgreen\033[0;00m, \033[1;33morange\033[0;00m - you should"
echo -e "type \033[1;31mr\033[1;33mo\033[1;32mg\033[1;33mo\033[0;00m in the following prompt"
echo ""
echo "You can vary the duration of a color by doubling them up, for example:"
echo -e "\033[1;31mrr\033[1;33moo\033[0;00m"
echo -e "This will make the colors cycle\033[1;31m red\033[0;00m,\033[1;33m orange\033[0;00m but each color will stay illuminated"
echo -e "for twice as long.  Another handy sequence is '\033[1;37mxxxx\033[0;00m' as this turns off the LED."
echo ""
echo "You will be asked for the sequence you want displayed at startup and shutdown"
echo "If you cant decide on a sequence yourself, you can type 'random'"
echo -n "Choose your running sequence (eg: rgog): "
read sequence_running
if [ $sequence_running == "random" ]; then
export a="$((RANDOM % 4))"
export b="$((RANDOM % 4))"
export c="$((RANDOM % 4))"
export d="$((RANDOM % 4))"
if [ $a == "0" ]; then
export a="r"
fi
if [ $a == "1" ]; then
export a="o"
fi
if [ $a == "2" ]; then
export a="g"
fi
if [ $a == "3" ]; then
export a="x"
fi

if [ $b == "0" ]; then
export b="r"
fi
if [ $b == "1" ]; then
export b="o"
fi
if [ $b == "2" ]; then
export b="g"
fi
if [ $b == "3" ]; then
export b="x"
fi

if [ $c == "0" ]; then
export c="r"
fi
if [ $c == "1" ]; then
export c="o"
fi
if [ $c == "2" ]; then
export c="g"
fi
if [ $c == "3" ]; then
export c="x"
fi

if [ $d == "0" ]; then
export d="r"
fi
if [ $d == "1" ]; then
export d="o"
fi
if [ $d == "2" ]; then
export d="g"
fi
if [ $d == "3" ]; then
export d="x"
fi
export sequence_running="$a$b$c$d"
fi
echo -n "Choose your shutdown sequence (eg: random): "
read sequence_shutdown
if [ $sequence_shutdown == "random" ]; then
export a="$((RANDOM % 4))"
export b="$((RANDOM % 4))"
export c="$((RANDOM % 4))"
export d="$((RANDOM % 4))"
if [ $a == "0" ]; then
export a="r"
fi
if [ $a == "1" ]; then
export a="o"
fi
if [ $a == "2" ]; then
export a="g"
fi
if [ $a == "3" ]; then
export a="x"
fi

if [ $b == "0" ]; then
export b="r"
fi
if [ $b == "1" ]; then
export b="o"
fi
if [ $b == "2" ]; then
export b="g"
fi
if [ $b == "3" ]; then
export b="x"
fi

if [ $c == "0" ]; then
export c="r"
fi
if [ $c == "1" ]; then
export c="o"
fi
if [ $c == "2" ]; then
export c="g"
fi
if [ $c == "3" ]; then
export c="x"
fi

if [ $d == "0" ]; then
export d="r"
fi
if [ $d == "1" ]; then
export d="o"
fi
if [ $d == "2" ]; then
export d="g"
fi
if [ $d == "3" ]; then
export d="x"
fi
export sequence_shutdown="$a$b$c$d"
fi

clear
echo -e "You have chosen \033[1;31m$sequence_running\033[0;00m for your running sequence, check your LED now"
/bin/led $sequence_running
sleep 5
echo -e "\033[1;34m"
echo -n "5 " && sleep 1 && echo -n "4 " && sleep 1 && echo -n "3 " && sleep 1 && echo -n "2 " && sleep 1 && echo "1 " && sleep 1 
echo -e "\033[0;00m"
/bin/led oooo
sleep 2
echo -e "You have chosen \033[1;31m$sequence_shutdown\033[0;00m for your shutdown sequence, check your LED now"
/bin/led $sequence_shutdown
sleep 5
echo -e "\033[1;34m"
echo -n "5 " && sleep 1 && echo -n "4 " && sleep 1 && echo -n "3 " && sleep 1 && echo -n "2 " && sleep 1 && echo "1 " && sleep 1
echo -e "\033[0;00m"
/bin/led oooo
echo "These MUST BE EXACTLY 4 characters long, they are limited to characters:"
echo -e "\033[1;31mr\033[1;33mo\033[1;32mg\033[1;37mx\033[0;00m"
echo ""
echo "The running sequence will be applied immediately and upon bootup."
echo "The shutdown sequence will be applied when you reboot"
echo "If these settings are not correct, press ctrl+c to cancel"
echo -e "\033[1;34m"
sleep 5
echo -n "10 " && sleep 1 && echo -n "9 " && sleep 1 && echo -n "8 " && sleep 1 && echo -n "7 " && sleep 1 && echo -n "6 " && sleep 1 && echo -n "5 " && sleep 1 && echo -n "4 " && sleep 1 && echo -n "3 " && sleep 1 && echo -n "2 " && sleep 1 && echo -n "1 " && sleep 1 && echo "0 " && echo -e "Continuing..." && sleep 2
echo -e "\033[0;00m"
echo "Configuring init scripts..."
echo "STARTUP=\"${sequence_running}\"" > /etc/conf.d/led
echo "SHUTDOWN=\"${sequence_shutdown}\"" >> /etc/conf.d/led

sleep 2
echo "Running now..."
sleep 2
/bin/led $sequence_running
clear
echo "All done"
echo "You should now see your LED cycling the colors you have chosen."
echo "This script can be rerun at any time."
echo ""
echo "If you get bored, try running 'randblink &' to make your LED constantly"
echo "change colors"
