#! /bin/bash -

#---------------------------------------------------------------------#
#                 check to see if vim is installed                    #
#---------------------------------------------------------------------#

echo
echo "---- Checking to see if we have vim installed... -----------------------"
echo

vim_var=$(which vim)

if [ $vim_var ]
then
    echo "Yes, we have vim installed. Continuing..."
else
    echo "You need to install vim first, douche. Exiting..."
    echo
    exit
fi

#---------------------------------------------------------------------#
#             grab a date_time stamp for backing up files             #
#---------------------------------------------------------------------#

dt_stamp=$(date +%Y%m%d_%H%M%S)

#---------------------------------------------------------------------#
#               build the PS1_user for a normal user                  #
#---------------------------------------------------------------------#

echo
echo "---- Updating PS1 to make it nice and pretty colors... -----------------"
echo 

PS1_user='\[\e[1;33m\]'   # set color (yellow)
PS1_user+='['             # opening bracket

PS1_user+='\[\e[1;34m\]'  # set color (blue)
PS1_user+='\u'            # user

PS1_user+='\[\e[1;31m\]'  # set color (red)
PS1_user+='@'             # separator

PS1_user+='\[\e[1;33m\]'  # set color (yellow)
PS1_user+='\h'            # host

PS1_user+='\[\e[1;37m\]'  # set color (white)
PS1_user+=':'

PS1_user+='\[\e[1;36m\]'  # set color (cyan)
PS1_user+='\w'            # working directory

PS1_user+='\[\e[1;33m\]'  # set color (yellow)
PS1_user+=']'             # closing bracket

PS1_user+='\[\e[0m\]'     # end ch (text reset)

PS1_user+='$ '            # $ for normal user

for user in $(ls /home/)
do
    echo $user
    if [ -e /home/$user/.bashrc ]
    then
	$(cp /home/$user/.bashrc /home/$user/.bashrc_$dt_stamp)
	$(chown $user /home/$user/.bashrc_$dt_stamp)
	$(chgrp $user /home/$user/.bashrc_$dt_stamp)
	echo "Your /home/$user/.bashrc has been saved as .bashrc_$dt_stamp"
	$(echo >> /home/$user/.bashrc)
	$(echo "## color prompt added by configureMe script" >> /home/$user/.bashrc)
	$(echo "#" >> /home/$user/.bashrc)
	$(echo "PS1='$PS1_user'" >> /home/$user/.bashrc)
	$(echo >> /home/$user/.bashrc)
	$(echo "export PS1" >> /home/$user/.bashrc)
	$(echo >> /home/$user/.bashrc)
	echo
    else
        echo "--nope, not here"
    fi
done

#---------------------------------------------------------------------#
#                   build the PS1_user for root                       #
#---------------------------------------------------------------------#

PS1_root='\[\e[1;33m\]'     # set color (yellow)
PS1_root+='['               # opening bracket

PS1_root+='\[\e[1;31m\]'    # set color (red)
PS1_root+='\u'              # user

PS1_root+='\[\e[1;37m\]'    # set color (white)
PS1_root+='@'               # separator

PS1_root+='\[\e[1;33m\]'    # set color (yellow)
PS1_root+='\h'              # host

PS1_root+='\[\e[1;37m\]'    # set color (white)
PS1_root+=':'               # separator

PS1_root+='\[\e[1;36m\]'    # set color (cyan)
PS1_root+='\w'              # working directory

PS1_root+='\[\e[1;33m\]'    # set color (yellow)
PS1_root+=']'               # closing bracket

PS1_root+='\[\e[0m\]'       # end ch (text reset)

PS1_root+='# '              # hash (#) for root

echo "root"

if [ -e /root/.bashrc ]
then 
    $(cp /root/.bashrc /root/.bashrc_$dt_stamp)
    echo "Your /root/.bashrc has been saved as .bashrc_$dt_stamp"
fi

$(echo >> /root/.bashrc)
$(echo "## color prompt added by configureMe script" >> /root/.bashrc)
$(echo "#" >> /root/.bashrc)
$(echo "PS1='$PS1_root'" >> /root/.bashrc)
$(echo >> /root/.bashrc)
$(echo "export PS1" >> /root/.bashrc)
$(echo >> /root/.bashrc)

#---------------------------------------------------------------------#
#                          wget sources.list                          #
#---------------------------------------------------------------------#

echo
echo "---- Downloading and installing sources.list --------------------------"
echo

#---------------------------- stable ---------------------------------#

printf "Do you want to download your standard Debian STABLE sources.list? [y/N]? "
read answer
echo

if [ "$answer" = "Y" ] || [ "$answer" = "y" ]
then
    echo "You answered yes..."
    echo
    $(cp /etc/apt/sources.list /etc/apt/sources.list_$dt_stamp)
    echo "Your sources.list has been copied and saved to sources.list_$dt_stamp"
    echo
    # wget options
    # -N is the option to specify to NOT retrieve the file unless it is newer than the local
    # -O is the option to specify the OUTPUT path and file name to download to
    # -P is the option to specify the PATH to download to, using same filename (as follows)
    $(wget -N https://nix.kedrovsky.net/_media/config_script/gk_config_sources_stable.list -P /etc/apt/ )
    $(mv /etc/apt/gk_config_sources_stable.list /etc/apt/sources.list)
else
    echo "You answered no..."
    echo "Your /etc/apt/sources.list has been left untouched. Onward and upward..."
fi

#---------------------------- testing --------------------------------#

printf "Do you want to download your standard Debian TESTING sources.list? [y/N]? "
read answer
echo

if [ "$answer" = "Y" ] || [ "$answer" = "y" ]
then
    echo "You answered yes..."
    echo
    $(cp /etc/apt/sources.list /etc/apt/sources.list_$dt_stamp)
    echo "Your sources.list has been copied and saved to sources.list_$dt_stamp"
    echo
    # wget options
    # -N is the option to specify to NOT retrieve the file unless it is newer than the local
    # -O is the option to specify the OUTPUT path and file name to download to
    # -P is the option to specify the PATH to download to, using same filename (as follows)
    $(wget -N https://nix.kedrovsky.net/_media/config_script/gk_config_sources_testing.list -P /etc/apt/ )
    $(mv /etc/apt/gk_config_sources_testing.list /etc/apt/sources.list)
else
    echo "You answered no..."
    echo "Your /etc/apt/sources.list has been left untouched. Onward and upward..."
fi


#---------------------------------------------------------------------#
#                            wget .vimrc                              #
#---------------------------------------------------------------------#

echo
echo "----- Downloading and installing .vimrc -------------------------------"
echo

# wget options
# -N is the option to specify to NOT retrieve the file unless it is newer than the local
# -O is the option to specify the OUTPUT path and file name to download to
# -P is the option to specify the PATH to download to, using same filename (as follows)

$(wget -N https://nix.kedrovsky.net/_media/config_script/gk_config_vimrc -P /var/tmp/)

## users
for user in $(ls /home/)
do
    echo "#--------------------#"
    echo "# user: $user"
    echo "#--------------------#"
    echo
    # Make a backup copy of each users' .vimrc if it exists
    if [ -e /home/$user/.vimrc ]
    then
        $(cp /home/$user/.vimrc /home/$user/.vimrc_$dt_stamp)
        $(chown $user /home/$user/.vimrc_$dt_stamp)
        $(chgrp $user /home/$user/.vimrc_$dt_stamp)
        echo "Your /home/$user/.vimrc has been saved as .vimrc_$dt_stamp"
    else
        echo "--nope, no .vimrc"
    fi
    # Copy my .vimrc into each users' subdir
    $(cp /var/tmp/gk_config_vimrc /home/$user/.vimrc)
    $(chown $user /home/$user/.vimrc)
    $(chgrp $user /home/$user/.vimrc)
done

## root
# Make a backup copy of current .vimrc if it exist
echo
echo "#--------------------#"
echo "# user: root"
echo "#--------------------#"
echo
if [ -e /root/.vimrc ]
then 
    $(cp /root/.vimrc /root/.vimrc_$dt_stamp)
    echo "Your /root/.vimrc has been saved as .vimrc_$dt_stamp"
fi
# Copy my .vimrc into each users' subdir
$(cp /var/tmp/gk_config_vimrc /root/.vimrc)

echo

#---------------------------------------------------------------------#
#                     wget gman vim color scheme                      #
#---------------------------------------------------------------------#

echo
echo "----- Downloading and installing vim color scheme gman1 ----------------"
echo

vim_colorscheme_dir=$(find /usr/share/vim -type d -name colors)

echo "vim_colorscheme_dir: $vim_colorscheme_dir"
echo

# wget options
# -N is the option to specify to NOT retrieve the file unless it is newer than the local
# -O is the option to specify the OUTPUT path and file name to download to
# -P is the option to specify the PATH to download to, using same filename (as follows)

$(wget -O $vim_colorscheme_dir/gman1.vim -N https://nix.kedrovsky.net/_media/config_script/gk_config_gman1.vim -P $vim_colorscheme_dir/)

echo
echo "-------------------------------- done ----------------------------------"
echo
