#!/bin/bash
# install rpm packages, pulled from Mageia 2
#
# Usage: sh install.sh [OPTIONS]
#
# Options:
# -l ... local install: use only rpms available locally, eg. on memstick
# -r ... remote install: use main Mageia repos to get all packages remotely
# -c ... core install: install only core packages, absolutely needed (eg. emacs)
# -a ... all: install all useful packages
# -h ... help message
# -v ... be verbose
#
# -----------------------------------------------------------------------------

# getopts handling is from Advanced Bash Scripting Guide (getopts example)

echo "entering process options... "
NO_ARGS=0
OPTERROR=65 
if [ $# -eq "$NO_ARGS" ]  # Script invoked with no command-line args?
then
  echo "Usage: `basename $0` options (-hlar)"
  echo "       `basename $0` -h for help"
  exit $OPTERROR          # Exit and explain usage, if no argument(s) given.
fi 
# init
remote=0  # local or remote install
all=0     # full or only core install
repo=""   # repository (UNUSED)
help=0    # print help message
verbose=0 # be verbose

rpm_opts=" -ivh"
#urpmi_opts=" --auto --noclean "
urpmi_opts=" --auto "

local_rpms="nano-2.3.1-1.mga2.x86_64.rpm lib64gif4-4.1.6-9.mga2.x86_64.rpm emacs-common-23.3-8.mga2.x86_64.rpm emacs-23.3-8.mga2.x86_64.rpm  lib64compface1-1.5.2-7.mga1.x86_64.rpm compface-1.5.2-7.mga1.x86_64.rpm pilot-link-common-0.12.5-10.mga2.x86_64.rpm lib64pisock9-0.12.5-10.mga2.x86_64.rpm lib64etpan16-1.1-2.mga2.x86_64.rpm claws-mail-3.8.1-1.mga3.x86_64.rpm lib64xscrnsaver1-1.2.2-1.mga2.x86_64.rpm midori-0.4.4-1.2.mga2.x86_64.rpm imagemagick-6.7.5.10-2.1.mga2.x86_64.rpm flash-plugin-11.2.202.238-release.x86_64.rpm"

local_urpmis="bc gcc gcc-java gcc-c++ ghc make autoconf imagemagick ffmpeg mjpegtools seamonkey konqueror ktorrent mplayer mpg123 totem mono monodevelop"
# add core packages as well, to make -a self-contained
local_urpmis="nano emacs claws-mail midori  $local_urpmis"
# optionally, add these packages
local_urpmis="$local_urpmis texlive cvs svn git java-1.7.0-openjdk java-1.7.0-openjdk-devel java-1.7.0-openjdk-javadoc xfig gnuplot xdvi gv  makagiga flash-plugin mplayer"

# # just for emacs installation add these
# urpmi libncurses
# urpmi libncurses-devel
# urpmi gtk+
# urpmi lib64gtk+2.0-devel
# urpmi libXpm
# urpmi libtiff
# urpmi libjpeg
# urpmi libxpm
# urpmi libgif-devel
# OR just
#  urpmi --noclean libncurses-devel lib64gtk+2.0-devel XFree86-devel Xaw3d-devel


# Usage: scriptname -options
# Note: dash (-) necessary 
while getopts "lrcaR:u:hv" Option
do
  case $Option in
    l     ) remote=0;;
    r     ) remote=1;;
    c     ) all=0;;
    a     ) all=1;;
    #n | o ) echo "Scenario #2: option -$Option-";;
    u     ) echo "Scenario #4: option -u-, with argument \"$OPTARG\" (UNUSED)";;
    R     ) repo=$OPTARG;;
    # Note that option 'q' must have an associated argument,
    # otherwise it falls through to the default.
    h     ) help=1;;
    v     ) verbose=1;;
    *     ) echo "Unimplemented option chosen.";;   # DEFAULT
  esac
done 
shift $(($OPTIND - 1))
# Decrements the argument pointer so it points to next argument. 
#}

#process_options
if [ $help -eq 1 ]
 then no_of_lines=`cat $0 | awk 'BEGIN { n = 0; } \
                                 /^$/ { print n; \
                                        exit; } \
                                      { n++; }'`
      echo "`head -$no_of_lines $0`"
      exit 
fi

# Configure the repo
if [ $remote -eq 1 ] 
  then echo "addmedia of repo ..."
       urpmi.addmedia --distrib --mirrorlist
  else # BEWARE: hard-wired UUID of the memstick with all rpms
       fgrep "Mageia 5" /etc/lsb-release
       if [ $? -eq 0 ] 
        then pushd /run/media/hwloidl/d3521fd1-c026-4eaf-895d-5643da9b24cc/var_cache_urpmi_Mageia5rc1/rpms
        else pushd /run/media/hwloidl/d3521fd1-c026-4eaf-895d-5643da9b24cc/var_cache_urpmi/rpms
       fi
       # pushd /mnt/tmp/var_cache_urpmi/rpms
       urpmi.addmedia file `pwd`

       popd
fi

if [ $all -eq 0 ] 
  then echo "Installing only core packages from this dir ..."

       for f in $local_rpms ; 
       do
	   rpm $rpm_opts $f
       done

  else echo "Installing all packages ..."
       # ls /var/cache/urpmi/rpms/

       for f in $local_urpmis
       do 
         urpmi $urpmi_opts $f
       done

fi

exit 0

function mpi_user () {
# More information on package mpich2-1.2.1-2.mga2.x86_64
# Post-installation procedure:
# - create a user with constant uid/gid and shared home directory on all nodes
# - ensure this user has rsh/ssh access to all nodes
# For instance, a solution could be:
groupadd -g 12384 -r -f mpi 
useradd -u 12384 -g mpi -d /home/mpi -r -s /bin/bash mpi -p "" -m
mkpasswd -l 32 | passwd --stdin mpi 2>&1 > /dev/null
ssh-keygen -f /home/mpi/.ssh/id_dsa -t dsa -N "" 2>&1 > /dev/null
chown mpi:mpi /home/mpi/.ssh/id_dsa
chmod 600 /home/mpi/.ssh/id_dsa
}
