Use basename when checking the script name
[ext/instance-debootstrap.git] / common.sh
1
2 SCRIPT_NAME=$(basename $0)
3
4 for dir in /lib/udev /sbin; do
5   if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
6     VOL_ID=$dir/vol_id
7   fi
8 done
9
10 if [ -z "$VOL_ID" ]; then
11     echo "vol_id not found, please install udev"
12     exit 1
13 fi
14
15 get_api5_arguments() {
16   TEMP=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
17   if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
18   # Note the quotes around `$TEMP': they are essential!
19   eval set -- "$TEMP"
20   while true; do
21     case "$1" in
22       -i|-n) instance=$2; shift 2;;
23
24       -o) old_name=$2; shift 2;;
25
26       -b) blockdev=$2; shift 2;;
27
28       -s) swapdev=$2; shift 2;;
29
30       --) shift; break;;
31
32       *)  echo "Internal error!"; exit 1;;
33     esac
34   done
35   if [ -z "$instance" -o -z "$blockdev" ]; then
36     echo "Missing OS API Argument"
37     exit 1
38   fi
39   if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
40     echo "Missing OS API Argument"
41     exit 1
42   fi
43   if [ "$SCRIPT_NAME" = "rename" -a -z "$new_name"  ]; then
44     echo "Missing OS API Argument"
45     exit 1
46   fi
47 }
48
49 get_api10_arguments() {
50   if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
51     echo "Missing OS API Variable"
52     exit 1
53   fi
54   instance=$INSTANCE_NAME
55   if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
56     echo "At least one disk is needed"
57     exit 1
58   fi
59   blockdev=$DISK_0_PATH
60   if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
61     echo "Missing OS API Variable"
62   fi
63   old_name=$OLD_INSTANCE_NAME
64 }
65
66 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
67   OS_API_VERSION=5
68   get_api5_arguments
69 elif [ "$OS_API_VERSION" = "10" ]; then
70   get_api10_arguments
71   if [ $SCRIPT_NAME = "import" -o $SCRIPT_NAME = "export" ]; then
72     echo "import/export still not compatible with API version 10"
73     exit 1
74   fi
75 else
76   echo "Unknown OS API VERSION $OS_API_VERSION"
77   exit 1
78 fi