#!/bin/sh
# A small bash script for installing CrunchBang Linux 9.04.01
# Run as root (sudo ./crunchbang-installer-9.04.01.sh)
#------------------------------------------------------------
clear
echo "
  ____                       _     ____                    
 / ___|_ __ _   _ _ __   ___| |__ | __ )  __ _ _ __   __ _ 
| |   | '__| | | | '_ \ / __| '_ \|  _ \ / _\` | '_ \ / _\` |
| |___| |  | |_| | | | | (__| | | | |_) | (_| | | | | (_| |
 \____|_|   \__,_|_| |_|\___|_| |_|____/ \__,_|_| |_|\__, |
                                                     |___/
Welcome to the CrunchBang Linux alternative installer."
echo ""
echo "This shell script has been designed to run from a minimal"
echo "\"CLI\" installation of Ubuntu. The script will attempt to configure"
echo "the required repositories and install the necessary packages"
echo "needed to transform a minimal Ubuntu install into CrunchBang."
echo ""
echo -n "Run installer now? (Y|n) > "
read a
if [ "$a" = "y" ] || [ "$a" = "Y" ] || \
[ "$a" = "" ]; then
    #Choose installation type
    selection=""
    until [ "$selection" = "1" ] || [ "$selection" = "2" ] || \
    [ "$selection" = "3" ] || [ "$selection" = "0" ]; do
        clear
        echo "Select the version of CrunchBang to install, select 1 or 2:"
        echo ""
        echo "1. Standard/Full desktop edition"
        echo "2. Lite edition"
        echo ""
        echo "0. Cancel installation"
        echo ""
        echo -n ":> "
        read selection
    done
    case $selection in
        1 ) DESKTOP_PACKAGE="crunchbang-desktop"
            ;;
        2 ) DESKTOP_PACKAGE="crunchbang-desktop-lite"
            ;;
        0 ) clear && echo "Installation cancelled, goodbye! :(\n" && exit 0
    esac
    echo ""
    echo "\"$DESKTOP_PACKAGE\" selected..."
    sleep 1s
    #Set-up CrunchBang repository
    #----------------------------
    clear
    echo "Configuring CrunchBang repository..."
    echo ""
    sleep 1s
    wget http://crunchbang.net/packages-9.04.xx/crunchbang.list -O /etc/apt/sources.list.d/crunchbang.list
    wget -q http://crunchbang.net/crunchbang.key -O- | apt-key add - && apt-get update
    echo ""
    echo "CrunchBang repository set-up complete..."
    echo ""
    sleep 1s
    #Set-up Medibuntu repository
    #---------------------------
    clear
    echo "Configuring Medibuntu repository."
    echo ""
    echo "NOTE: You will be prompted to install the Medibuntu APT key package."
    echo "      Answer \"y\" when prompted."
    echo ""
    echo -n "Press enter to continue..."
    read anykey
    echo ""
    sleep 1s
    wget http://www.medibuntu.org/sources.list.d/jaunty.list -O /etc/apt/sources.list.d/medibuntu.list
    apt-get update && apt-get install medibuntu-keyring && apt-get update
    echo ""
    echo "Medibuntu repository set-up complete..."
    echo ""
    sleep 1s
    #Set-up apt pinning for CrunchBang repository
    #--------------------------------------------
    clear
    echo "Configuring APT..."
    echo ""
    sleep 1s
    if [ -f /etc/apt/preferences ]; then
        cp -f /etc/apt/preferences /etc/apt/preferences~preCrunchBang9.04.01
    fi
    echo "Package: *" > /etc/apt/preferences
    echo "Pin: release a=9.04.xx" >> /etc/apt/preferences
    echo "Pin-Priority: 1001" >> /etc/apt/preferences
    echo "" >> /etc/apt/preferences
    echo "Package: *" >> /etc/apt/preferences
    echo "Pin: release a=jaunty" >> /etc/apt/preferences
    echo "Pin-Priority: 100" >> /etc/apt/preferences
    apt-get update
    echo "APT configuration complete..."
    echo ""
    sleep 1s
    #Perform Upgrade
    #---------------
    clear
    echo "Upgrading existing packages before install..."
    echo ""
    sleep 2s
    apt-get dist-upgrade -y --force-yes
    echo ""
    echo "All packages upgraded..."
    echo ""
    sleep 2s
    #Install packages
    #----------------
    clear
    echo "Installing packages..."
    echo ""
    sleep 2s
    apt-get install -y --install-recommends $DESKTOP_PACKAGE
    echo ""
    echo "All packages installed..."
    echo ""
    sleep 1s
    #Set-up networking for NM?
    #-------------------------
    clear
    echo "Configure networking..."
    echo "By default, CrunchBang uses Network Manager to manage"
    echo "wired and wireless networking. In order for Network Manager"
    echo "to work correctly on first boot a modification needs to be"
    echo "made to \"/etc/network/interfaces\". Hit enter (advised)"
    echo "to accept this change, or enter \"n\" to remove Network Manager."
    echo ""
    echo -n "Use network manager? (Y|n) > "
    read a
    if [ "$a" = "y" ] || [ "$a" = "Y" ] || \
    [ "$a" = "" ]; then
        echo ""
        echo "Creating backup of /etc/network/interfaces..."
        echo ""
        sleep 1s
        mv -f /etc/network/interfaces /etc/network/interfaces-crunchbangBackup
        echo "Modifying /etc/network/interfaces for use with Network Manager..."
        echo ""
        sleep 1s
        echo "auto lo\niface lo inet loopback" > /etc/network/interfaces
    else
        echo ""
        echo "Removing Network Manager..."
        echo ""
        sleep 1s
        apt-get autoremove -y network-manager
        echo ""
    fi
    echo "Network set-up complete..."
    sleep 1s
    #Set-up defaults
    #---------------
    clear
    echo "Setting default applications..."
    echo ""
    sleep 1s
    update-alternatives --set editor /usr/bin/vim.tiny >/dev/null
    if [ -f /usr/bin/firefox-3.0 ]; then
        update-alternatives --set x-www-browser /usr/bin/firefox-3.0 >/dev/null
    fi
    echo ""
    echo "Default applications configured..."
    echo ""
    sleep 1s
    #Set MOTD
    #--------
    if [ -f /etc/motd.tail ]; then
        cp -f /etc/motd.tail /etc/motd.tail~precrunchbang
    fi
    echo "" > /etc/motd.tail
    echo "" >> /etc/motd.tail
    echo "The programs included with CrunchBang Linux are free software;" >> /etc/motd.tail
    echo "the exact distribution terms for each program are described in the" >> /etc/motd.tail
    echo "individual files in /usr/share/doc/*/copyright." >> /etc/motd.tail
    echo "" >> /etc/motd.tail
    echo "CrunchBang Linux comes with ABSOLUTELY NO WARRANTY," >> /etc/motd.tail
    echo "to the extent permitted by applicable law." >> /etc/motd.tail
    echo "" >> /etc/motd.tail
    echo "More information about CrunchBang Linux can be found at:" >> /etc/motd.tail
    echo "http://crunchbang.org/projects/linux/" >> /etc/motd.tail
    echo "" >> /etc/motd.tail
    #Apply CUPS fix
    #--------------
    clear
    echo "Applying CUPS fix..."
    echo ""
    sleep 1s
    cd /etc/ssl/certs/
    make-ssl-cert generate-default-snakeoil
    cd ~
    /etc/init.d/cups restart
    echo ""
    echo "CUPS fix complete..."
    sleep 1s
    #Clean up downloaded packages
    #----------------------------
    clear
    echo "Clean up. A number of packages were downloaded during"
    echo "the set-up routine. These packages are no longer needed,"
    echo "please choose whether or not you would like to remove them."
    echo ""
    echo -n "Remove dowloaded packages? (Y|n) > "
    read a
    if [ "$a" = "y" ] || [ "$a" = "Y" ] || \
    [ "$a" = "" ]; then
        echo ""
        echo "Performing \"apt-get clean\"..."
        sleep 1s
        apt-get clean
    else
        echo ""
        echo "Skipping package cleanup..."
        sleep 1s
    fi
    #Prompt for reboot
    #-----------------
    clear
    echo "Installation complete! Thank you for installing CrunchBang Linux."
    echo "You might want to consider rebooting your system."
    echo ""
    echo -n "Reboot now? (Y|n) > "
    read a
    if [ "$a" = "y" ] || [ "$a" = "Y" ] || \
    [ "$a" = "" ]; then
        echo "Goodbye, hopefully see you on the otherside! :)"
        echo "Rebooting..."
        sleep 1s
        reboot now
        exit
    else
        echo "Issue the following command to reboot:"
        echo ""
        echo "    \$ sudo reboot now"
        echo ""
        echo "Goodbye! :)"
    fi
fi
exit
