#!/bin/sh
############################################################
#                                                          #
# lget - get sources from the net                          #
#                                                          #
############################################################
# leach is part of the sorcery spell management utility    #
# Copyright 2001 by Kyle Sallee                            #
############################################################
#                                                          #
# this WAS the leach script of a source based Linux distro,#
# calls Sorcerer GNU/Linux, or SGL. SGL is no longer       #
# available with GPL license. Since this script was taken  #
# before licensing scheme change, no legal problems I      #
# guess.                                                   #
#                                                          #
# the code is re-written for Lunar. The previous Copyright #
# notices are kept; just in case some code is left :=)     #
# Kagan Kongar <kongar@tsrsb.org.tr>, 20020519             #
#                                                          #
############################################################
#                                                          #
# Parts Copyrighted Jason Johnston  2002 under GPLv2       #
#                                                          #
# Parts Copyrighted Kagan Kongar 2002 under GPLv2          #
#                                                          #
############################################################



help() {

  cat  <<  EOF

Lget downloads single or multiple module source files.

Example: lget  nano hdparm sudo
Usage:  lget  [parameters] [modules]

Typing lget with no modules causes lget to download 
every available source specified in the moonbase.

Optional Parameters:

       --from directory Specify an alternate for $SOURCE_CACHE
       --url URL  Specify an alternate download URL
EOF

  exit  1

}


process_parameters()  {

  while  [  -n  "$1"  ];  do

    if  echo  "" $1  |  grep  -q  "^ -";  then

      case  $1  in
         --from)  SOURCE_CACHE=$2;  shift 2  ;;
         --help)  help;             exit  1  ;;
          --url)  BASE_URL="$2";    shift 2  ;;
              *)  help                       ;;
      esac

    else  shift
    fi

  done

}


strip_parameters()  {

  while  [  -n  "$1"  ];  do

    if  echo  "" $1  |  grep  -q  "^ -";  then

      case  $1  in
        --from)  shift 2  ;;
        --help)  shift 1  ;;
         --url)  shift 2  ;;
             *)  shift 1  ;;
      esac

    else  echo  $1
          shift 
    fi

  done

}


main() {

  cd  /tmp
  process_parameters        $*
  MODULES=`strip_parameters  $*`

  if  [  -z  "$MODULES"  ];  then
    verbose_msg "downloading entire moonbase"
    MODULES=$(list_moonbase | sort)
  fi

  for MODULE in $MODULES; do
    if ! lget_locked ; then
      lget_lock
    
      [ "$VEBOSE" == "on" ] && echo "+ downloading module \"$MODULE\""
      download_module
    
      lget_unlock
    else
      false
    fi
  done

}


. /etc/lunar/config

root_check || exit 1

renice +10 -p $$  >/dev/null
main  $*

