04c0817e163ac1149d4d659445ba826db6d7c457
[build.git] / mirroring / mirror.sh
1 #!/bin/bash
2 # $Id$
3
4 COMMAND=$(basename $0)
5 DIRNAME=$(dirname $0)
6
7 default_url="http://localhost/mirror/"
8 default_distro="f8"
9 all_distros="fc4 fc6 f7 f8 centos5"
10
11 function check_distro () {
12     local distro=$1; shift
13     if [ ! -d $DIRNAME/$distro ] ; then
14         echo "Distro $distro not supported - skipped"
15         return 1
16     fi
17     return 0
18 }
19
20 function do_init () { 
21     local distro=$1; shift
22     repo=/etc/vservers/.distributions/$distro/yum.repos.d/building.repo
23     dir=/etc/vservers/.distributions/$distro/yum.repos.d/
24     if [ ! -d $dir ] ; then
25         [ -n "$VERBOSE" ] && echo Creating dir $dir
26         mkdir -p -d $dir
27     fi
28     [ -n "$VERBOSE" ] && echo "Creating $repo"
29     sed -e "s,@MIRRORURL@,$URL," < $DIRNAME/$distro/building.repo.in > $repo
30 }
31
32 function do_display () {
33     local distro=$1; shift
34     dir=/etc/vservers/.distributions/$distro/yum.repos.d/
35     if [ -d $dir ] ; then
36         echo "====================" Contents of $dir
37         find $dir -name '*.repo' | xargs head --verbose --lines=1000
38     else
39         echo "====================" $dir does not exist
40     fi
41 }
42
43 function do_clear () {
44     local distro=$1; shift
45     repo=/etc/vservers/.distributions/$distro/yum.repos.d/building.repo
46     [ -n "$VERBOSE" ] && echo Removing $repo
47     rm $repo
48 }
49
50 function do_superclean () {
51     local distro=$1; shift
52     dir=/etc/vservers/.distributions/$distro/yum.repos.d/
53     [ -n "$VERBOSE" ] && echo Removing all repo files in $dir
54     rm $dir/*.repo
55 }
56
57 function usage () {
58     echo "Usage $COMMAND [options] <command>"
59     echo "  a help to manage the yum.repos.d template in /etc/vservers/.distributions/<distro>"
60     echo "Available commands"
61     echo "  init: creates /etc/vservers/.distributions/<distro>/yum.repos.d/building.repo"
62     echo "       default is to use mirror root at $default_url"
63     echo "       use -u URL to specify another location"
64     echo "  display: shows content"
65     echo "  clean: removes building.repo"
66     echo "  superclean: removes yum.repos.d altogether"
67     echo "Options"
68     echo "  -f <distro> : defaults to $default_distro"
69     echo "  -a : runs on all distros $all_distros"
70     echo "  -v : verbose"
71     echo "Examples"
72     echo "  $COMMAND -a display "
73     echo "  $COMMAND -a superclean"
74     echo "  $COMMAND -a -u http://mirror.one-lab.org/ init"
75     echo "  $COMMAND -a display"
76     exit 1
77 }
78     
79 DISTROS=""
80 URL=""
81 VERBOSE=""
82
83 function main () {
84
85     while getopts "u:f:av" opt; do
86         case $opt in
87             u) URL=$OPTARG ;;
88             f) DISTROS="$DISTROS $OPTARG" ;;
89             a) DISTROS="$DISTROS $all_distros" ;;
90             v) VERBOSE=true ;;
91             *) usage ;;
92         esac
93     done
94
95     shift $(($OPTIND - 1))
96     
97     # no action = display
98     case "$#" in 
99         0)
100             action=display ;;
101         1) 
102             action=$1; shift
103             case $action in
104                 init*) action=init ;;
105                 dis*) action=display ;;
106                 clea*) action=clear ;;
107                 super*) action=superclean ;;
108                 *) usage ;;
109             esac ;;
110         *)
111             usage ;;
112     esac
113
114     [ -z "$URL" ] && URL=$default_url
115     [ -z "$DISTROS" ] && DISTROS="$default_distro"
116
117     # remove trailing slash
118     URL=$(echo $URL | sed -e 's,/$,,')
119
120     for distro in $DISTROS; do
121         [ -n "$VERBOSE" ] && echo ==================== Running $action for $distro
122         check_distro $distro && do_$action $distro
123     done
124
125     exit 0
126 }
127
128 main "$@"